简单的C程序returns无输出
Simple C program returns no output
我写了一个简单的 C 程序,它接受用户输入并将其存储在 dataofUser 变量中,然后它允许用户通过说是或否来选择是否要显示他们输入的数据,这是通过 fgets 完成的和一个 if 语句,它检查 userChoice 变量是否在其中存储了是或否取决于它是或否它将显示数据或显示 "no output!",当我 运行 程序时我没有输出我在下面输入的数据中,您可以看到控制台上显示的内容:
Please enter your input: this is a random input
Your data has been entered please enter Yes or No to display it: yes
Process returned 0 (0x0) execution time : 4.829 s
Press ENTER to continue.
这是构建消息日志和各种错误消息
||=== Build: Debug in randopoint2 (compiler: GNU GCC Compiler) ===|
/home/Documents/randopoint2/randopoint2/main.c||In function ‘main’:|
/home/Documents/randopoint2/randopoint2/main.c|17|warning: comparison between pointer and integer|
/home/Documents/randopoint2/randopoint2/main.c|17|warning: comparison with string literal results in unspecified behavior [-Waddress]|
/home/Documents/randopoint2/randopoint2/main.c|21|warning: comparison between pointer and integer|
/home/Documents/randopoint2/randopoint2/main.c|21|warning: comparison with string literal results in unspecified behavior [-Waddress]|
||=== Build finished: 0 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|
||=== Run: Debug in randopoint2 (compiler: GNU GCC Compiler) ===|
#include <stdio.h>
#include <stdlib.h>
int main()
{
char dataOfUser[50];
char userChoice[50];
printf("Please enter your input: ");
fgets(dataOfUser, 50, stdin);
printf("Your data has been entered please enter Yes or No to dipslay it: ");
fgets(userChoice, 50, stdin);
if(userChoice[50] == "yes")
{
printf("Your notes are: %s", dataOfUser);
}
else if(userChoice[50] == "no")
{
printf("no output!");
}
return 0;
}
问题:
if(userChoice[50] == "yes")
在 C 语言中,这不是比较字符串的方式。
此外,访问 userChoice[50]
是未定义的行为,因为您正试图越界访问数组。
else if(userChoice[50] == "no")
也一样
另请注意,当您按回车键结束输入时,换行符 \n
存储在 userChoice
.
解法:
添加#include <string.h>
得到strcmp
函数。
将if(userChoice[50] == "yes")
改为if (strcmp(userChoice, "yes\n") == 0)
将else if(userChoice[50] == "no")
更改为else if (strcmp(userChoice, "no\n") == 0)
旁白:
printf("Your data has been entered please enter Yes or No to dipslay it: ");
您要求用户输入 "Yes" 或 "No",但将其与代码中的 "yes" 和 "no" 进行比较。
我写了一个简单的 C 程序,它接受用户输入并将其存储在 dataofUser 变量中,然后它允许用户通过说是或否来选择是否要显示他们输入的数据,这是通过 fgets 完成的和一个 if 语句,它检查 userChoice 变量是否在其中存储了是或否取决于它是或否它将显示数据或显示 "no output!",当我 运行 程序时我没有输出我在下面输入的数据中,您可以看到控制台上显示的内容:
Please enter your input: this is a random input
Your data has been entered please enter Yes or No to display it: yes
Process returned 0 (0x0) execution time : 4.829 s
Press ENTER to continue.
这是构建消息日志和各种错误消息
||=== Build: Debug in randopoint2 (compiler: GNU GCC Compiler) ===|
/home/Documents/randopoint2/randopoint2/main.c||In function ‘main’:|
/home/Documents/randopoint2/randopoint2/main.c|17|warning: comparison between pointer and integer|
/home/Documents/randopoint2/randopoint2/main.c|17|warning: comparison with string literal results in unspecified behavior [-Waddress]|
/home/Documents/randopoint2/randopoint2/main.c|21|warning: comparison between pointer and integer|
/home/Documents/randopoint2/randopoint2/main.c|21|warning: comparison with string literal results in unspecified behavior [-Waddress]|
||=== Build finished: 0 error(s), 4 warning(s) (0 minute(s), 0 second(s)) ===|
||=== Run: Debug in randopoint2 (compiler: GNU GCC Compiler) ===|
#include <stdio.h>
#include <stdlib.h>
int main()
{
char dataOfUser[50];
char userChoice[50];
printf("Please enter your input: ");
fgets(dataOfUser, 50, stdin);
printf("Your data has been entered please enter Yes or No to dipslay it: ");
fgets(userChoice, 50, stdin);
if(userChoice[50] == "yes")
{
printf("Your notes are: %s", dataOfUser);
}
else if(userChoice[50] == "no")
{
printf("no output!");
}
return 0;
}
问题:
if(userChoice[50] == "yes")
在 C 语言中,这不是比较字符串的方式。
此外,访问 userChoice[50]
是未定义的行为,因为您正试图越界访问数组。
else if(userChoice[50] == "no")
另请注意,当您按回车键结束输入时,换行符 \n
存储在 userChoice
.
解法:
添加
#include <string.h>
得到strcmp
函数。将
if(userChoice[50] == "yes")
改为if (strcmp(userChoice, "yes\n") == 0)
将
else if(userChoice[50] == "no")
更改为else if (strcmp(userChoice, "no\n") == 0)
旁白:
printf("Your data has been entered please enter Yes or No to dipslay it: ");
您要求用户输入 "Yes" 或 "No",但将其与代码中的 "yes" 和 "no" 进行比较。