1

我正在学习 C,现在我想从键盘获取输入(2 个数字范围 [0,99] 并比较它们)。我发现了很多获取输入的选项:

  • 扫描函数()
  • 获取字符()
  • fgets()
  • 获取()

我尝试了其中一些:

扫描:

#include<stdio.h>

main()  
{
int flag=0,num1, num2;

printf("Give a number between 0-999");

do{

    if(flag == 1){
       printf("Wrong Input. Plz give a number between 0-99 ");    
    }   
    printf("\nnumber 1: ");
    scanf("%d", &num1);
    flag=1;   
}while(num1<0 || num1>1000);

printf("Give one more");
flag=0;

do{

    if(flag == 1){
       printf("Wrong Input. Plz give a number between 0-99 ");    
    }   
    printf("\nnumber 2: ");
    scanf("%d", &num2);
    flag=1;   
}while(num2<0 || num2>1000);

if(num1>num2){
    printf("the max number is : %d\n", num1);
}else 
    if (num1<num2){
        printf("the max number is : %d\n", num2);
    }else
        printf("they are equals\n");            

}

但是,如果我按 a 不是数字,它会无限打印:

数字 2:输入错误。请给出 0-99 之间的
数字 2:输入错误。请给出 0-99 之间的
数字 2:输入错误。请给一个 0-99 之间的数字
// 然后继续..

然后我尝试使用getchar:

#include<stdio.h>

main()
{
int flag=0;
char input1,input2;

printf("Give a number between 0-999");

do{

    if(flag == 1){
       printf("Wrong Input. Plz give a number between 0-99 ");    
    }   
    printf("\nnumber 1: ");
    input1 = getchar();
    flag=1;   
}while(input1<0 || input1>1000);

printf("Give one more");
flag=0;

do{

    if(flag == 1){
       printf("Wrong Input. Plz give a number between 0-99 ");    
    }   
    printf("\nnumber 2: ");
    input1 = getchar();
    flag=1;   
}while(input2<0 || input2>1000);

if(input1>input2){
    printf("the max number is : %c\n", input1);
}else 
    if (input1<input2){
        printf("the max number is : %c\n", input2);
    }else
        printf("they are equals\n");


}

但这段代码不起作用。有什么建议么?

4

1 回答 1

2

scanf返回成功扫描或-1出错的值的数量。因此,如果scanf没有返回1,则说明出现问题,您应该采取相应措施。


问题getchar在于它返回一个字符(或EOF错误。因此,如果您输入一个数字,则只会返回第一个数字,并且作为字符而不是数字。然后第二次调用getchar将返回第一个数字的第二个数字输入,作为一个字符。


我建议输入函数看起来像这样:

int get_input(void)
{
    /* Infinite loop */
    for (;;)
    {
        printf("Please enter a value between 1 and 9999 (inclusive): ");
        fflush(stdout);

        char buffer[256];

        /* Read input from the user */
        if (fgets(buffer, sizeof(buffer), stdin) == NULL)
        {
            perror("Error getting input");
            return -1;
        }

        int value;

        /* Extract an integer value from the input */
        if (sscanf(buffer, "%d", &value) != 1)
        {
            printf("Your input was not a valid value.\n");
            continue;  /* Try to get input again */
        }

        /* Check for a valid value */
        if (value >= 1 && value <= 9999)
            return value;  /* Got a value that is allowed */

        printf("Not a valid value.\n");
        /* Since we're in a loop, will continue from the beginning by asking for a value */
    }

    return -1;  /* Must return a value */
}

调用这个函数来获取一个值,如果它返回 -1 则有一个错误:

int value1;
if ((value1 = get_input()) == -1)
    exit(1);  /* Error reading value */
于 2013-10-23T11:28:25.763 回答