I am making a simple guessing game in C, but when the guess is correct it wont exit the loop. Prob a dumb question but I am new to C. Below is a copy of the code if anyone can help thank you.
我用C语言做了一个简单的猜谜游戏,但当猜测正确时,它不会退出循环。问一个愚蠢的问题,但我对C语言还不熟悉,下面是代码的副本,如果有人能帮忙的话,谢谢。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <stdbool.h>
int getGuess()
{
int guess;
printf("Enter your guess: ");
scanf("%d", &guess);
return guess;
}
int playGuessingGame()
{
int guess = getGuess();
int answer = sqrt(rand() % 100 + 10);
bool solved = false;
while (!solved)
{
if (guess == answer)
{
printf("You got it!\n");
solved = true;
break;
}
else if (guess < answer)
{
printf("To Low!\n");
guess = getGuess();
}
else
{
printf("To High!\n");
guess = getGuess();
}
}
return 0;
}
int main(void)
{
printf("Welcome to the guessing game!\n");
while (1) // infinite loop
{
printf("\nEnter Y to play or Q to quit: "); // ask for each iteration
char input;
scanf(" %c", &input); // Use " %c" to consume leading whitespace left over.
if (input == 'Q') // break infinite loop
{
break;
}
if (input == 'Y')
{
playGuessingGame();
}
else
{
printf("Invalid input\n");
}
}
}
I am trying to make it so only the print statement goes through, and it doesnt ask for the guess again.
我正在尝试让它只通过打印语句,并且它不会再次要求猜测。
更多回答
Seems to be some code missing...
似乎有一些代码丢失了。
I can send you more code from the file what else is needed @Fe2O3
我可以从文件中给你发送更多代码@Fe2O3还需要什么
while (input != 'Q') { if (input == 'Y') { playGuessingGame(); } else { printf("Invalid input\n");
So-o-o-o the value of input
is always 'Y'
and this loop is re-running the same function... Easy when you show your code...
WHILE(INPUT!=‘Q’){IF(INPUT==‘Y’){playGuessingGame();}ELSE{printf(“无效输入\n”);so-o-o-o输入的值始终为‘Y’,此循环将重新运行相同的函数...当你展示你的代码时很容易...
Also please change scanf("%c", &input);
to scanf(" %c", &input);
Note the leading space in the format string. The %c
format will otherwise read all characters, including the newline from the previous input. The leading space tells scanf
to skip all space, including newline.
另外,请将scanf(“%c”,&Input);更改为scanf(“%c”,&Input);请注意格式字符串中的前导空格。否则,%c格式将读取所有字符,包括上一次输入的换行符。前导空格告诉scanf跳过所有空格,包括换行符。
优秀答案推荐
Rearranging the code of main()
:
重新排列main()的代码:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main( void )
{
srand(time(NULL)); // seed the random number generator.
printf("Welcome to the guessing game!\n");
while (1) // infinite loop
{
printf("\nEnter Y to play or Q to quit: "); // ask for each iteration
char input;
scanf(" %c", &input); // Use " %c" to consume leading whitespace left over.
// credit @SomeProgrammerDude
if (input == 'Q') // break infinite loop
{
break;
}
if (input == 'Y')
{
playGuessingGame();
}
else
{
printf("Invalid input\n");
}
}
}
更多回答
I implemented this code and the output is still the same. it says "Welcome to the guessing game! Enter Y to play or Q to quit: Y Enter your guess: 7 You got it! Enter your guess: "
我实现了这段代码,输出仍然是一样的。上面写着“欢迎玩猜谜游戏!输入Y玩或Q退出:Y输入你的猜想:7你猜对了!输入你的猜想:”
@MichaelFerguson Have you tried recompiling? The code in your question is still incomplete, so anything is possible (including undefined behaviour.) Suggest you edit the question adding the full code so that we can compile and test for ourselves... (Otherwise, it's just a different kind of guessing game that attracts downvotes...)
@MichaelFerguson你试过重新编译吗?您问题中的代码仍然不完整,因此任何事情都是可能的(包括未定义的行为)。建议您编辑问题,添加完整的代码,以便我们可以自己编译和测试…(否则,这只是一种吸引反对票的不同类型的猜谜游戏……)
Ill put the whole file into the question
我会把整份文件都放在这个问题上
@MichaelFerguson I've just compiled and run the program. I guess the answer (always 7 because the random number generator is not initialised in your code). I get congratulations. Then I'm asked if I want to play again (Y or Q)... This is as expected.
@MichaelFerguson我刚刚编译并运行了程序。我猜答案(总是7,因为随机数生成器没有在您的代码中初始化)。我得到了祝贺。然后我被问到是否想再玩一次(Y或Q)……这不出所料。
Ok well thats good. Im not sure why mine says otherwise bc i have recomplied multiple times. i have have even tried restarting vscode but i get the same response every time
好的,那很好。我不知道为什么我的答案不是这样,我已经重复了很多次了。我甚至尝试重新启动vscode,但每次都得到相同的响应
我是一名优秀的程序员,十分优秀!