如何只整数scanf函数重复,如果用户输入非数字字符阅读? [英] How to scanf only integer and repeat reading if the user enter non numeric characters?

查看:162
本文介绍了如何只整数scanf函数重复,如果用户输入非数字字符阅读?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是一个小青年初学者与C code题从输入一个字符或整数只是想prevent用户小于0或超过23。

Here is a little young tyro's problem with 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;  
}

我成功了一半和一个小的推升将AP preciated。

I made it halfway through and a small push up will be appreciated.

推荐答案

使用 scanf函数(%d个,&安培;行)而不是 scanf函数(%S,输入)

这允许你从标准输入获得direcly整数值,而不需要转换为int。

This allow you to get direcly the integer value from stdin without need to convert to int.

如果用户输入包含非数字字符,那么你有一个 scanf函数之前清理你的标准输入一个字符串(%d个,&安培;行)。

If the user enter a string containing a non numeric characters then you have to clean your stdin before the next scanf("%d",&rows).

您code可能是这样的:

your code could look like this:

#include <stdio.h>  
#include <stdlib.h> 

int clean_stdin()
{
    while (getchar()!='\n');
    return 1;
}

int main(void)  
{  
    int count = 0;  
    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没什么capted

Example1: If the user enter aaddk and then ENTER, the scanf will return 0. Nothing capted

例2:如果用户输入 45 然后 ENTER ,scanf函数将返回2(2元素capted)。在这里,%d个是capting 45 %C 是capting \\ 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

例3:如果用户输入 45aaadd 然后 ENTER ,scanf函数将返回2(2元素capted)。在这里,%d个是capting 45 %C 是capting 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 ç=='\\ n'

In the example2: this condition is FALSE because scanf return 2 and c == '\n'

在例3:这个条件 TRUE 因为scanf函数返回 2 ç=='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,&安培;行,和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,&安培;行,和C)= 2 | !| c ='\\ n') FALSE 等以后条件的&放大器;&安培; 将不会检查(因为曾经它的结果是什么,整个状况会 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,&安培;行,和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,&安培;行,和放大器;!C)= 2 || C ='\\ n')及!&安培; clean_stdin( ))将返回 FALSE 仅当用户输入整数,没有别的

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,&安培;行,和放大器;!C)= 2 || C ='\\ n')及和放大器; clean_stdin ()) FALSE 整数之间和 1 23 那么,而循环将打破别人的循环将继续

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

这篇关于如何只整数scanf函数重复,如果用户输入非数字字符阅读?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