我的代码存在这个问题。事情是我需要比较两个字符n
和a[p]
,但结果总是负面的。这是我的任务的一个小测验计划。 q[]
是一组问题,a[]
是一组答案。玩家输入t
或f
为true或false,但if
似乎不起作用,因为它总是打印出'你输了!'。 (即使条件是真的)。C语言中的char比较if语句
char questions(){
const char *q [100];
q[0]= "Centipedes always have 100 feet."; //f
q[1] = "Marie Curie’s husband was called Pierre."; //t
[...]
q[99] = "";
const char *a[100];
a[0] = "f";
a[1] = "t";
[...]
a[99] = "";
char n;
int p, i;
for (i = 0; i<=7; ++i)
{
srand(time(NULL));
p = (rand() % 18);
printf("\n");
printf(q[p]);
printf("\n");
fflush(stdin);
scanf("%c", &n);
if (n == a[p]){
printf("Correct answer!\n");
}
else
{
printf("You lose!");
break;
}
}
'如果(N ==一个[P]){' - >'如果(N == *一个[P]){' – BLUEPIXY 2014-10-19 19:06:17
你应注意你的警告。 – 2014-10-19 19:10:25
**注意:**'fflush(stdin);'** **不**从'stdin'移除多余的字符。你需要手动移除任何与'int flush = 0;'类似的无关字符,然后在从stdin读取之前/之后第二次包含'do {flush = getchar(); } while(flush!='\ n');'。这将完成你是**,而不是**用'fflush(stdin);'实现。 – 2014-10-19 19:10:43