1

I've run into another issue while writing this code (with MUCH help from programmers here) and I'm having an issue with the program outputting the correct values. beadArrayTop should output something looking like this:

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

instead it outputs that, only with 0's where the 4's should be.

beadArrayBottom does much of the same. I've tried putting the function printArray before beadArrayTop and beadArrayBottom, and even putting beadArrayTop and beadArrayBottom inside of printArray, but to no avail. Any hints on what I should do in order to make my output correct? Here is the code:

    #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;
}
//*********************************************//
4

2 回答 2

3

You are shadowing the variable beadArray:

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

Remove the int beadArray[MAX] declaration here, then the global value will be in scope, and you will be modifying it instead.

Normally, a compiler would warn you about this. If your compiler did not warn you about this, turn up the warnings. (For gcc, I like -Wall -Wextra, though other people like to turn up the warnings even further. This is a good minimum.)

于 2012-05-05T22:31:51.530 回答
3

Your problem is that you're creating two arrays, one on the stack and one global array.

int beadArray[MAX];

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

    ...
}

When you call startArray, it uses the local beadArray because it shadows the global array in printArray's scope.

You may want to learn about references and limit the scope of your variables as much as possible to avoid mistakes like this in the future.

于 2012-05-05T22:33:26.967 回答