如果用户输入非数字字符,如何仅扫描整数并重复阅读? [英] How to scanf only integer and repeat reading if the user enters non-numeric characters?
问题描述
这里有一些 C 代码只是为了防止用户输入字符或小于 0 或大于 23 的整数.
#include #include int main(void){常量字符*输入;字符 * iPtr;整数计数 = 0;整数行;printf("请输入一个整数:");scanf("%s", 输入);行 = strtol(输入, & iPtr, 0);while( *iPtr != ' ')//检查是否插入了任何字符{printf("请输入一个 1 到 23 之间的整数:");scanf("%s", 输入);}while(0 < rows && rows < 24)//检查用户输入是否在边界内{printf("从1到23中选择一个整数:");scanf("%s", 输入);}而(计数!=行){/* 做一些事情 */}返回0;}
我已经完成了一半,将不胜感激.
使用 scanf("%d",&rows)
而不是 scanf("%s",input)
这允许您直接从 stdin 获取整数值,而无需转换为 int.
如果用户输入包含非数字字符的字符串,那么您必须在下一个 scanf("%d",&rows)
之前清理标准输入.
您的代码可能如下所示:
#include #include int clean_stdin(){而 (getchar()!='
');返回 1;}int main(void){整数行 =0;字符 c;做{printf("
请输入一个 1 到 23 之间的整数:");} while (((scanf("%d%c", &rows, &c)!=2 || c!='
') && clean_stdin()) || rows<1 ||行>23);返回0;}
说明
1)
scanf("%d%c", &rows, &c)
这意味着期望用户输入一个整数并在其附近输入一个非数字字符.
Example1: 如果用户输入 aaddk
然后ENTER
,scanf 将返回 0.没有被捕获
Example2:如果用户输入45
然后ENTER
,scanf将返回2(2个元素被俘).这里 %d
正在捕获 45
而 %c
正在捕获
示例 3: 如果用户输入 45aaadd
然后 ENTER
,scanf 将返回 2(2 个元素被俘).这里 %d
正在捕获 45
而 %c
正在捕获 a
2)
(scanf("%d%c", &rows, &c)!=2 || c!='
')
在示例 1 中: 这个条件是 TRUE
因为 scanf 返回 0
(!=2
)
在示例 2 中: 这个条件是 FALSE
因为 scanf 返回 2
和 c =='
'
在示例 3 中: 这个条件是 TRUE
因为 scanf 返回 2
和 c =='a' (!='
')
3)
((scanf("%d%c", &rows, &c)!=2 || c!='
') && clean_stdin())
clean_stdin()
总是 TRUE
因为函数返回总是 1
在示例 1 中: (scanf("%d%c", &rows, &c)!=2 || c!='
')
是 TRUE
所以应该检查 &&
之后的条件,以便 clean_stdin()
将被执行并且整个条件为 TRUE
在示例 2 中: (scanf("%d%c", &rows, &c)!=2 || c!='
')
是 FALSE
所以 &&
之后的条件不会被检查(因为它的结果是整个条件将是FALSE
) 所以 clean_stdin()
不会被执行,整个条件是 FALSE
在示例 3 中: (scanf("%d%c", &rows, &c)!=2 || c!='
')
是 TRUE
所以应该检查 &&
之后的条件,以便 clean_stdin()
将被执行并且整个条件为 TRUE
所以你可以注意到 clean_stdin()
只有当用户输入包含非数字字符的字符串时才会执行.
还有这个条件 ((scanf("%d%c", &rows, &c)!=2 || c!='
') && clean_stdin())
只会在用户输入 integer
而没有其他内容时返回 FALSE
如果条件 ((scanf("%d%c", &rows, &c)!=2 || c!='
') && clean_stdin())
是 FALSE
并且 integer
介于 1
和 23
之间,然后 while
循环将中断,否则 while
循环将继续
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 != ' ') // 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.
Use scanf("%d",&rows)
instead of scanf("%s",input)
This allow you to get direcly the integer value from stdin without need to convert to int.
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()!='
');
return 1;
}
int main(void)
{
int rows =0;
char c;
do
{
printf("
Enter an integer from 1 to 23: ");
} while (((scanf("%d%c", &rows, &c)!=2 || c!='
') && clean_stdin()) || rows<1 || rows>23);
return 0;
}
Explanation
1)
scanf("%d%c", &rows, &c)
This means expecting from the user input an integer and close to it a non numeric character.
Example1: If the user enter aaddk
and then ENTER
, the scanf will return 0. Nothing capted
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
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!='
')
In the example1: this condition is TRUE
because scanf return 0
(!=2
)
In the example2: this condition is FALSE
because scanf return 2
and c == '
'
In the example3: this condition is TRUE
because scanf return 2
and c == 'a' (!='
')
3)
((scanf("%d%c", &rows, &c)!=2 || c!='
') && clean_stdin())
clean_stdin()
is always TRUE
because the function return always 1
In the example1: The (scanf("%d%c", &rows, &c)!=2 || c!='
')
is TRUE
so the condition after the &&
should be checked so the clean_stdin()
will be executed and the whole condition is TRUE
In the example2: The (scanf("%d%c", &rows, &c)!=2 || c!='
')
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
In the example3: The (scanf("%d%c", &rows, &c)!=2 || c!='
')
is TRUE
so the condition after the &&
should be checked so the clean_stdin()
will be executed and the whole condition is TRUE
So you can remark that clean_stdin()
will be executed only if the user enter a string containing non numeric character.
And this condition ((scanf("%d%c", &rows, &c)!=2 || c!='
') && clean_stdin())
will return FALSE
only if the user enter an integer
and nothing else
And if the condition ((scanf("%d%c", &rows, &c)!=2 || c!='
') && 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屋!