Reading from a file and putting it into an array

It reads the file but it isn't taking the numbers in. Also, where I am taking the data, is there a simple way to do it instead of all the ones in a row? The text file looks like this:

123456789 77.2 88.3 22 28 35 45 33 35 40
234567890 97.5 90 25 30 38 48 34 35 50
345678901 82.4 77.5 22.5 27 35.5 44 35 33 48
456789012 65.5 67 20 25 28 40 27 25 35
567890123 79.5 82 25 30 32 47 30 33 46
678901234 90 86.5 25 30 40 46 34 35 50
789012345 77 82.2 25 30 36 46 33 32.5 47
890123456 67.7 72.5 23 28 35 44 30 30 44
901234567 76.5 83.4 25 29 38 49 35 35 49


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
  #include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
using namespace std;

struct SStudent
{
	int id;
	double test, hw;
};

int getInput(SStudent [], int);
void process(SStudent [], int);
void display(const SStudent[], int);

int main()
{
	SStudent length[30];
	int i;

	getInput(length, 30);
}

int getInput(SStudent students[], int size)
{
	ifstream inFile;
	inFile.open("scores.txt");

	if(!inFile)
	{
		cout << "Can't open input file\n";
		_getch();
		exit(1);
	}

	for (int i = 0; i < size; ++i)
	{
		cin >> students[i].id >> students[i].test >> students[i].test 
			>> students[i].hw >> students[i].hw >> students[i].hw 
			>> students[i].hw >> students[i].hw >> students[i].hw >> students[i].hw;
	}

	return size;
}
So each line is a student number, then 2 test scores, then 7 homework scores? You need to expand your struct. You currently only have room for 1 test score and 1 homework. Make test two variables, test1 and test2, and make homework an array with 7 elements.

In your for loop, you're using cin >>, but trying to get data from the file. This will promt the user for input instead. You need to use inFile >>.
So this would be correct?

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

struct SStudent
{
	int id;
	double test1, test2, hw [7];
};

int getInput(SStudent [], int);
void process(SStudent [], int);
void display(const SStudent[], int);

int main()
{
	SStudent length[30];
	int i;

	getInput(length, 30);
}

int getInput(SStudent students[], int size)
{
	ifstream inFile;
	inFile.open("scores.txt");

	if(!inFile)
	{
		cout << "Can't open input file\n";
		_getch();
		exit(1);
	}

	for (int i = 0; i < size; ++i)
	{
		inFile >> students[i].id >> students[i].test1 >> students[i].test2 
			>> students[i].hw[7] >> students[i].hw[7] >> students[i].hw[7] 
			>> students[i].hw[7] >> students[i].hw[7] >> students[i].hw[7] >> students[i].hw[7];
	}

}
The first thing I see is that the struct SStudent should be declaring an array on the two tests double test[2] and an array on the hw (homework) double hw[7].

Try something like this. Removing the for loop.

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
	ifstream inFile;
	inFile.open("scores.txt");

	if(!inFile)
	{
		cout << "Can't open input file\n";
		_getch();
		exit(1);
	}
	/*
	for (int i = 0; i < size; ++i)
	{
		cin >> students[i].id >> students[i].test >> students[i].test 
			>> students[i].hw >> students[i].hw >> students[i].hw 
			>> students[i].hw >> students[i].hw >> students[i].hw >> students[i].hw;
	}
	*/
	int count = 0; 
        while(!inFile.eof())
        {
          // temp hold variables
           int tmpid;
           double tmptest[2], tmphw[7];
           
		   SStudent temp;

		   //load the temp variables 
                           inFile>>tmpid>>
			   tmptest[0]>>tmptest[1]>>
			   tmphw[0];tmphw[1];tmphw[2];tmphw[3];tmphw[4];tmphw[5];tmphw[6];

			   temp.id = tmpid; //id
			 
			   temp.test[0] = tmptest[0]; //tests
			   temp.test[1] = tmptest[1];

			   temp.hw[0] = tmphw[0]; // homeworks - etc etc etc

			   students[count] = temp;
			   
			   count++;

			   if(count = size - 1)
			   {
				   break;
			   }
		
	}


Last edited on
Topic archived. No new replies allowed.