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

问题描述 投票:1回答:2

这个问题在这里已有答案:

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

也就是说,当你输入第一个和第二个数字时,输入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;
}

再次感谢您的帮助!你会成为一个救生员!干杯! -Will 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语句。

c loops input while-loop scanf
2个回答
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. } 换句话说,永远不会读取不匹配的字符。所以当你输入例如一个字符,当scanf继续在同一个字符上失败时,你的代码将无限循环。
  3. 当程序执行其最后的scanf("%f",&num2)调用时,由于输入,缓冲区中存在换行符\n字符,因此由于ch = getchar(),新行\n存储在ch中,并且随后的if条件满足并且循环再次执行。 if(ch =='\n') continue;

2
投票
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 ...
© www.soinside.com 2019 - 2024. All rights reserved.