[英]User input error when asking for a digit (switch case) C
For this program i'm asking the user to enter a digit and it returns the digit you've entered but in another language.对于这个程序,我要求用户输入一个数字,它会以另一种语言返回您输入的数字。 I'm using a switch case to utilize this and for user input errors when typing a letter instead of the digits 0-9 it defaults to invalid user input, however, when I enter 10, 11, or 12 which is outside the range it prints out isa which is number 1 which shouldn't be happening.
我正在使用 switch case 来利用它,并且对于输入字母而不是数字 0-9 时的用户输入错误,它默认为无效的用户输入,但是,当我输入超出范围的 10、11 或 12 时打印出 isa,这是不应该发生的数字 1。 How do I make it so that if the user enters an invalid number outside the 0-9 range it also says invalid input?
我该如何做到这一点,如果用户输入了 0-9 范围之外的无效数字,它也会说输入无效?
Here's the code这是代码
#include <stdio.h>
int main(void) {
printf("This program will display the Filipino(Tagalog) word for a digit of your choice.\n");
printf("Enter a digit 0-9: ");
int digit = getchar();
switch(digit) {
case '0' :
printf("wala\n");
break;
case '1' :
printf("isá\n");
break;
case '2' :
printf("dalawá\n");
break;
case '3' :
printf("tatló\n");
break;
case '4' :
printf("ápat\n");
break;
case '5' :
printf("limá\n");
break;
case '6' :
printf("anim\n");
break;
case '7' :
printf("pitó\n");
break;
case '8' :
printf("waló\n");
break;
case '9' :
printf("siyám\n");
break;
default :
printf("Invalid input\n");
}
return 0;
}
Thank you!谢谢!
It's because getchar()
only retrieves one character from standard input.这是因为
getchar()
仅从标准输入中检索一个字符。 It doesn't read in an entire integer. When you pass 10
, 11
or 12
, your code is actually reading in 1
and leaving the second digit in standard input.它不会读入整个 integer。当您传递
10
、 11
或12
时,您的代码实际上是读入1
并将第二个数字留在标准输入中。
You have at least a couple of options:你至少有几个选择:
scanf()
instead of getchar()
.scanf()
而不是getchar()
从标准输入读取integer 。 Take a look at How can I get an int from stdio in C?fgets()
and check to make sure it only contains 1 character, then retrieve that first character to use in the switch.fgets()
读取整行输入并检查以确保它只包含一个字符,然后检索第一个字符以在开关中使用。 This is in keeping with your intent of wanting to switch on digits , and also additionally will not fail if you pass a non-integer input to stdin, whereas if using scanf()
, you need to check the return code to make sure malformed input was not given.scanf()
,您需要检查返回码以确保输入格式错误没有给出。 Echoing what the previous answerer said, getchar
reads only one character from the standard input stream. Once it gets a character, it assigns it to digit
.与前面回答者所说的相呼应,
getchar
只从标准输入 stream 中读取一个字符。一旦它获得一个字符,它就会将它分配给digit
。 getchar
and friends return a value of type int
in order to be able to return the EOF
value (which is out of char
range) so you know when your stream ends. getchar
和朋友返回一个int
类型的值,以便能够返回EOF
值(超出char
范围),以便您知道 stream 何时结束。 So really digit
is a character, just with int
type for that extra little bit of range.所以真正的
digit
是一个字符,只是在额外的一点范围内使用int
类型。
I'll add that getchar
will leave behind any characters left behind on the input stream that were not used.我要补充一点,
getchar
将留下输入 stream 上未使用的任何字符。 If you're using this program on the command line and press 3
followed by the enter key in order to get the output tatló
, the enter key will leave a '\n'
character (in Linux--I'm honestly not sure if it will be '\n'
or '\r'
in Windows) on the standard input stream, which the first getchar
does not consume.如果您在命令行上使用此程序并按
3
然后按回车键以获得 output tatló
,回车键将留下一个'\n'
字符(在 Linux 中——老实说我不确定是否它将是标准输入 stream 上的'\n'
或'\r'
在 Windows 中),第一个getchar
不会使用它。 So you could check this value with another call to getchar
.因此,您可以通过再次调用
getchar
检查此值。 Something like this:像这样:
...
printf("Enter a digit 0-9: ");
int digit = getchar();
if(getchar() != '\n') {
/*The user typed something after the initial digit, do something, like maybe assigning an invalid value to `digit`*/
digit = 'a';
}
switch(digit) {
...
The second call to getchar
will return '\n'
if your user entered just a single character;如果您的用户只输入了一个字符,第二次调用
getchar
将返回'\n'
; otherwise it will return the second character that the user entered.否则它将返回用户输入的第二个字符。
This is a suitable solution to parsing a number from user input if you never intend to parse multi-digit numbers.如果您从不打算解析多位数字,那么这是解析用户输入的数字的合适解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.