我有以下代码:
#include<stdio.h>
#include "commonf.h" //So, this is the way you include from a directory?
void main(){
println("Welcome to Number Guesser v 0.1 ");
println("Think of a number between 0 and 100 (and please, make it an integer!)");
println("Legend: Y=Yes B=Bigger than that S= Smaller than that");
int guessed=0;
float curnum=100.0;
char cursign='?';
while(cursign!='y'){
if(cursign=='?' || cursign=='s'){
curnum=curnum/2;
}
else if(cursign=='b'){
curnum=curnum+curnum/2;
}
else{
printf("You are wrong. Stop it. %c . TEESST",cursign);
}
char askstring[4096];
sprintf(askstring,"%s%f%s","Is your number ",curnum," ? (y/b/s)");
println(askstring);
scanf("%c",&cursign); //Scanf has to expect a new line for some reason.
}
}
(我都贴了,因为我是菜鸟)
如果代码看起来像这样,循环将在每个用户输入时执行两次,一次使用 cursign= 对用户输入的任何内容,一次使用它等于 \n。
如果我将 scanf 行更改为
scanf("%c\n",&cursign);
它要求第一个输入两次,然后作为一个魅力。有什么问题,我该怎么办?