gpt4 book ai didi

c++ - 数组不接受完整数字

转载 作者:行者123 更新时间:2023-11-27 22:59:29 24 4
gpt4 key购买 nike

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

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

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

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

};

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

最佳答案

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

关于c++ - 数组不接受完整数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29206733/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com