简体   繁体   English

如何防止用户输入的错误值存储在变量中?

[英]How to prevent the wrong value entered by the user from getting stored in the variable?

I'm trying to prevent the user from entering a wrong value in this simple C program by using an if statement within while loop. 我试图通过在while循环中使用if语句来防止用户在此简单的C程序中输入错误的值。 But the problem is that whenever the user enters a wrong value, it gets stored in the variable and the same value is then use for further calculations. 但是问题在于,只要用户输入错误的值,它就会存储在变量中,然后将相同的值用于进一步的计算。 Here's the actual program: 这是实际的程序:

#include <stdio.h>
#include <stdlib.h>
/*Program to calculate the marks obtained scored by the class in a quiz*/

    int main()
    {
    float marks, average, total = 0.0;
    int noStudents;
    printf("Enter the total number of students: ");
    scanf("%d", &noStudents);
    int a=1;
    while(a<=noStudents)
    {
        printf("Enter marks obtained out of 20: ");
        scanf("%f", &marks);
        if(marks<0 || marks >20)
        {
            printf("You have entered wrong marks\nEnter again: ");
            scanf("%d", &marks);
        }
        total = total+marks;
        a++;
    }
    average = total/(float)noStudents;
    printf("Average marks obtained by the class are: %f", average);


    return 0;
}

The first problem is the inconsistency in your code. 第一个问题是代码中的不一致。 Inside the condition statement body, you wrote 在条件语句主体中,您编写了

 scanf("%d", &marks);

which uses mismatched argument type for %d . %d使用不匹配的参数类型。 This invokes undefined behavior . 这会调用未定义的行为 You should be using %f , as before. 您应该像以前一样使用%f

That said, 那就是

  • you're relying on user to correct themselves in the second attempt, don't do that. 您在第二次尝试中依靠用户来纠正自己 ,请不要这样做。 Use a loop and only after you get a valid value, break out of that. 使用循环,只有在获得有效值之后,才可以使用该循环。
  • In the statement average = total/(float)noStudents; 在语句中, average = total/(float)noStudents; , you don't need the cast. ,则不需要演员表。 One of the operand, total is already of float type, so the other operand will be automatically promoted and floating point division will take place, even in absence of the explicit cast. 其中一个操作数total已经是float型的,因此即使没有显式强制转换,另一个操作数也将被自动提升并进行浮点除法。

Have slightly tweaked your code. 略微调整了您的代码。 Hope it helps. 希望能帮助到你。 As already mentioned in one of the comment, don't expect user to give the correct value in out of range scenario. 正如评论中已经提到的那样,不要指望用户在超出范围的情况下提供正确的值。 You should keep on asking the user to enter within the range unless he feeds in the correct value. 除非用户输入正确的值,否则应继续要求用户输入该范围内的值。

#include<stdio.h>
#include<stdlib.h>

int main()
{
    float marks, average, total = 0.0;
    int noStudents;
    printf("Enter the total number of students: ");
    scanf("%d", &noStudents);
    int a=1;

     while(a<=noStudents)
     {
        printf("Enter marks obtained out of 20: ");
        scanf("%f", &marks);
        if(marks<0 || marks >20)
        {
            printf("You have entered wrong marks.Enter again:\n ");
            continue;
        }
        total = total+marks;
        a++;
     }
    average = total/noStudents;
    printf("Average marks obtained by the class are: %f", average);

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM