gpt4 book ai didi

c++ - 数组c++中的随机数据丢失

转载 作者:行者123 更新时间:2023-11-30 01:13:43 25 4
gpt4 key购买 nike

这是我的问题....在下面的代码中,在 char guess[4]cin >> guess 我的 answer 之间索引 0 处的元素消失了。谁知道为什么?

while(bullCount != 4)
{
//Create the answer
char answer[4];

for(int loops = 0; loops < 4; loops++)
{
answer[loops] = createAnswerDigit(seed);
}

//Reset bullCount and cowCount from previous loop
bullCount = 0;
cowCount = 0;

cout << "Enter your guess [1000-9999]: ";

//Guess by player
char guess[4];

cout << "Answer[0]: " << answer[0] << "\n"; //Prints "Answer[0]: 4

//Retrieve guess by player
cin >> guess;

cout << "Answer[0]: " << answer[0] << "\n" //Prints "Answer[0]: "

for(int digitLoc = 3; digitLoc >= 0; digitLoc--)
{
//Do check backwards to prevent mistaking bulls for cows
int check = checkGuess(guess[digitLoc], digitLoc, answer);

if(check == cow)
{
cowCount++;
}

else if(check == bull)
{
bullCount++;
}
}
}

最佳答案

guess 的大小为 4,但是,您输入了一个介于 1000 - 9999 之间的数字,即 4 > 字符长。请记住,在一个字符串中,字符串的末尾必须有一个\0,所以guess少了1个字符。这可能会覆盖 answer 的第一个元素。尝试让 guess 大小为 5,或者更好的是,改用 std::string

关于c++ - 数组c++中的随机数据丢失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31541652/

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