I have this program that I am working on using C. The code compiles and runs I am just a little lost with the last portion of it. The purpose of the program is for the user to enter grades for multiple students. When the user has finished with one student they enter -1 at that point the the average is displayed. Then it is suppose to reset and start with student 2 and so on.
***This is the part I am having problems with.
When the user has not more students to enter grades for -1 in the grade 1 to indicate no more students. Then it should produce the average for all students.
I realize that this isn't the simpliest code to use but I can not use an array which would make this program so much easier.
Any assistance would be a big help.
Code:
#include <stdio.h>
int main()
{
int studentquantity = 1;
int gradenumber = 1;
int grade;
int studenttotal = 0;
int totalnumgrades;
int overallgrade;
float average;
float classaverage;
printf("To change change students enter -1\n");
printf("When completed with list enter -1 as first score\n\n");
do {
do {
do {
printf("Student #%d, grade #%d = ", studentquantity, gradenumber);
scanf("%d", &grade );
if (grade > 100)
printf("Grade is invalid, try again\n");
else
break;
} while ( grade != -1 );
if (grade < 0 ) break;
gradenumber = gradenumber + 1;
studenttotal = studenttotal + grade;
} while ( grade != -1 );
gradenumber--;
average = ( float ) studenttotal / gradenumber;
printf("Student #%d had %d grade(s), average is %.2lf\n", studentquantity, gradenumber, average);
totalnumgrades = gradenumber;
overallgrade = studenttotal;
gradenumber = 1;
grade = 0;
studentquantity++;
} while ( ( gradenumber == 1) && (grade == -1) );
if ( ( gradenumber == 1) && (grade == -1) )
classaverage = (float) overallgrade / totalnumgrades;
printf("There were %d student(s) with an average of %.2fl\n", studentquantity, classaverage);
system("PAUSE");
return 0;
}