c - 如果输入无效输入(在 C 编程中),如何使该程序再次请求输入?

标签 c loops input while-loop scanf

<分区>

检查发生在两个不同的地方。

也就是说,您在其中输入 asmdq 以及当您输入第一个和第二个数字时。

在任何检查中,如果检查为假,它应该要求您重新输入您的输入。

我猜这可以通过在 while 循环检查中为数字部分放置一个 scanf 语句来完成,但是当我输入无效值(非数字)时,循环会无限运行。

所以我一定是做错了什么。我制作了 asmdq 部分大部分时间都在工作。

但是第二部分似乎从来没有奏效。为此,我将失败的尝试留在了 while 循环中,而不是在 //comments 中。

任何帮助将不胜感激! 到目前为止,这是我的代码:

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char ch;
    float num1,num2,answer;
    printf("Enter the operation of your choice:\n");
    printf("a. add      s. subtract\n");
    printf("m. multiply q. divide\n");
    printf("q. quit\n");
    while ((ch = getchar())!='q')
    {
        printf("Enter the operation of your choice:\n");
        printf("a. add      s. subtract\n");
        printf("m. multiply q. divide\n");
        printf("q. quit\n");
        ch=tolower(ch);
        if (ch=='\n')
            continue;
        else
        {
            switch(ch)
            {
                case 'a':
                //The code below is what I have tried to make work.
                //This code would also be copy pasted to the other cases,
                //of course with the correct operations respectively being used.
                //
                //printf("Enter first number: ")
                //while(scanf("%f",&num1)==0)
                //{
                //  printf("Invalid input. Please enter a number.");
                //  scanf("%f",&num1);
                //}
                //printf("Enter second number: ")
                //while(scanf("%f",&num2)==0)
                //{
                //  printf("Invalid input. Please enter a number.");
                //  scanf("%f",&num2);
                //}
                //answer = num1 + num2;
                //printf("%f + %f = %f\n",num1,num2,answer);
                //break;
                //
                //I have also tried to make this work using do-while loops
                printf("Enter first number: ");
                scanf("%f",&num1);
                printf("Enter second number: ");
                scanf("%f",&num2);
                answer = num1 + num2;
                printf("%f + %f = %f\n",num1,num2,answer);
                break;
            case 's':
                printf("Enter first number: ");
                scanf("%f",&num1);
                printf("Enter second number: ");
                scanf("%f",&num2);
                answer = num1 - num2;
                printf("%f - %f = %f\n",num1,num2,answer);
                break;
            case 'm':
                printf("Enter first number: ");
                scanf("%f",&num1);
                printf("Enter second number: ");
                scanf("%f",&num2);
                answer = num1 * num2;
                printf("%f * %f = %f\n",num1,num2,answer);
                break;
            case 'd':
                printf("Enter first number: ");
                scanf("%f",&num1);
                printf("Enter second number: ");
                scanf("%f",&num2);
                answer = num1 / num2;
                printf("%f / %f = %f\n",num1,num2,answer);
                break;
            default:
                printf("That is not a valid operation.\n");
                break;
        }
    }
}
return 0;
}

再次感谢您的帮助! 你会是一个救命恩人! 干杯! -威尔·S。

编辑:我的代码可以工作了!这是最终代码...

#include <stdio.h>
#include <ctype.h>

