从数组中输出特定值并获取正确的值

时间:2012-05-05 22:24:59

标签: c++ arrays

我在编写这段代码时遇到了另一个问题(这里有程序员的帮助),我遇到了输出正确值的程序的问题。 beadArrayTop应输出如下内容:

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

而是输出,只有0表示4的位置。

beadArrayBottom做了很多相同的事情。我尝试将函数printArray放在beadArrayTop和beadArrayBottom之前,甚至将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;
}
//*********************************************//

2 个答案:

答案 0 :(得分:3)

您是shadowing the variable beadArray

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

在此处删除int beadArray[MAX]声明,然后全局值将在范围内,您将改为修改它。

通常情况下,编译器会警告您这一点。如果您的编译器没有就此发出警告,请打开警告。 (对于gcc,我喜欢-Wall -Wextra,但其他人喜欢进一步发出警告。这是一个很好的最低限度。)

答案 1 :(得分:3)

你的问题是你正在创建两个数组,一个在堆栈上,一个是全局数组。

int beadArray[MAX];

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

    ...
}

当你调用startArray时,它使用本地的beadArray,因为它会影响printArray范围内的全局数组。

您可能希望了解引用并尽可能地限制变量的范围,以避免将来出现此类错误。

相关问题