Array issues, first time using them.

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <iomanip>

using namespace std;

//function prototypes

void GetData (int TestScoresf [], int &indexf);
float ComputeAvg (int TestScoref [], int indexf);
void PrintReport (int TestScoref [], int indexf, float Avgf);

//constant identifier

const int MAXSIZE=10;

//Main body

int main()
{
	//local identifiers
	int TestScores [MAXSIZE];
	int index;
	float Avg;

	//function calls
	GetData (TestScores, index);
	Avg = ComputeAvg (TestScores, index);
	PrintReport (TestScores, index, Avg);

    system("pause");
    return 1;

} //end main

//function deffinitions

void GetData (int TestScoresf [], int &index)
{
	int indexf = -1;
	do
	{
		indexf++;
		cout<<"Please enter test #"<<indexf + 1<<": <-1 to exit>\t";
		cin>>TestScoresf[indexf];
	} while (TestScoresf[indexf] >= 0);

	return;
} //end of GetData

float ComputeAvg (int TestScoresf[], int indexf)
{
	float Avg;
    int sum = 0;
    int i = 0;
	for (i >= MAXSIZE; i < indexf; i++)
	{
    	cout<<TestScoresf;
     }
    Avg = float (sum) / indexf;
	return Avg;
} //end ComputeAvg

void PrintReport (int TestScoref [], int indexf, float Avgf)
{
	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];
} while (TestScoref [indexf] >= 0);

	cout<<"Class average = "<<Avgf<<endl;
	
	return;

} //end of PrintReport  
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.
closed account (DSLq5Di1)
Your professor wrote this? uckk..

1
2
3
4
5
6
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); // ??
    ...
Thank you both!
Topic archived. No new replies allowed.