Reading different data types from a text file and storing into a 2d array.

So I have a text file set up as such:

Johnson 5 6 7 1 9
Abby 5 3 9 7 1
Joe 99 5 7 8 1

I want to store the names in a one dimensional array and the numbers (test scores) in a two dimenstion array. I noticed that I will have to iterate through the test scores faster than the names, which I cant quite figure out how to do. Hers what I have:

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

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

int main(){
    const int numStudents = 5;
    const int numTests = 5;
    
    string names[numStudents];
    //char grades[numStudents];
    int scores[numStudents][numTests];
    
    ifstream inFile;
    inFile.open("/Users/kylemiller/Desktop/Ch9Ex11.txt");
        
    for (int i = 0; i < numStudents; i++)
        inFile >> names[i] >> scores[i][0];
    
    for (int i = 0; i < numStudents; i++)
        cout << names[i] << endl;
    
    for (int i = 0; i < numStudents; i++)
        for (int j = 0; j < numTests; j++)
            cout << setw(5) << scores[i][j];
    
    return 0;
}


The problem I am having is with the inFile. I cant set up separate loops because it will iterate completely through the name loop before getting to the score loop, and I don't think its possible to go cout << names[i] << for (blah blah blah), since you cant put a for loop there.
I think you need to put the whole thing in a while loop.
So while(infile);
then for loops.


I tried to work on it but I can't get it to read all the test scores.
I don't know how to do multi dimensional arrays.

Here's the code; see if it helps:
I included cout statements in the loops to see what was getting placed in the variables.

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

int main()
{
  string students[5];
  int tests[5];
  ifstream infile;
  int n=0;

  infile.open("names");

  infile >> students[n];
  while (infile)
    {
      cout << "Location is " << n << endl;
      cout << "Student is " << students[n] << endl;
      for (int i =0; i < 5; i++)
	{
	  infile >> tests[n];
	  cout << "Test is " << tests[n] << endl;
	}
      n++;
      infile >> students[n];
    }
  
  infile.close();
  for (int i=0; i < n; i++)
    cout << students[i]  << " " << tests[i] << endl;
    
  return 0;
}


Sample Input:
Johnson 5 6 7 1 9
Abby 5 3 9 7 1
Joe 99 5 7 8 1
Dave 1 2 3 4 5


Sample Output:
Location is 0
Student is Johnson
Test is 5
Test is 6
Test is 7
Test is 1
Test is 9
Location is 1
Student is Abby
Test is 5
Test is 3
Test is 9
Test is 7
Test is 1
Location is 2
Student is Joe
Test is 99
Test is 5
Test is 7
Test is 8
Test is 1
Location is 3
Student is Dave
Test is 1
Test is 2
Test is 3
Test is 4
Test is 5
Johnson 9
Abby 1
Joe 1
Dave 5


Sample Output Without Cout Statements:
Johnson 9
Abby 1
Joe 1
Dave 5


It seems that my test array only holds the last variable.
Not sure why.


Last edited on
Topic archived. No new replies allowed.