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

时间:2017-02-26 03:33:34

标签: c loops input while-loop scanf

检查发生在两个不同的位置。

也就是说,您输入asmdq以及输入第一个和第二个数字的位置。

在任何检查中,如果检查结果为false,则应要求您重新输入输入。

我猜这可以通过在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;
}

再次感谢您的帮助! 雅将是一个救命的人! 干杯! - 会不会。

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

#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语句。

2 个答案:

答案 0 :(得分:2)

scanf

此循环每次迭代扫描两个数字。这不是你想要的。丢失第二个int result; while((result = scanf("%f",&num1))==0) { printf("Invalid input. Please enter a number."); } if (result == EOF) .... report an error and exit ...

您还应检查EOF和错误。

{{1}}

答案 1 :(得分:2)

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

  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)调用时,由于输入,缓冲区中存在换行符\n,因此{{1 },新行ch = getchar()存储在\n中,后面的ch条件满足,循环再次执行。

    if