如果用户输入非数字字符,如何仅扫描整数并重复读取? [英] How to scanf only integer and repeat reading if the user enters non-numeric characters?
问题描述
这里有一些C代码试图阻止用户键入字符或小于0或大于23的整数.
Here is some C code trying simply to prevent the user from typing a character or an integer less than 0 or more than 23.
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
const char *input;
char *iPtr;
int count = 0;
int rows;
printf("Enter an integer: ");
scanf("%s", input);
rows = strtol(input, &iPtr, 0);
while( *iPtr != '\0') // Check if any character has been inserted
{
printf("Enter an integer between 1 and 23: ");
scanf("%s", input);
}
while(0 < rows && rows < 24) // check if the user input is within the boundaries
{
printf("Select an integer from 1 to 23: ");
scanf("%s", input);
}
while (count != rows)
{
/* Do some stuff */
}
return 0;
}
我完成了一半,将进行一次小小的俯卧撑.
I made it halfway through and a small push up will be appreciated.
推荐答案
使用 scanf(%d",& rows)
代替 scanf(%s",input)
这使您可以直接从stdin获取整数值,而无需转换为int.
This allow you to get direcly the integer value from stdin without need to convert to int.
如果用户输入的字符串包含非数字字符,则必须在下一个 scanf(%d",& rows)
之前清除标准输入.
If the user enter a string containing a non numeric characters then you have to clean your stdin before the next scanf("%d",&rows)
.
您的代码可能看起来像这样:
your code could look like this:
#include <stdio.h>
#include <stdlib.h>
int clean_stdin()
{
while (getchar()!='\n');
return 1;
}
int main(void)
{
int rows =0;
char c;
do
{
printf("\nEnter an integer from 1 to 23: ");
} while (((scanf("%d%c", &rows, &c)!=2 || c!='\n') && clean_stdin()) || rows<1 || rows>23);
return 0;
}
说明
1)
scanf("%d%c", &rows, &c)
这意味着期望用户输入一个整数,并在其附近输入一个非数字字符.
This means expecting from the user input an integer and close to it a non numeric character.
示例1: 如果用户输入 aaddk
,然后输入 ENTER
,则scanf将返回0.
Example1: If the user enter aaddk
and then ENTER
, the scanf will return 0. Nothing capted
Example2: :如果用户输入 45
,然后输入 ENTER
,则scanf将返回2(2个元素带上限).在这里,%d
为 45
设置了标题,而%c
为 \ n
Example2: If the user enter 45
and then ENTER
, the scanf will return 2 (2 elements are capted). Here %d
is capting 45
and %c
is capting \n
Example3: :如果用户输入 45aaadd
,然后输入 ENTER
,则scanf将返回2(2个元素带上限).在这里%d
为 45
设置了标题,而%c
为 a
Example3: If the user enter 45aaadd
and then ENTER
, the scanf will return 2 (2 elements are capted). Here %d
is capting 45
and %c
is capting a
2)
(scanf("%d%c", &rows, &c)!=2 || c!='\n')
在示例1中: 此条件为 TRUE
,因为scanf返回 0
(!= 2
)
In the example1: this condition is TRUE
because scanf return 0
(!=2
)
在示例2中: 此条件为 FALSE
,因为scanf返回 2
和 c =='\ n'
In the example2: this condition is FALSE
because scanf return 2
and c == '\n'
在示例3中: 此条件为 TRUE
,因为scanf返回 2
和 c =='a'(!='\ n')
In the example3: this condition is TRUE
because scanf return 2
and c == 'a' (!='\n')
3)
((scanf("%d%c", &rows, &c)!=2 || c!='\n') && clean_stdin())
clean_stdin()
始终为 TRUE
,因为该函数始终返回 1
clean_stdin()
is always TRUE
because the function return always 1
在示例1中: (scanf(%d%c",& rows& c)!= 2 || c!='\ n')
为 TRUE
,因此应检查&&
之后的条件,以便 clean_stdin()
将被执行,整个条件为 TRUE
In the example1: The (scanf("%d%c", &rows, &c)!=2 || c!='\n')
is TRUE
so the condition after the &&
should be checked so the clean_stdin()
will be executed and the whole condition is TRUE
在示例2中: (scanf(%d%c",& rows& c)!= 2 || c!='\ n')
为&&
之后的条件(因为其结果是整个条件 FALSE
),因此将不会执行 clean_stdin()
,整个条件为 FALSE
In the example2: The (scanf("%d%c", &rows, &c)!=2 || c!='\n')
is FALSE
so the condition after the &&
will not checked (because what ever its result is the whole condition will be FALSE
) so the clean_stdin()
will not be executed and the whole condition is FALSE
在示例3中: (scanf(%d%c",& rows& c)!= 2 || c!='\ n')
为 TRUE
,因此应检查&&
之后的条件,以便 clean_stdin()
将被执行,整个条件为 TRUE
In the example3: The (scanf("%d%c", &rows, &c)!=2 || c!='\n')
is TRUE
so the condition after the &&
should be checked so the clean_stdin()
will be executed and the whole condition is TRUE
因此,您可以指出,只有当用户输入包含非数字字符的字符串时,才会执行 clean_stdin()
.
So you can remark that clean_stdin()
will be executed only if the user enter a string containing non numeric character.
此条件((scanf(%d%c",& rows& c)!= 2 || c!='\ n')&& clean_stdin())
仅在用户输入 integer
且没有其他输入
And this condition ((scanf("%d%c", &rows, &c)!=2 || c!='\n') && clean_stdin())
will return FALSE
only if the user enter an integer
and nothing else
如果条件((scanf(%d%c",& rows& c)!= 2 || c!='\ n')&& clean_stdin())
是 FALSE
,而 integer
在 1
和 23
之间,然后是 while
循环将中断,否则 while
循环将继续
And if the condition ((scanf("%d%c", &rows, &c)!=2 || c!='\n') && clean_stdin())
is FALSE
and the integer
is between and 1
and 23
then the while
loop will break else the while
loop will continue
这篇关于如果用户输入非数字字符,如何仅扫描整数并重复读取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!