Test Average Program

Trying to make a test average program that gets data from a file(names and score), puts it in an array and calculates using data from the array. How do I access the file and read the data?

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void getData();
double calcAvg();
double calcAvgTest();
 
int main()
{
	const int students = 20;
	const int exams = 5;

	int studentGrades[students][exams];
	



	getData();

         calcAvg();
         calcAvgTest();
	system("PAUSE");
}
void getData()
{
	string name;
	ifstream inFile("namesAndScores");
	inFile.open("namesAndScores");
	inFile >> name;
	name = "John";
	
	const int students = 20;
	const int exams = 5;

	int studentGrades[students][exams];
	inFile.close;

}
double calcAvg()
{
	double test1, test2, test3, test4, test5;
	double average;

	average = (test1 + test2 + test3 + test4 + test5) / 5.0;
	
	
}
double calcAvg2()
{

}
Last edited on
Line 30: You don't need to open the file if you supplied a name to the ifstream constructor.

Line 32: You're overlaying the name you just read in.

Line 37: This array shadows the array at line 15. Any changes to this array won't be reflected in the array in main. You need to pass the array in main as an argument.

Lines 31-38: You need a loop to read the records from the file.

Line 43: These are uninitialized variables. You calculation at line 46 is just going to compute garbage. You don;t want to be using local variables here. You want to be using the data you read in.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.


Last edited on
I need to pass the data from the file to arrays, what loop do I use?

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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void getData();
double calcAvg();
double calcAvgTest();
 
int main()
{
	const int students = 20;
	const int exams = 5;

	int studentGrades[students][exams];
	string name;
	double scores;
	ifstream inFile("namesAndScores");



	getData();

	system("PAUSE");
}
void getData()
{
	
	inFile >> name;
	inFile >> scores;
	
	const int students = 20;
	const int exams = 5;

	int studentGrades[students][exams];
	

}
double calcAvg()
{

	
	
}
double calcAvg2()
{

}
Topic archived. No new replies allowed.