int main(void)
{
    char ch;
    float num1,num2,answer;
    printf("Enter the operation of your choice:\n");
    printf("a. add      s. subtract\n");
    printf("m. multiply q. divide\n");
    printf("q. quit\n");
    while ((ch = getchar())!='q')
    {
        ch=tolower(ch);
        //Ignore whitespace
        if (ch=='\n')
            continue;
        else
        {
            switch(ch)
            {
                //Addition part
                case 'a':
                    //First number
                    printf("Enter first number: ");
                    //Check to see if input is a number
                while (scanf("%f",&num1)==0)
                {
                    printf("Invalid input. Please enter a number, such as 2.5, -1.78E8, or 3: ");
                    scanf("%*s");
                }
                //Second number
                printf("Enter second number: ");
                while (scanf("%f",&num2)==0)
                {
                    printf("Invalid input. Please enter a number, such as 2.5, -1.78E8, or 3: ");
                    scanf("%*s");
                }
                //Do math for respective operation
                answer = num1 + num2;
                //Print out result
                printf("%.3f + %.3f = %.3f\n", num1,num2,answer);
                break;
            //Subtraction part
            case 's':
                printf("Enter first number: ");
                while (scanf("%f",&num1)==0)
                {
                    printf("Invalid input. Please enter a number, such as 2.5, -1.78E8, or 3: ");
                    scanf("%*s");
                }
                printf("Enter second number: ");
                while (scanf("%f",&num2)==0)
                {
                    printf("Invalid input. Please enter a number, such as 2.5, -1.78E8, or 3: ");
                    scanf("%*s");
                }
                answer = num1 - num2;
                printf("%.3f - %.3f = %.3f\n", num1,num2,answer);
                break;
            //Multiplication part
            case 'm':
                printf("Enter first number: ");
                while (scanf("%f",&num1)==0)
                {
                    printf("Invalid input. Please enter a number, such as 2.5, -1.78E8, or 3: ");
                    scanf("%*s");
                }
                printf("Enter second number: ");
                while (scanf("%f",&num2)==0)
                {
                    printf("Invalid input. Please enter a number, such as 2.5, -1.78E8, or 3: ");
                    scanf("%*s");
                }
                answer = num1 * num2;
                printf("%.3f * %.3f = %.3f\n", num1,num2,answer);
                break;
            //Division part
            case 'd':
                printf("Enter first number: ");
                while (scanf("%f",&num1)==0)
                {
                    printf("Invalid input. Please enter a number, such as 2.5, -1.78E8, or 3: ");
                    scanf("%*s");
                }
                printf("Enter second number: ");
                while (scanf("%f",&num2)==0)
                {
                    printf("Invalid input. Please enter a number, such as 2.5, -1.78E8, or 3: ");
                    scanf("%*s");
                }
                //Check for if number is a zero
                while (num2==0)
                {
                    printf("Please enter a non-zero number, such as 2.5, -1.78E8, or 3: ");
                    while (scanf("%f",&num2)==0)
                    {
                        printf("Invalid input. Please enter a number, such as 2.5, -1.78E8, or 3: ");
                        scanf("%*s");
                    }
                }
                answer = num1 / num2;
                printf("%.3f / %.3f = %.3f\n", num1,num2,answer);
                break;
            //For if a non-valid operation is entered
            default:
                printf("That is not a valid operation.\n");
                break;
        }
    }
    printf("Enter the operation of your choice:\n");
    printf("a. add      s. subtract\n");
    printf("m. multiply q. divide\n");
    printf("q. quit\n");
}
printf("Bye.\n");
return 0;

回过头来看,我可能可以不用 if/else 语句。

最佳答案

您的代码存在多个问题。首先在这个循环中

  1. 您正在接受两次失败的输入

    while(scanf("%f",&num1)==0)   //Taking Input Here Once
    {
        printf("Invalid input. Please enter a number.");
        scanf("%f",&num1);             //Again Taking input.
    }
    

    相反,您想要的是检查 scanf() 的返回值,如果返回值是 0,您将再次执行循环,所以这就是方法这样做:

    int l = 0;
    
    while(l==0){  //Checking l, if it is zero or not, if zero running loop again.
        printf("Invalid input. Please enter a number.");
        l = scanf("%f",&num1);                   //Storing Return Value of scanf in l   
    }
    
  2. 当程序遇到任何包含scanf("%f", &num1)scanf("%f", &num2) 的行时,它将跳过所有空格并等待下一个输入。在输入与格式规范不匹配的情况下,输入不会被消耗并保留在输入缓冲区中。

    int l = 0;
    
    while(l==0){  //Checking l 
    
         printf("Invalid input. Please enter a number.");
         l = scanf("%f",&num1);  //scanf will look at the buffer if the input
                                 //does not match, it will not be consumed 
                                 //and will remain in buffer.       
    }
    

    换句话说,不匹配的字符永远不会被读取。所以当你输入例如a 字符,您的代码将无限循环,因为 scanf 在同一字符上继续失败。

  3. 当程序执行最后一个 scanf("%f",&num2) 调用时,由于 enter 有一个换行符 \n 字符存在于缓冲区中,因此由于 ch = getchar(),新行 \n 被存储在 ch 和以下if 条件满足,循环再次执行。

     if(ch =='\n')
         continue;
    

关于c - 如果输入无效输入(在 C 编程中),如何使该程序再次请求输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42464437/

相关文章:

html - 无法使用文字输入

ubuntu下编译头文件。我在终端中输入什么?

c - 模拟器中运行时函数调用出现 ARM 硬故障

c - 来自 fgets 的段错误

javascript - getJSON 请求循环遍历参数数组,每次调用之间有延迟

c++ - 如果输入是一个字符然后在 C++ 中存储在一个整数中,为什么 cin 会得到奇怪的值?

c - 当存在具有相同名称的局部变量时,如何在 C 中使用全局变量?

datetime - 批处理文件: Iterate over files modified since a given date

c - Tries 数据结构 - 打印所有值

javascript - 表单提交空值。我该如何防止这种情况?