So this compiles but when it runs it exits before executing the getAvg function and doesn't execute anything after the getAvg function call. I have been looking at this for hours, what am I missing that is causing this to happen?
void getNames (string[], int);
void getScores (string[], double[], int);
double getAvg (double[], int); // average function prototype
int main()
{
constint NAMES = 5; // Number of names to hold.
constint GRADES = 5; // Number of grades to hold.
constint SCORES = 4; // Number of scores to hold for each student.
string name[NAMES]; // Five string array for names.
char grade[GRADES]; // Five char array for grades.
double sScores[SCORES]; // Four double array to hold student scores.
int count; // Loop counter
double average; // To hold average.
// Get five student names from user.
getNames(name, NAMES);
// Get four scores for each student from user.
getScores(name, sScores, SCORES);
// Get each students average.
average = getAvg(sScores, SCORES);
for (count = 0; count < NAMES; count++)
{
cout << "The grade average for " << name[count] << " is: "
<< average << endl;
}
// Make sure we place the end message on a new line
cout << endl;
// The following is system dependent. It will only work on Windows
system("PAUSE");
/*
// A non-system dependent method is below
cout << "Press any key to continue";
cin.get();
*/
return 0;
}
void getNames(string name[], int totNames)
{
int count; // Loop counter
for (count = 0; count < totNames; count++)
{
cout << "\nEnter the name of student " << (count + 1) << ": ";
cin >> name[count];
}
}
void getScores(string name[], double scores[], int totScores)
{
int num; // Loop counter
for (num = 0; num < totScores; num++)
{
// Accept scores 1-100 only.
while (scores[num] < 0 || scores[num] > 100)
{
cout << "Error: Enter scores in range 1-100: ";
cin >> scores[num];
}
cout << "Enter score " << (num + 1) <<" for " << name[num] << ": ";
cin >> scores[num];
}
}
double getAvg(double scores[], int scoreTotal)
{
int count; // Loop counter
double avg, sum;
avg = sum / scoreTotal;
for (count = 0; count < scoreTotal; count++)
sum += scores[count];
return avg;
}
For one, you don't need to declare a counter for a loop outside the loop itself. Just put the declaration where you put the assignment to the initial value. As for the issue here, look at line 72. What value does the variable sum hold right there?
I fixed the loop declarations, and set sum = 0 so the getAvg functions looks like this:
double getAvg(double scores[], int scoreTotal)
{
double avg,
sum = 0;
avg = sum / scoreTotal;
for (int count = 0; count < scoreTotal; count++)
sum += scores[count];
return avg;
}
Well, for one put avg = sum / scoretotal;
after the actual for loop, so the function makes sense and doesn't just return 0.
Also, your function doesn't do what you think it does. I recommend looking into structures; they will be a big help in a case like this. Rather than having three arrays in this odd format, you can have a structure that includes name, scores, and the functions required to interact with the structure.
The problem requires a five string array for names, four double array for individual scores, and five character array for grades. I moved avg = sum/scoreTotal to the bottom of the loop and it is still returning zero. I am thinking of not using the function and finding a way to get the averages to run in main.