Hello. I have to write a program that computes the averages of tests using arrays. I copied most of the code (all but the print report function) off the board the way it was written by the professor. Whenever I execute, the program crashes shortly after I type "-1." Does it have something to do with the print report function, or something else? Thank you for your time.
An easy way to check and see whether or not it has to do with the PrintReport function is to simply remove it from the program (use comments, don't delete) and see if the program still closes when you input -1. If it does, then it's not your PrintReport function. If -1 isn't a reasonable input for this program, consider implementing an error check within the GetData function.
**Also, I just looked over the GetData function to see if you had an error check implemented... it appears in the output prompt that -1 exits the program. Problem solved.
int main()
{
int TestScores [MAXSIZE];
int index; // index is undefined
float Avg;
...
GetData (TestScores, index);
1 2 3 4
void GetData (int TestScoresf [], int &index) // passed by reference but not used at all
{
int indexf = -1; // local variable used instead?
...
Avg = ComputeAvg (TestScores, index);
1 2 3 4 5 6 7 8 9 10 11 12
float ComputeAvg (int TestScoresf[], int indexf) // what does indexf = ?
{
float Avg;
int sum = 0;
int i = 0;
for (i >= MAXSIZE; i < indexf; i++) // ?? for(initialization; condition; iteration)
{
cout<<TestScoresf; // ??
}
Avg = float (sum) / indexf; // ?? sum = 0
return Avg;
}
PrintReport (TestScores, index, Avg);
1 2 3 4 5 6 7 8 9 10 11
void PrintReport (int TestScoref [], int indexf, float Avgf) // indexf = the undefined value of index
{
cout<<"Class Report:\n\n";
cout<<"Test #\t\t\t\tTest Score\n";
cout<<"------\t\t\t\t----------\n\n";
do
{
cout<<" "<<indexf + 1<<"\t\t\t\t\t"<<TestScoref [indexf]; // likely out of bounds,
// this is where you crash
} while (TestScoref [indexf] >= 0); // ??
...