Filling an array

Apr 3, 2009 at 2:40am
My instructions are to create a data file with some test scores. The problem is I cant figure out how to get the input file to fill the arrays.
Assume there are only 3 students and 5 tests for each of the students.
Each row represents the test scores of a student. Each student's scores is in different row.
80.0 90.0 50.0 45.0 34.5
77.0 76.0 76.6 50.6 90.0
34.9 24.1 66.0 70.2 60.2
create a program that asks for an input file that has the structure above. Read the data into arrays (one array for a student) of floats. Manipulate the data by loops. Then average the scores of each students test then average each test. This is what I have its not much but I don't need it solved just pointed into the right direction.

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
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main(){

	char input_file[16];
	int a[4], b[4], c[4], i;
	ifstream fin;
	cout<<"Enter a file containing students test scores\n";
	cin>>input_file;

	fin.open(input_file);
	if(fin.fail()){
	cout<<"Input file opening failed!\n";
	exit(1);
	}
	fin>>a[i];
	fin>>b[4];
	fin>>c[4];
	int sum=0;
	for(int i=0;i<5;i++){
		sum=i+a[i];
		cout<<sum;
		}
	return 0;
}
Last edited on Apr 3, 2009 at 2:41am
Apr 3, 2009 at 4:56am
I would use std::string instead of a char[], because if I enter, say, 20 characters, you program will be accessing bad memory.
Your a,b,c arrays are arrays on ints, you want them to be doubles (since you are reading doubles).
i is never defined, a[i] could be really bad.
You did c[4], but are accessing element 4, bad. c[4] gives you c[0]-c[3].
You will want a (for) loop to fill all the arrays, not just a single statement.
Apr 3, 2009 at 5:15am
Would the for loop look like this?
1
2
for( int i=0; i<5;i++)
             fin>>a[i];

and then the same loop for b[] and c[]. I tried something to that effect earlier and got some crazy numbers like the loop wasn't working. Thanks for the help.
Apr 3, 2009 at 6:04am
This is where I am at, it will show the first number of the set then it gives random numbers.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
for(int i=0;i<5;i++){
		fin>>a[i];
		for(int j=0;j<5;j++){	
			fin>>b[j];
			for(int k=0;k<5;k++){
				fin>>c[k];
			}
		}
	}
	int i, j, k;
	double student1,sum;
	for( i=0;i<5;i++){
	cout<<a[i]+i<<endl;;
	}
Apr 5, 2009 at 1:43pm
I am still stuck here I have tried everything I know.
Apr 5, 2009 at 1:48pm
your for loops are nested within each other
Apr 5, 2009 at 9:33pm
So if I close them before each loop the arrays will fill correctly? I am still not sure if the for loop is correct. Thanks for the advice.
Apr 6, 2009 at 12:52am
It will compile when running the program but the only number that comes out correct is student3. Cant figure it out. Thanks for the help.

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 <cstdlib>
using namespace std;

int main(){

	char input_file[16];
	double a[4], b[4], c[4];
	ifstream fin;
	cout<<"Enter a file containing students test scores\n";
	cin>>input_file;

	fin.open(input_file);
	if(fin.fail()){
	cout<<"Input file opening failed!\n";
	exit(1);
	}
	do{

	for(int i=0;i<5;i++){
		fin>>a[i];
	}
	for(int i=0;i<5;i++){
		fin>>b[i];
	}
	for(int i=0;i<5;i++){
		fin>>c[i];
	}
	}while(!fin.eof());
		
	double student1=0, student2=0, student3=0, test;	
		for(int i=0;i<5;i++){	
			student1=student1+a[i];
		}
		for(int i=0;i<5;i++){
			student2=student2+b[i];
		}
		for(int i=0;i<5;i++){
			student3=student3+c[i];
		}
		
	cout<<student1<<" ";		
	cout<<student2<<" ";
	cout<<student3<<" ";

	return 0;
}
Apr 6, 2009 at 12:54am
these are the numbers I get after running the program.

275 353.4 255.4
Apr 6, 2009 at 1:17am
your arrays are only created at size 4. Size 5 with have 5 elements, 0 through 4.
Apr 6, 2009 at 1:45am
Man, its always the small things that get you. Thanks for the help.
Topic archived. No new replies allowed.