gpt4 book ai didi

c - 我怎么不能忽略 0(零)作为成绩?

转载 作者:太空宇宙 更新时间:2023-11-04 04:20:01 25 4
gpt4 key购买 nike

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 类型之外的所有类型。

我怎样才能使它更正?

最佳答案

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

来自 man scanf :

Return Value These functions return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure.

因此您可以使用 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 .

关于c - 我怎么不能忽略 0(零)作为成绩?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47633117/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com