Code:
#include <stdio.h>
int main( void )
{
int grade;
int total;
int counter;
float average;
total = 0;
counter = 0;
printf ("Enter grade, -1 to find class average\n");
scanf ("%d", &grade);
while (grade != -1)
{
total = total + grade;
counter = counter + 1;
average = (float) total / counter;
printf ("Enter grade, -1 to find class average\n");
scanf ("%d", &grade);
}
if (average <= 74)
{
printf ("The average grade is %.2f\n", average);
printf ("0-74: F\n");
printf ("Enter grade, -1 to find class average, -2 to quit\n");
scanff ("%d", &grade);
}
else
if (average <= 82)
{
printf ("The average grade is %.2f\n", average);
printf ("75-82: C\n");
printf ("Enter grade, -1 to find class average, -2 to quit\n");
scanff ("%d", &grade);
}
else
if (average <= 91)
{
printf ("The average grade is %.2f\n", average);
printf ("83-91: B\n");
printf ("Enter grade, -1 to find class average, -2 to quit\n");
scanff ("%d", &grade);
}
else
if (average <= 100)
{
printf ("The average grade is %.2f\n", average);
printf ("92-100: A\n");
printf ("Enter grade, -1 to find class average, -2 to quit\n");
scanff ("%d", &grade);
}
return 0;
}
2 Problems I am having here.
1. The loop does not continue after typing "-1" to find the grade average. I tried inserting another "while" statement after the "if...else" statements, but it just terminates after I enter "-1" again without printing the grade average.
2. Not quite sure how to get the program to terminate on my terms. Its terminating itself because it's not running back through the loop after typing "-1", but once thats fixed I'm not sure how to go about making it terminate using the sentinal value. I tried a "while" statement using "-2" but my printf was not displayed and the program just quit for the previously mentioned reason.
Any help would be appreciated both on those issues and my coding in general. I want to establish good habits from the start. If at all possible, it would be great to be pointed in the right direction without actually being given the exact answer. Thanks!