C - 读取用户输入
我有一个需要用户输入的程序,用户输入数字 1-8 来确定如何对某些数据进行排序,但如果用户只是按 Enter 键,则会执行不同的功能。我大致了解了如何做到这一点,并且我认为我所做的一切都可以很好地工作,但是当用户按下回车键时我遇到了一些问题。目前我的代码如下所示:
//User input needed for sorting.
fputs("Enter an option (1-8 or Return): ", stdout);
fflush(stdout);
fgets(input, sizeof input, stdin);
printf("%s entered\n", input); //DEBUGGING PURPOSES
//If no option was entered:
if(input == "\n")
{
printf("Performing alternate function.");
}
//An option was entered.
else
{
//Convert input string to an integer value to compare in switch statment.
sscanf(input, "%d", &option);
//Determine how data will be sorted based on option entered.
switch(option)
{
case 1:
printf("Option 1.\n");
break;
case 2:
printf("Option 2.\n");
break;
case 3:
printf("Option 3.\n");
break;
case 4:
printf("Option 4.\n");
break;
case 5:
printf("Option 5.\n");
break;
case 6:
printf("Option 6.\n");
break;
case 7:
printf("Option 7.\n");
break;
case 8:
printf("Option 8.\n");
break;
default:
printf("Error! Invalid option selected!\n");
break;
}
}
现在我已经更改了 if 语句以尝试 input == ""、input == " " 和 input == "\n" 但这些似乎都不起作用。任何建议将不胜感激。目前据我所知,最初的 if 语句失败,代码跳转到 else 部分,然后打印默认情况。
需要明确的是,我为此代码声明的变量如下:
char input[2]; //Used to read user input.
int option = 0; //Convert user input to an integer (Used in switch statement).
I have a program where user input is required, a user types in a number 1-8 to determine how to sort some data, but if the user just hits enter a different function is performed. I get the general idea of how to do this and I thought what I had would work just fine but I'm having some issues when it comes to when the user just hits the enter key. Currently my code looks as follows:
//User input needed for sorting.
fputs("Enter an option (1-8 or Return): ", stdout);
fflush(stdout);
fgets(input, sizeof input, stdin);
printf("%s entered\n", input); //DEBUGGING PURPOSES
//If no option was entered:
if(input == "\n")
{
printf("Performing alternate function.");
}
//An option was entered.
else
{
//Convert input string to an integer value to compare in switch statment.
sscanf(input, "%d", &option);
//Determine how data will be sorted based on option entered.
switch(option)
{
case 1:
printf("Option 1.\n");
break;
case 2:
printf("Option 2.\n");
break;
case 3:
printf("Option 3.\n");
break;
case 4:
printf("Option 4.\n");
break;
case 5:
printf("Option 5.\n");
break;
case 6:
printf("Option 6.\n");
break;
case 7:
printf("Option 7.\n");
break;
case 8:
printf("Option 8.\n");
break;
default:
printf("Error! Invalid option selected!\n");
break;
}
}
Now I've changed the if statement to try input == "", input == " ", and input == "\n" but none of these seems to work. Any advice would be greatly appreciated. Currently from what I can see, the initial if statement fails and the code jumps to the else portion and then prints the default case.
Just to be clear the variables I declared for this code are as follows:
char input[2]; //Used to read user input.
int option = 0; //Convert user input to an integer (Used in switch statement).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
问题在于如何进行字符串比较(
if (input == "\n")
)。 C 没有“原生”字符串类型,因此要比较字符串,您需要使用strcmp()
而不是==
。或者,您可以只与输入的第一个字符进行比较:if (input[0] == '\n') ...
。由于您随后比较的是字符而不是字符串,因此比较不需要函数。The problem is in how you're doing the string comparison (
if (input == "\n")
). C doesn't have a "native" string type, so to compare strings, you need to usestrcmp()
instead of==
. Alternatively, you could just compare to the first character of the input:if (input[0] == '\n') ...
. Since you're then comparing char's instead of strings, the comparison doesn't require a function.尝试:
在顶部和
适当的位置 if 你的
if ( input == ... )
基本上,你必须使用 C 中的字符串比较函数,不能使用比较运算符。
Try:
at the top and
in place if your
if ( input == ... )
Basically, you have to use string comparison functions in C, you can't use comparison operators.
尝试:
input[0] == '\n'
(或 *input == '\n')
Try:
input[0] == '\n'
(or *input == '\n')
您需要使用单引号而不是双引号
将输入地址与字符串“\n”的地址进行比较,
您要做的是比较输入缓冲区的第一个字符
像这样的字符文字 \n
注意在 '\n' 周围使用单引号
You need to use single quotes rather than double quotes
compares the input address to the address of the string "\n",
What you want to do is to compare the first character of the input buffer
to the character literal \n like this
Note the use of single quotes around '\n'
您需要从
sscanf
捕获返回代码,它会告诉您有多少字段被“分配”,其中在“Enter”键情况下,返回代码为 0edit:
比较字符串时应该使用strcmp,而不是运算符“==”。
You need to capture return code from
sscanf
, it will tell you how many of the field are "assigned", which in "Enter" key case, return code of 0edit:
you should use
strcmp
when comparing string, not the operator "==".问题在于字符串,您正在比较指针,即内存地址。由于输入和
"\n"
不是完全相同的内存,因此它总是失败(我假设输入是char *
)。由于您正在寻找单个字符,因此您可以取消引用input
并使用单引号而不是双引号与字符进行比较。应该按你的意图工作。
The issue is with strings, you are comparing pointers, i.e. memory addresses. Since the input and
"\n"
aren't the same exact memory, it always fails (I assume input is achar *
). Since you're looking for a single character, you can instead dereferenceinput
and compare to a char using single quotes instead of double.Should work as you intend.