【问题标题】:Array not accepting full number数组不接受全数
【发布时间】:2015-05-26 05:39:06
【问题描述】:

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

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

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

010101010 返回为 1010101010

000000001 返回为 1

000000000 返回为 0

如您所见,1 似乎没问题,但如果 0 是第一个数字,则不会拾取它们。

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()
{

};

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

【问题讨论】:

  • 不相关:除了占用激活堆栈上的空间之外,我会在getthemagicnumber 中使用参数size,因为您在传递它时遇到了麻烦。关于你的问题。您需要一次处理一个字符的输入字符串。

标签: c++ arrays


【解决方案1】:

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

【讨论】:

  • 非常感谢您的帮助,在这种情况下,我将进一步研究字符串!编辑:我会支持你,但似乎我还不能,但再次感谢:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
  • 2015-11-27
  • 1970-01-01
  • 2019-07-28
  • 2022-01-12
  • 1970-01-01
  • 2019-09-26
相关资源
最近更新 更多