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型以外のすべての型を除外しようとしたため、許容されません。どのように私はグレードとして0(ゼロ)を無視することはできません?
どうすればこの問題を解決できますか?
質問をより明確に説明できますか? – Unh0lys0da
ようこそstackoverflow.comへようこそ。 [ヘルプページ](http://stackoverflow.com/help)、特に[ここではどのトピックを聞くことができますか?](http://stackoverflow.com/help/)のセクションを読んでください。 on-topic)と[[どのような種類の質問を避けるべきですか?]](http://stackoverflow.com/help/dont-ask)を参照してください。また、[ツアーを受けてください](http://stackoverflow.com/tour)と[良い質問をする方法を読む](http://stackoverflow.com/help/how-to-ask)もご覧ください。最後に、[最小限の完全で検証可能な例](http://stackoverflow.com/help/mcve)の作成方法を学んでください。 –
これを他のものの代わりに使用すると(グレード<0 || grade> 100)、すべてのchar型のものが許可され、ループに入ります –