Please help with creating 2 different parallel arrays

The instructions state I must use a 1 dimensional array to read the 1st column of data from a text file.
Then i must use a 2-dimensional array to read the number associated with the names.
Both arrays must be parallel array.

The user must be able to input the Term number, so they can retrieve the number associated with that user. I'm not concerned about this part for now.

My main goal right now, is for the compiler to generate the contents of the text file.

This is what the Data.txt file looks like


adam 21 34
bram 56 23
cole 21 38
dave 15 13


The program will first begin by displaying all that content in the following format:

Name: adam Result 1: 21 Result 2: 34
Name: bram Result 1: 56 Result 2: 23
Name: cole Result 1: 21 Result 2: 38
Name: dave Result 1: 15 Result 2: 13


I did the following code:

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

int main()   
{
	string name[4]; //generate 4 names from file.
	double results[4][3];   //result of each name's session, 4rows 3columns	
	
	int row;  //row counter
	
	ifstream inFile;
			
	inFile.open("Data.txt");
	cout << "The file's data: " << endl;
	
 	for (row = 0; row < 4; row++){  //read the array of Names
		inFile >> name[row];		}
	inFile.close();



	for (row = 0; row < 4; row++){  //display the array
		cout << "Name: " << name[row] << endl; //I'll worry about the results later << "    Result 1: " << results[rR][1] << endl;
	}	
		
	system ("pause");
	return 0;


And the program outputs

Name: adam
Name: 21
Name: 34
Name: bram

Also please, keep in mind, that i'm taking things one step at a time. If i delete the numbers from the text file, all the Name's are able to display properly..

Thanks in advanced.
Last edited on
Anyone know? please
The following was able to execute successfully

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
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()   
{
	const int SUM = 4;

	string name[SUM]; //generate 4 names from file.
	double res0[SUM][2];   //result of each name's session, 4index 3columns	
	double res1[SUM][1], res2[SUM][2]; //Not allowed to use this method

	int index = 0; //index counter
	
	ifstream inFile;
			
	inFile.open("Data.txt");
	cout << "The file's data: " << endl;
	
	//get information
 	for (index; index < SUM; index++){  //read the array of Names
		inFile >> name[index];	
		inFile >> res1[index][1];
		inFile >> res2[index][2];
	}
	
	inFile.close();

	//display information
	for (index = 0; index < SUM; index++){ 
		cout  << "Name: "  <<  name[index] <<  " Result 1: " ;
		cout  << res1[index][1] <<  " Result 2: " ; 
		cout  << res2[index][2]  <<  "\n" ; 
		}
		
	system ("pause");
	return 0;
}


But as you may know, the instructions require i use a 2 dimensional array; IE double res0[SUM][2];
How would i do so?

I was thing double res0[SUM][3] would be more logical that double res0[SUM][2], but the instructions, but the instructions say to use [2], which is odd.


EDIT: Ok i think i may resolved it now to get it to display just as in the text file.
Last edited on
Topic archived. No new replies allowed.