程序无法识别 if-else 语句中的字符输入
Program not recognizing character inputs in if-else statements
我调试程序发现,虽然你输入了一个选项,比如Y/y/n/N
或H/h/l/L
,但是if函数无法识别输入,它会跳过。感谢您的任何建议! RNG 运行正常,对我来说似乎一切都很好。
#include <stdio.h>
#include <time.h>
int main(){
char startAnswer[1];
char gameAnswer[1];
int i;
int r;
int t;
int compare;
srand(time(NULL));
printf("Do you want to play a game? Y/N. \n");
scanf(" %c", &startAnswer);
start:
if (startAnswer == 'y' || 'Y'){
goto game;
}
else if (startAnswer == 'n' || 'N'){
printf("Game will now exit.\n");
}
else {
printf("You entered an invalid answer, try again.\n");
goto start;
}
game:
printf("There will be ten numbers calculated.\n");
printf("Try to guess what the numbers are before\n");
printf("they are displayed. One at a time.\n\n");
for(i = 0; i < 12; i++){
randGen:
r = ( rand() % 9 ) + 1;
t = ( rand() % 9 ) + 1;
if(t == r){
goto randGen;
}
printf("Is the next number higher or lower than %d\n\n" , r);
scanf(" %c" , &gameAnswer);
//ignore (r > t) ? (compare = 0) : (compare = 1);
if (r > t){
compare = 1;
}
else{
compare = 0;
}
if (gameAnswer == ('H' || 'h')){
if (compare == 1){
printf("You win this round!");
goto randGen;
}
}
else if (gameAnswer == ('L' || 'l')){
if (compare == 0){
printf("You win this round!");
goto randGen;
}
}
else if (gameAnswer != ('l' || 'L' || 'h' || 'H')){
if (compare == 0){
printf("Please Enter H/L for higher/lower answer.\n\n");
goto randGen;
}
}
else{
printf("You lost this round.");
goto randGen;
}
r = t;
}
}
您是否在阅读选项之前使用 fflush(stdin)
?这将清理您的缓冲区。
在你的代码中
if (gameAnswer == ('H' || 'h'))
并不像你想象的那样。将其更改为
if ((gameAnswer[0] == 'H') || (gameAnswer[0] == 'h')) //gameAnswer is an array
类似地,对于所有其他事件。
此外,
scanf(" %c", &startAnswer);
应该是
scanf(" %c", &startAnswer[0]);
同样
注:
如果您不需要 array
,不要使用。坚持单一变量。
main()
的推荐签名是int main(void)
.
只是一个建议,请尽量避免goto
语句。这不是一个好的做法。尝试编写和使用函数。
if (startAnswer == 'y' || 'Y'){
没有。这不是如何链接条件。 You are translating broken English into C++.
有很多问题:
其中之一是:
char startAnswer[1];
char gameAnswer[1];
应该是
char startAnswer;
char gameAnswer;
你的编译器肯定在这里发出了警告。
您的代码有 2 个问题:
if
条件,比如这个:if (gameAnswer == ('H' || 'h'))
,本来应该是:if (gameAnswer == 'H' || gameAnswer=='h')
chars
:char startAnswer[1]; char gameAnswer[1];
要么这样做:char startAnswer; char gameAnswer;
要么:在代码中使用 gameAnswer[0]
而不是 gameAnswer
。
最后,对于 srand
和 rand
,您需要包含 stdlib
库,只需添加此行:#include<stdlib.h>
。 http://www.cplusplus.com/reference/cstdlib/srand/
我调试程序发现,虽然你输入了一个选项,比如Y/y/n/N
或H/h/l/L
,但是if函数无法识别输入,它会跳过。感谢您的任何建议! RNG 运行正常,对我来说似乎一切都很好。
#include <stdio.h>
#include <time.h>
int main(){
char startAnswer[1];
char gameAnswer[1];
int i;
int r;
int t;
int compare;
srand(time(NULL));
printf("Do you want to play a game? Y/N. \n");
scanf(" %c", &startAnswer);
start:
if (startAnswer == 'y' || 'Y'){
goto game;
}
else if (startAnswer == 'n' || 'N'){
printf("Game will now exit.\n");
}
else {
printf("You entered an invalid answer, try again.\n");
goto start;
}
game:
printf("There will be ten numbers calculated.\n");
printf("Try to guess what the numbers are before\n");
printf("they are displayed. One at a time.\n\n");
for(i = 0; i < 12; i++){
randGen:
r = ( rand() % 9 ) + 1;
t = ( rand() % 9 ) + 1;
if(t == r){
goto randGen;
}
printf("Is the next number higher or lower than %d\n\n" , r);
scanf(" %c" , &gameAnswer);
//ignore (r > t) ? (compare = 0) : (compare = 1);
if (r > t){
compare = 1;
}
else{
compare = 0;
}
if (gameAnswer == ('H' || 'h')){
if (compare == 1){
printf("You win this round!");
goto randGen;
}
}
else if (gameAnswer == ('L' || 'l')){
if (compare == 0){
printf("You win this round!");
goto randGen;
}
}
else if (gameAnswer != ('l' || 'L' || 'h' || 'H')){
if (compare == 0){
printf("Please Enter H/L for higher/lower answer.\n\n");
goto randGen;
}
}
else{
printf("You lost this round.");
goto randGen;
}
r = t;
}
}
您是否在阅读选项之前使用 fflush(stdin)
?这将清理您的缓冲区。
在你的代码中
if (gameAnswer == ('H' || 'h'))
并不像你想象的那样。将其更改为
if ((gameAnswer[0] == 'H') || (gameAnswer[0] == 'h')) //gameAnswer is an array
类似地,对于所有其他事件。
此外,
scanf(" %c", &startAnswer);
应该是
scanf(" %c", &startAnswer[0]);
同样
注:
如果您不需要
array
,不要使用。坚持单一变量。main()
的推荐签名是int main(void)
.只是一个建议,请尽量避免
goto
语句。这不是一个好的做法。尝试编写和使用函数。
if (startAnswer == 'y' || 'Y'){
没有。这不是如何链接条件。 You are translating broken English into C++.
有很多问题:
其中之一是:
char startAnswer[1];
char gameAnswer[1];
应该是
char startAnswer;
char gameAnswer;
你的编译器肯定在这里发出了警告。
您的代码有 2 个问题:
if
条件,比如这个:if (gameAnswer == ('H' || 'h'))
,本来应该是:if (gameAnswer == 'H' || gameAnswer=='h')
chars
:char startAnswer[1]; char gameAnswer[1];
要么这样做:char startAnswer; char gameAnswer;
要么:在代码中使用gameAnswer[0]
而不是gameAnswer
。
最后,对于 srand
和 rand
,您需要包含 stdlib
库,只需添加此行:#include<stdlib.h>
。 http://www.cplusplus.com/reference/cstdlib/srand/