2012-05-05 136 views
1

我在編寫此代碼時遇到了另一個問題(這裏有很多程序員的幫助),而且我遇到了輸出正確值的程序問題。 beadArrayTop應該輸出的東西看起來像這樣:從數組中輸出特定值並獲取正確的值

4 * 4 * 4 * 4 * 4 * 4 * 

,而不是其輸出,只用0,其中4的應該是。

beadArrayBottom也有很多相同之處。我試着在beadArrayTop和beadArrayBottom之前放置函數printArray,甚至將beadArrayTop和beadArrayBottom放入printArray中,但無濟於事。爲了使我的輸出正確,我應該做什麼提示?下面是代碼:

#include <iostream> 
#include <iomanip> 



using namespace std; 

const int MAX = 14; 
void line (int &cycleCounter, int &starCounter); //outputs a star followed by six spaces 
void solidLine(); //outputs a solid line of stars 
void smallerLine(); //ouputs a smaller line of stars 
void board(); //outputs the mancala board, with numbers indicating the bins. 
void binNumbersTop(); //outputs numbers indicating the bin # (Top only). 
void binNumbersBottom(); //outputs numbers indicating the bin # (Bottom only). 
void beadArrayTop(); //outputs the array for the top. 
void beadArrayBottom(); //outputs the array for the bottom. 
void beadArrayMiddle(); //outputs the array for the end bins. 
void startArray (int beadArray [MAX]); //Creates an array of 14, with 4 in every slot except for 6 and 13. 
void printArray(); //Outputs the array mentioned above 
int beadArray[MAX]; 



int main() 

{ 





    board(); 
    cout<<endl; 












    cout<<endl; 
    system("pause"); 
    return 0; 




} 


//**********************************************// 
void line() 
{ 
    int cycleCounter = 9; 
    int starCounter = 6; 

    while (cycleCounter > 0) 
    { 
     cout<<"*"; 


     int spaceCounter = 1; 

     while (spaceCounter > 0) 
     { 
      cout<<"  "; 
      spaceCounter = spaceCounter - 1; 
      starCounter = starCounter - 1; 
      cycleCounter = cycleCounter - 1; 
     } 
    } 
} 

//************************************************// 
void solidLine() 

{ 
    int loopCounter; 
    loopCounter = 57; 

    while (loopCounter > 0) 
    { 
     cout<<"*"; 
     loopCounter = loopCounter - 1; 
    } 

} 
//************************************************// 
void smallerLine() 
{ 
    int loopCounterTwo; 
    loopCounterTwo = 43; 
    cout<<"* 13 "; 
    while (loopCounterTwo > 0) 
    { 
     cout<<"*"; 
     loopCounterTwo = loopCounterTwo - 1; 
    } 
    cout<<" 6 *"; 
} 
//************************************************// 
void binNumbersTop() 
{ 
    cout<<"*"; 
    int topNumbers = 1; 
    cout << setw (7)<<"*"; 
    cout <<setw (4)<< 0; 
    cout << setw (3)<<"*"; 

    while (topNumbers < 6) 
    { 
     cout <<setw (4)<< topNumbers; 
     cout <<setw (3)<<"*"; 
     topNumbers = topNumbers + 1; 
    } 

    cout << setw (7)<<"*"<<endl; 

} 


//**********************************************// 
void binNumbersBottom() 
{ 
    cout<<"*"; 
    int bottomNumbers = 11; 
    cout << setw (7)<<"*"; 
    cout <<setw (4)<< 12; 
    cout << setw (3)<<"*"; 

    while (bottomNumbers >= 7) 
    { 
     cout <<setw (4)<< bottomNumbers; 
     cout <<setw (3)<<"*"; 
     bottomNumbers = bottomNumbers - 1; 
    } 

    cout << setw (7)<<"*"<<endl; 

} 

//**********************************************// 

void beadArrayTop() 
{ 

    cout<<"*"; 
    cout <<setw (7)<<"*"; 
    cout <<setw (4) <<beadArray[0]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[1]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[2]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[3]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[4]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[5]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (7) <<"*"; 

} 
//***********************************************// 
void beadArrayBottom() 
{ 

    cout<<"*"; 
    cout <<setw (4)<<beadArray[13]; 
    cout <<setw (3)<<"*"; 
    cout <<setw (4) <<beadArray[12]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[11]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[10]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[9]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[8]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[7]; 
    cout <<setw (3) <<"*"; 
    cout <<setw (4) <<beadArray[6]; 
    cout<< setw (3) <<"*"; 

} 
//***********************************************// 

void board() 
{ 



solidLine(); 
cout<<endl; 


line(); 
cout<<endl; 



binNumbersTop(); 


line(); 
cout<<endl; 
beadArrayTop(); 

cout<<endl; 
line(); 
cout<<endl; 


smallerLine(); 
cout<<endl; 
line(); 
cout<<endl; 


binNumbersBottom(); 


line(); 
cout<<endl; 
beadArrayBottom(); 
cout<<endl; 
line(); 
cout<<endl; 





solidLine(); 
cout<<endl; 


} 

//*********************************************// 

void startArray (int beadArray[MAX]) 
{ 
    for(int i=0; i<MAX; ++i) 
    { 
     beadArray[i]=4; 
    } 
    beadArray[6]=0; 
    beadArray[13]=0; 
} 
//*********************************************// 
void printArray() 
{ 
    startArray (beadArray); 
    for(int i=0; i<MAX; i++) 
    cout << beadArray[i]; 
    cout<<endl<<endl; 
} 
//*********************************************// 

回答

3

您是shadowing the variablebeadArray

void printArray() 
{ 
    int beadArray[MAX]; 
    startArray (beadArray); 

這裏取下int beadArray[MAX]聲明,則全局值將在規模,你會被代替修改它。

通常情況下,編譯器會向您發出警告。如果你的編譯器沒有提醒你這個問題,請打開警告。 (對於gcc,我喜歡-Wall -Wextra,雖然其他人喜歡將警告進一步提高。這是一個很好的最低限度。)

+0

我拿出int beadArray [MAX];但是當我輸出數組時,我仍然得到0。 – chrisvx930

+0

我已經放好了所有的代碼,這樣你就可以看到這裏發生了什麼。 – chrisvx930

+0

有些東西似乎很奇怪,你正在改變'printArray()'調用的函數中數組的內容 - 也許該方法應該被命名爲'initializeAndPrintArray()'或者更具描述性的東西? (_Why_是否在打印時初始化陣列?這在某種程度上限制了陣列的實用程序_和_打印功能...) – sarnold

3

你的問題是你正在創建兩個數組,一個在堆棧和一個全局數組。

int beadArray[MAX]; 

void printArray() 
{ 
    int beadArray[MAX]; 
    startArray (beadArray); 

    ... 
} 

當您調用startArray時,它使用本地beadArray,因爲它會在printArray的作用域中遮住全局數組。

您可能想了解參考資料並儘量限制變量的範圍,以避免將來出現類似錯誤。