我如何不能忽视0(零)成绩?

时间:2017-12-04 12:12:59

标签: c

int main(){

    char students_number[30], students_grade[30];
    char *number, *value;
    int flag=0, students, i, grade, a=0, b=0, c=0, d=0, f=0;
    float sum=0;

    while(flag==0) // This while loop exist just because to run program until the number of students will be given correct..
    {

        printf("Please enter the number of students (It must be between 1-100): ");
        scanf("%s",&students_number); //  This scanf gets the number of students as an array instead of integer because the number which was given needs to be checked..
        students = strtol(students_number, &number, 10); // strtol is a function of stdlib.h and checks the variable is whether int or not for this program..

        if(students<=100 && students>0)
        {
            for(i=1;i<=students;i++)
            {
                printf("Please enter %d. student's grade (in integer form):",i);
                scanf("%s",&students_grade);//  This scanf gets the number of students as an array instead of integer because the number which was given needs to be checked..
                grade = strtol(students_grade, &value, 10); // This line checks the grade which was given is integer or not by using strtol which is in the stdlib.h..

                if(grade<0 || grade>100 || grade=='\0') 
                {
                    printf("The grade of the student was given incorrect!\n");
                    i--; // To make the for loop which is on the 25th line work again until the grade will be given correct..   
                }
                else
                {
                    if(grade<=50 && grade>=0) // This if and else if commands work for to count how many f,d,c,b and a are exist..
                        f++;
                    else if(grade<=60 && grade>=51)
                        d++;
                    else if(grade<=73 && grade>=61)
                        c++;
                    else if(grade<=85 && grade>=74)
                        b++;
                    else if(grade<=100 && grade>=86) 
                        a++;
                    sum += grade;               
                }
            }
            sum /= students; // This command divides the sum of the grades to number of the students to get the average results in the class..

            printf("\nThe average result of the class is %.2f..\n",sum);
            printf("\nThe letter form of the all results are:\nNumber of F: %d\nNumber of D: %d\nNumber of C: %d\nNumber of B: %d\nNumber of A: %d\n",f,d,c,b,a);
            flag = 1; // As it was mentioned before, this commands exist to break the while loop because the program was done successfully..
        }
        else // This if command controls the number of students wheter was given right or not..
        {
            printf("Please enter a proper number of students.\n");
            flag = 0;           
        }
    }
    return 0;
}

你好,这是我的第一个问题。我必须创建一个程序来计算结果的平均值。但是当我输入0(零)作为等级时,它不允许它只是因为我试图排除除int类型之外的所有类型。

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

您可以使用scanf读取一个数字并检查scanf是否正确完成了其工作:

来自man scanf

  

返回值   这些函数返回成功匹配和分配的输入项的数量,这可以少于提供的数量,或者在早期匹配失败的情况下甚至为零。

因此,您可以检查您是否已使用scanf读取整数,而无需编写if (value == '\0'),这会阻止您阅读0等级...

for(i=1;i<=students;i++)
{
    int ok;

    printf("Please enter %d. student's grade (in integer form):",i);

    /* read line from input. Why using fgets instead of scanf directly? See http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html*/
    if (NULL == fgets(students_grade, sizeof students_grade, stdin))
    {
         perror("fgets");
         exit(1);             
    }

    /* **try** to parse what have been read */
    ok = sscanf(students_grade, "%d", &value);

    /* check that scanf has done its work */
    if (1 != ok)
    {
        printf("The grade of the student was given incorrect!\n");
            i--; // To make the for loop which is on the 25th line work again until the grade will be given correct..   

    }
    else
    {
        if(grade<=50 && grade>=0) // This if and else if commands work for to count how many f,d,c,b and a are exist..
        f++;
        /* ... */
    }

我还建议你阅读这篇文章:http://sekrit.de/webdocs/c/beginners-guide-away-from-scanf.html