Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果输入了无效的输入(在C编程中),如何使该程序再次请求输入?_C_Loops_Input_While Loop_Scanf - Fatal编程技术网

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

如果输入了无效的输入(在C编程中),如何使该程序再次请求输入?,c,loops,input,while-loop,scanf,C,Loops,Input,While Loop,Scanf,检查发生在两个不同的地方 也就是说,当您输入a、s、m、d或q时,以及当您输入第一个和第二个数字时 在任何检查中,如果检查为假,则应要求您重新输入输入 我猜这可以通过将数字部分的scanf语句放入while循环检查中来实现,但当我输入无效值(非数字)时,循环将无限运行 所以我一定是做错了什么。我已经使a、s、m、d和q部分在大多数情况下都起了作用 但第二部分似乎从未奏效。为此,我将失败的尝试留在while循环中,而是放在//comments中 任何帮助都将不胜感激! 以下是我目前的代码: #in

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

也就是说,当您输入
a
s
m
d
q
时,以及当您输入第一个和第二个数字时

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

我猜这可以通过将数字部分的scanf语句放入while循环检查中来实现,但当我输入无效值(非数字)时,循环将无限运行

所以我一定是做错了什么。我已经使
a
s
m
d
q
部分在大多数情况下都起了作用

但第二部分似乎从未奏效。为此,我将失败的尝试留在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;
}
#包括
#包括
内部主(空)
{
char ch;
浮点数num1,num2,回答;
printf(“输入您选择的操作:\n”);
printf(“a.加s.减”);
printf(“m.乘q.除”\n);
printf(“q.quit\n”);
而((ch=getchar())!='q')
{
printf(“输入您选择的操作:\n”);
printf(“a.加s.减”);
printf(“m.乘q.除”\n);
printf(“q.quit\n”);
ch=托洛尔(ch);
如果(ch='\n')
继续;
其他的
{
开关(ch)
{
案例“a”:
//下面的代码是我试图使其工作的代码。
//该代码也将复制粘贴到其他案例中,
//当然,分别使用正确的操作。
//
//printf(“输入第一个数字:”)
//而(scanf(“%f”,&num1)==0)
//{
//printf(“无效输入。请输入一个数字。”);
//scanf(“%f”和num1);
//}
//printf(“输入第二个数字:”)
//而(scanf(“%f”,&num2)==0)
//{
//printf(“无效输入。请输入一个数字。”);
//scanf(“%f”&num2);
//}
//答案=num1+num2;
//printf(“%f+%f=%f\n”,num1,num2,答案);
//中断;
//
//我还尝试使用do while循环来完成这项工作
printf(“输入第一个数字:”);
scanf(“%f”和num1);
printf(“输入第二个数字:”);
scanf(“%f”&num2);
答案=num1+num2;
printf(“%f+%f=%f\n”,num1,num2,答案);
打破
案例s:
printf(“输入第一个数字:”);
scanf(“%f”和num1);
printf(“输入第二个数字:”);
scanf(“%f”&num2);
答案=num1-num2;
printf(“%f-%f=%f\n”,num1,num2,答案);
打破
案例“m”:
printf(“输入第一个数字:”);
scanf(“%f”和num1);
printf(“输入第二个数字:”);
scanf(“%f”&num2);
答案=num1*num2;
printf(“%f*%f=%f\n”,num1,num2,答案);
打破
案例“d”:
printf(“输入第一个数字:”);
scanf(“%f”和num1);
printf(“输入第二个数字:”);
scanf(“%f”&num2);
答案=num1/num2;
printf(“%f/%f=%f\n”,num1,num2,答案);
打破
违约:
printf(“这不是一个有效的操作。\n”);
打破
}
}
}
返回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;
#包括
#包括
内部主(空)
{
char ch;
浮点数num1,num2,回答;
printf(“输入您选择的操作:\n”);
printf(“a.加s.减”);
printf(“m.乘q.除”\n);
printf(“q.quit\n”);
而((ch=getchar())!='q')
{
ch=托洛尔(ch);
//忽略空白
如果(ch='\n')
继续;
其他的
{
开关(ch)
{
//附加部分
案例“a”:
//第一个数字
printf(“输入第一个数字:”);
//检查输入是否为数字
而(scanf(“%f”,&num1)==0)
{
printf(“无效输入。请输入一个数字,如2.5、-1.78E8或3:”);
scanf(“%*s”);
}
//第二个数字
printf(“输入第二个数字:”);
而(scanf(“%f”,&num2)==0)
{
printf(“无效输入。请输入一个数字,如2.5、-1.78E8或3:”);
scanf(“%*s”);
}
//为各自的操作做数学运算
答案=num1+num2;
//打印结果
printf(“%.3f+%.3f=%.3f\n”,num1,num2,答案);
打破
//减法部分
案例s:
printf(“输入第一个数字:”);
而(scanf(“%f”,&num1)==0)
{
printf(“无效输入。请输入一个数字,如2.5、-1.78E8或3:”);
scanf(“%*s”);
}
printf(“输入第二个数字:”);
而(scanf(“%f”,&num2)==0)
{
printf(“无效输入。请输入一个数字,如2.5、-1.78E8或3:”);
scanf(“%*s”);
}
while(scanf("%f",&num1)==0)
{
     printf("Invalid input. Please enter a number.");
      scanf("%f",&num1);
 }
int result;
while((result = scanf("%f",&num1))==0)
{
     printf("Invalid input. Please enter a number.");
}

if (result == EOF) .... report an error and exit ...
while(scanf("%f",&num1)==0)   //Taking Input Here Once
{
    printf("Invalid input. Please enter a number.");
    scanf("%f",&num1);             //Again Taking input.
}
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   
}
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.       
}
 if(ch =='\n')
     continue;