【问题标题】:Entering a character instead of integer输入字符而不是整数
【发布时间】:2015-12-15 20:58:58
【问题描述】:

我正在使用 do-while 循环将菜单打印到屏幕上。我正在阅读选择作为一个整数。问题是,如果用户输入一个字符,程序就会爆炸。我怎样才能避免这种情况?

#include <stdio.h>
int menu() {            // prints the main menu of labs///
    int choice;
    printf("1)Lab 5 ( Repetetitions ).\n2)Lab 10 ( Passing 1D-Arrays to functions ).\n3)GPA Calculation.\n4)EXIT.\n\nEnter your choice: ");
    scanf("%d", &choice);
    return choice;
}

int main() {
    int choice;

    do {

        choice = menu();
        if (choice != 4) {

            if (choice == 1)
                //lab5(choice);

            else if (choice == 2)
                //lab10(choice);


            else if (choice == 3)
                //  lab11(choice);
            else
                printf("invalid choice\n");
        }

    } while (choice != 4);
    return 0;
}        

【问题讨论】:

  • 为避免这种情况,请读入一个字符串,然后自行将其转换为整数。
  • 检查来自scanf的返回以确保它是好的。如果没有清除缓冲区,请重试。
  • main() 函数作为一个简单的switch() 语句会更清晰
  • 这里有点幽默:从程序中删除爆炸物质和爆破帽,然后用户不能'炸毁'

标签: c scanf


【解决方案1】:

这应该对你有用,你需要检查 scanf 的返回值

int menu() {            // prints the main menu of labs///
    int choice;
    printf("1)Lab 5 ( Repetetitions ).\n2)Lab 10 ( Passing 1D-Arrays to functions ).\n3)GPA Calculation.\n4)EXIT.\n\nEnter your choice: ");
    if(scanf("%d", &choice) == 1)
    {
        return choice;
    }
    else
    {
        return 0;
    }
}

【讨论】:

    【解决方案2】:

    scanf()(和函数族)从输入缓冲区返回成功转换的次数,如果转换(%d)失败,函数返回 0(或 EOF)。在这种情况下,无法转换的字符不会从缓冲区中删除,这就是为什么会出现无限循环,转换永远失败的原因。

    scanf 调用之后,我使用此method 刷新输入缓冲区,程序按预期运行。

    Run the program online(不知道还能坚持多久)

    void flushInput(){
      int c;
      while((c = getchar()) != EOF && c != '\n')
            /* discard */ ;  
    }
    
    int menu() {            // prints the main menu of labs///
        int choice;
        printf("1)Lab 5 ( Repetetitions ).\n2)Lab 10 ( Passing 1D-Arrays to functions ).\n3)GPA Calculation.\n4)EXIT.\n\nEnter your choice: ");
        if(scanf("%d", &choice) != 1){
            flushInput();
            return 0;
        }else{
            flushInput();
            return choice;
        }
    }
    

    【讨论】:

    • 你应该检查scanf的返回值。您的代码将刷新虚假输入,但如果输入失败,它将使用不确定的值 choice
    • 无论选择成功与否都应该刷新输入
    • 另外,一些文档说如果转换失败它会返回 EOF,但我检查并在这种情况下返回 0。官方行为是什么?
    • 通常返回成功匹配的数量。 EOF 仅在需要更多字符才能开始转换时才返回,但没有更多字符。
    • 关于此语句:“scanf 函数返回从输入缓冲区成功读取的字符数”这不是真的! scanf()(和函数族)返回成功输入转换的次数。成功时,返回的值将匹配 'format' 参数中格式说明符的数量。
    【解决方案3】:
    /*USE This Code*/  
    #include <stdio.h>
    #include <stdlib.h>
    void orderAsk(int orderStorage[1]);
    int main()
    {     
        int orderStorage[1];
        orderAsk(orderStorage);
        printf("%d",orderStorage[0]);
        return 0;
    }
    
    void orderAsk(int orderStorage[1]){
        int d;
        d = scanf("%d", &orderStorage[0]);
        if ( d!= 1) {
            system("cls");
            fflush(stdin);
            printf("Error! Re-Enter\n");
            orderAsk(orderStorage);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-08-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-12-17
      • 1970-01-01
      相关资源
      最近更新 更多