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



发生了两个不同的斑点。

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

在任何检查中,如果支票是错误的,则应要求您重新输入输入。

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

所以我必须做错了什么。我在大多数情况下制作了asmdq零件工作。

,但第二部分似乎从来没有起作用。为此,我将失败的尝试段循环放出,而是在//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. subtractn");
    printf("m. multiply q. dividen");
    printf("q. quitn");
    while ((ch = getchar())!='q')
    {
        printf("Enter the operation of your choice:n");
        printf("a. add      s. subtractn");
        printf("m. multiply q. dividen");
        printf("q. quitn");
        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 = %fn",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 = %fn",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 = %fn",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 = %fn",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 = %fn",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. subtractn");
    printf("m. multiply q. dividen");
    printf("q. quitn");
    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 = %.3fn", 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 = %.3fn", 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 = %.3fn", 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 = %.3fn", 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. subtractn");
    printf("m. multiply q. dividen");
    printf("q. quitn");
}
printf("Bye.n");
return 0;

}

回头看,我可能可以没有/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 hewline n字符中存在于缓冲区中,因此由于ch = getchar(),新行n被存储在ch中,并且遵循if条件满足,循环再次执行。

     if(ch =='n')
         continue;
    
while(scanf("%f",&num1)==0)
{
     printf("Invalid input. Please enter a number.");
      scanf("%f",&num1);
 }

此循环在迭代中扫描两个数字。这不是您想要的。丢失第二个scanf

您还应该检查EOF和错误。

int result;
while((result = scanf("%f",&num1))==0)
{
     printf("Invalid input. Please enter a number.");
}
if (result == EOF) .... report an error and exit ...

最新更新