【问题标题】:Exit when press 0 in C [duplicate]在C中按0时退出[重复]
【发布时间】:2016-10-04 00:40:06
【问题描述】:
int main()
{
int program = 0;
while (program >= 0)
{
    printf("\nChoose one of the following programs: \n\n (1) Fibonacci Sequence Calculator \n (2) Decimal and Binary Calculator \n (3) Prime Number Calculator \n \nIf you want to exit the program, press (e).\nYour choice: ");

    scanf("%d", &program);

    if (program == 0)
    {
        printf("Quitting the program...\n\n");
        return 0;
    }

    else if(program==1)
    {
        printf ("FIBONACCI SEQUENCE CALCULATOR");
    }
    else if(program==2)
    {
        printf("DECIMAL AND BINARY CALCULATOR");
    }

    else if(program==3)
    {
        printf("PRIME NUMBER CALCULATOR");
    }

    else
    {
        printf("ERROR");
    }
}

当用户输入除 0、1、2 和 3 以外的任何内容时,我想打印“ERROR”,但这是我输入时的结果:

  • 除 0、1、2 和 3 以外的任何数字:“ERROR”(这是正确的)
  • 任何字母/符号:“退出程序...”(这应该是“错误”!)

也许这可以帮助我找出问题的答案:我已经知道 %d 用于扫描整数, %c 用于扫描字符。但是当我想同时扫描它们时,我必须使用什么?

任何帮助将不胜感激:)

【问题讨论】:

  • scanf 返回匹配参数的数量,在您的情况下为 0 或 1。
  • 由于您使用的是%d,如果您输入任何不能被解释为整数的内容(除了空格),scanf 将不会更新program,并且旧值将保留。错误的输入也将保留在流中以供稍后再次读取。
  • scanf() 返回成功扫描的字段数,并留下无法在流中解释的输入,因此您可以稍后阅读...所以您可以尝试使用 %d,如果它不会返回 1 您可以尝试使用不同的函数或不同的说明符再次读取输入。

标签: c


【解决方案1】:

示例其中一种方式。

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

void end_proc(void){
    printf("Quitting the program...\n\n");
    exit(0);
}

void input_error_proc(void){
    int ch;
    printf("ERROR\n");
    while((ch = getchar()) != '\n' && ch != EOF);//clear input
}

int main(void){
    int program;

    while(1){
        printf("\n"
            "Choose one of the following programs: \n\n"
            " (1) Fibonacci Sequence Calculator \n"
            " (2) Decimal and Binary Calculator \n"
            " (3) Prime Number Calculator \n"
            " \n"
            "If you want to exit the program, press (e or 0).\n"
            "Your choice: ");

        if(1==scanf("%d", &program)){//input number 
            if (program == 0){
                end_proc();
            } else if(program == 1){
                printf ("FIBONACCI SEQUENCE CALCULATOR");
            } else if(program==2) {
                printf("DECIMAL AND BINARY CALCULATOR");
            } else if(program==3) {
                printf("PRIME NUMBER CALCULATOR");
            } else {
                input_error_proc();
            }
        } else {//input not number
            char ch, check;
            if(2 == scanf(" %c%c", &ch, &check) && ch == 'e' && check == '\n')
                end_proc();
            else
                input_error_proc();
        }
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    我添加了 digit 或 char 测试,并更改了您的程序流程。

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    int main() {
        int program = 0;
        char c;
        for (; ;) {
            printf("\nChoose one of the following programs: \n\n (1) Fibonacci Sequence Calculator \n (2) Decimal and Binary Calculator \n (3) Prime Number Calculator \n \nIf you want to exit the program, press (e).\nYour choice: ");
    
            scanf(" %c", &c);
    
            if (isalpha(c)) {
                printf("ERROR");
                continue;
            }
    
            else if (isdigit(c)) {
    
                program = atoi(&c);
    
                if (program == 0) {
                    printf("Quitting the program...\n\n");
                    return 0;
                }
    
                else if (program == 1) {
                    printf("FIBONACCI SEQUENCE CALCULATOR");
    
                }
                else if (program == 2) {
                    printf("DECIMAL AND BINARY CALCULATOR");
                }
    
                else if (program == 3) {
                    printf("PRIME NUMBER CALCULATOR");
                }
    
                else {
                    printf("ERROR");
                }
    
            }
    
        }
    }
    

    测试

    Debug/gnu
    
    Choose one of the following programs: 
    
     (1) Fibonacci Sequence Calculator 
     (2) Decimal and Binary Calculator 
     (3) Prime Number Calculator 
    
    If you want to exit the program, press (e).
    Your choice: 1
    FIBONACCI SEQUENCE CALCULATOR
    Choose one of the following programs: 
    
     (1) Fibonacci Sequence Calculator 
     (2) Decimal and Binary Calculator 
     (3) Prime Number Calculator 
    
    If you want to exit the program, press (e).
    Your choice: q
    ERROR
    Choose one of the following programs: 
    
     (1) Fibonacci Sequence Calculator 
     (2) Decimal and Binary Calculator 
     (3) Prime Number Calculator 
    
    If you want to exit the program, press (e).
    Your choice: 
    

    【讨论】:

    • program = atoi(&amp;c); 错误。
    • @BLUEPIXY program = c - '0'; 更好吗?
    • atoi 不应使用,因为它需要序列中的最后一个到 NUL 字符。 program = c - '0'; 更好,但它非常有限。(原来的似乎也被认为是负面的。)
    • 感谢大家的回答:)
    猜你喜欢
    • 2016-05-05
    • 2020-10-11
    • 2013-04-14
    • 1970-01-01
    • 2013-06-27
    • 2017-04-26
    • 2015-09-20
    • 1970-01-01
    • 2023-03-04
    相关资源
    最近更新 更多