我尝试制作一个类似游戏的程序,检查通过键盘输入的字符(在三秒的时间限制内)是否与随机生成的输出相同或没有。我使用的是TC ++(最新版)。
第二次,无论我输入什么字符(正确或错误),程序都说输入的字符是错误的。它没有转到scanf(也许),因为它打印" Q"然后" OOPS"线
- - - - - - - - - - - - - - - - - - 码 - - - - - - - ------------------------
#include <stdio.h>
#include<conio.h>
#include<time.h>
void main()
{
int count , sec;
char output, input;
clock_t start, end;
clrscr();
restart:
count = 0;
next:
//Generating random letters and symbols: ASCII characters from A-z
//Set output to A
output = 'A';
//Adding a value from 1-57 to get a random ASCII character from A-z
output += rand() % 57;
printf("%c\n",output);
start = clock();
scanf("%c",&input);
end = clock() - start;
sec = end / CLK_TCK;
//Time limit check (Time in seconds)
if(sec >= 3)
{
printf("\nYou ran out of time.");
goto endgame;
}
if(input == output)
{
count++;
goto next;
}
else
{
printf("\nOOPS! You are wrong\n\nYour Score is %d",count*10);
goto endgame;
}
endgame:
printf("\nDo you want to restart?(Y/N)");
scanf("%c",&input);
if(input == 'y' || input == 'Y')
{
goto restart;
}
getch();
}
OUTPUT:
E
*I enter E*
Q
*CONSOLE DOESN'T WAIT FOR ME TO ENTER ANYTHING*
OOPS! You are wrong.
Your score is 10.
Do you want to restart(Y/N)?
答案 0 :(得分:1)
感谢@xing和@jxh,我得到了我的愚蠢错误.. 控制台太过显着“\ n”,scanf(“%c”); %c之前的空间做了这个伎俩, 感谢大家!