Get the average from ifstream of void function

These are the codes that the prof gives us, I find out I could not get any value from the text,in the void InputGrades and CalculateStatistics. Therefore, I can not finish the following functions, like calculate the average.
Sorry for the long-long code I posed!~~:)

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

//Declaring global variables in order to avoid having to pass arguments to functions

  int numGrades;           // Number of grades
  float average;           // Average grade
  int highest;             // Highest grade
  int lowest;              // Lowest grade
  int aboveAverage;        // Number of grades above the average
  int belowAverage;        // Number of grades below the average
  int grades[101];         // Array of grades


// Declare function prototypes
// "Pre" means what must have been done before invoking the function
// "Post" means what will have been done upon returning from the function

void CalculateStatistics(ifstream& inData);
// Post: Data has been read and statistics calculated.
void OpenFiles(ifstream& inData, ofstream& outData);
// Post: File names have been prompted for and files are opened.
//       Input file name has been written on output file
void InputGrades(ifstream& inData);
// Pre:  inData is assigned and not empty
// Post: Grades have been read from inData,
//       numGrades is the number of grades in inData
void CalculateAverage(float &average);
// Post: Average grade has been calculated
void CalculateHighest(int &highest);
// Post: Hightest grade has been calculated
void CalculateLowest(int &lowest);
// Post: Return value is the lowest grade
void CalculateAboveAverage(int &aboveAverage	);
// Post: Number of grades above the average has been calculated
void CalculateBelowAverage(int belowAverage);
// Post: Number of grades below the average has been calculated
void PrintResults(ofstream& outData);
// Pre:  outData has been opened
// Post: Output has been written on outData

int main()
{
  // Declare and open files
  ifstream inData;
  ofstream outData;
  OpenFiles(inData, outData);
  if (!inData || !outData )
  {
    cout << "Files not opened successfully." << endl;
    return 1;
  }
  CalculateStatistics(inData);
  PrintResults(outData);
  inData.close();
  outData.close();
  return 0;
}

//*********************************************************************

// Add the functions here.


void OpenFiles(ifstream & inData, ofstream& outData)
{

	inData.open("grades.txt");
	if(!inData)
	{
		cout<<"Open data fail."<<endl;
	}
	
	outData.open("output.txt");
	if (!outData)
	{
		cout<<"Open output file fail."<<endl;
	}

}
void CalculateStatistics (ifstream& inData)
{
	int sum=0;
	inData.open("grades.txt");
	inData>>numGrades;
	for (int i =0; i<numGrades;i++)
	{
		sum = sum+i;
	}
	cout<<sum;
	
	
}

void InputGrades(ifstream& inData)
{
	for (int i=0;i<numGrades;i++)
	{
		inData>>grades[i];
	}
		
}


void CalculateAverage()
{
	int sum =0;
	float avg;
	for (int i=0; i<101;i++)
	{
		sum = sum+ grades[i];//?
	}
	cout<<"the average is: ";
     avg = sum/101;
	 cout<<avg;
	 avg = average;
}
void CalculateHighest()
{
	for (int i =0; i<numGrades;i++)
	{
		if(grades[i]>highest)
		{
			highest = grades[i];
	    }
	}
	cout<<"the highest value is :"<<highest;
}

void CalculateLowest()
{
	for (int j =0; j<numGrades;j++)
	{
		if(grades[j]<lowest)
		{
			lowest = grades[j];
		}
		cout<<"the lowest grades is :"<<lowest;
     }
}

void CalculateAboveAverage()
{
	for(int k =0; k < numGrades; k++)
	{
		if (grades[k]>average)
		{
			grades[k] = aboveAverage;
			cout<<aboveAverage<<"  ";
			
		}
	}
}
Topic archived. No new replies allowed.