数组不接受完整数字

时间:2015-03-23 09:24:46

标签: c++ arrays

首先,我是编程新手,所以如果我的问题看起来很基本,我会事先道歉!

我正在尝试构建牛和公牛游戏,我将一个随机的9位数代码(在0-1之间)存储在一个数组中,然后将其与用户输入的另一个数组进行对比。程序然后在正确的位置输出正确的数字作为公牛,并将正确的数字输出到错误的位置作为奶牛。

我遇到的问题是,当我输入一个与密码匹配的号码时,我需要在每个数字后按Enter键才能正确输入。例如,当我输入以下内容时:

010101010作为1010101010返回

000000001返回为1

000000000返回为0

正如你所看到的那样,1似乎很好,但是如果它们是第一个数字则不会被拿起。

Here is the code:
#include <iostream>
#include <ctime>

using namespace std;

void gameOne();
void gameTwo();
void gameThree();
void getthemagicnumber(int magicnoarray[], int size);
void gettheplayernumber(int usernoarray[], int size);
void checkthenumbers(int magicnoarray[], int usernoarray[], int size);

int main()
{
    int menuChoice = 0;


        cout << "    //////////////////////////\\\\\\\\\\\\\\\\\\\\\\\n"
        "   //         Welcome to bulls and cows            \\\n"
        "  //  Where everything is not quite as it seems     \\\n"
        " //               Press 1 to play me                 \\\n"
        "//           Press 2 to let me play                   \\\n"
        "\\       Press 3 to play me with a twist              //\n"
        " \\               Press 4 to exit                    //\n"
        "  \\\\\\\\\\\\\\\\\\\\\\\\\\\/////////////////////////\n" << endl;
        cin >> menuChoice;

        switch (menuChoice)     // menu select
        {
        case 1:
            gameOne();
            break;

        case 2:
            gameTwo();
            break;

        case 3:
            gameThree();
            break;

        default:
            cout << "Thanks for playing, have a great day!" << endl;
        }
}


void gameOne()
{
    int magicnoarray[9], usernoarray[9];
    int size = 9;

    srand((unsigned)time(NULL));

    getthemagicnumber(magicnoarray, size);
    gettheplayernumber(usernoarray, size);

//  checkthenumbers(magicnoarray, usernoarray, size);
//  display the results;

//  do the above until either bulls = 9 OR 
//      number of goes = 7, or whatever you want





    cout << "your guess was: ";

    for (int i = 0; i < 9; i++)
    {
        cout << usernoarray[i]<<" ";
    }
    cout << endl;

    cout << "my guess was:   ";

    for (int i = 0; i < 9; i++)
    {
        cout << magicnoarray[i] << " ";
    }
    cout << endl;

}


void getthemagicnumber(int array[],int size)
{
    for (int i = 0; i < 9; i++)
    {
        array[i] = rand() %2; 
    }
};


void  gettheplayernumber(int array[], int size)
{
    for (int i = 0; i < size; i++)
    {
        cout << "Please enter your guess: ";
        cin >> array[i];
    }
};



void gameTwo()
{

};

void gameThree()
{

};

提前感谢您的帮助,非常感谢!

1 个答案:

答案 0 :(得分:4)

您无法读取整数中的前导零。您必须将其作为字符串读取,然后根据需要进行处理。