【发布时间】:2018-09-13 14:51:32
【问题描述】:
我在用 C 语言进行数字猜谜游戏时遇到问题。在运行程序时,当我将 char 元素放入其中时,它会显示返回函数。有人可以帮我弄这个吗?另外,如何改进我的代码以使其可行?我真的被这个问题困住了。
代码如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int main()
{
char s, n, q;
int b;
bool (1=true), (0=false);
int secret;
secret = rand();
int guess;
int seed;
srand(seed);
printf("Welcome to the guessing game!\n");
do{
printf("Menu: (s) to start a new game, (n) to set a new range, or (q) to quit:\n");
scanf("%s, %s, %s", s, n, q);
if((s==1))
{
printf("The secret number is Between 0 AND rand(). Guess\n");
scanf("%s", b);
}
else if((n ==1))
{
printf("Enter a new MAXIMUM\n");
scanf("%s", rand());
if(( s ==1))
{
printf("The secret number is Between 0 AND rand(). Guess\n");
scanf("%s", b);
printf("The secret number is between 0 and rand() Guess:");
scanf("%s", b);
if(guess = rand()){
printf("Congratulations you won, You took %d guesses!", b);
break;
}
else if(guess > rand())
{
printf("Too High, Guess again:");
}
else if(guess < rand()){
printf("Too Low, Guess Again:");
}
else{
printf("This number out of the number set!");
}
}
}
else{
printf("Unrecognized command");
}
}while(q == 1);
printf("You quited the game");
return 0;
}
【问题讨论】:
-
您的
scanf有很多错误。例如scanf("%s", b);应该是scanf("%d", &b);我建议你先阅读有关 scanf 文档。 -
这将
scanf("%s", rand());导致UB。 -
你用的是什么编译器?
-
您希望用户在您的菜单中输入什么选项?每个选项一个字符还是一个是/否字符串?
scanf("%s, %s, %s", s, n, q);您应该阅读scanf的联机帮助页并再次考虑格式字符串。还有关于参数。 -
重要提示:第一件事!您需要掌握
scanf的用法,然后才能在更大的上下文中使用它。只有在您能够获得用户输入后,您才能尝试处理不同的菜单选项。要验证您的输入是否正确,您可能需要阅读How to debug small programs
标签: c