a function that reads two different arrays from a file

Within my code, i have a one-d array that holds 10 students names, while the other array is a 2-d array (is 10x5) and holds the students' score. i'm supposed to be using a function that will pull out both of these arrays. I have the code to pull out the information from the txt file but i'm not sure how to implement it into function

here's my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[..]
inputfile.open("dataIP1.txt");
  for (int count = 0; count < 10; count++)
		{

			getline(inputfile, student_name[count], ',');										


			for (int counter = 0; counter < 5; counter++)
			{
				inputfile >> student_score[count][counter];
				inputfile.get(commas);
				inputfile.get(ending);										
																		
			}
			inputfile.get(ending);

		}
inputfile.close();

also, later on within my code i will have to use these arrays again in order to search for names within the array, i'm not sure how to "save" the array for future reference within my code.
if you would also tell me how the code works too that would be awesome
Last edited on
Hi,
Can you show us what the input file looks like?
Mary Peterson, 95, 93, 77, 94, 77,
Jake Andersen, 90, 90, 95, 93, 49,
Susan Cooper, 79, 94, 44, 90, 73,
Mike Smith, 95, 93, 30, 79, 97,
Jim Blair, 53, 45, 97, 39, 59,
Clark Lee, 70, 95, 45, 39, 77,
Kennedy Davis, 77, 34, 55, 74, 93,
Kim Bronson, 93, 94, 99, 77, 97,
Sunny Hill, 79, 95, 59, 93, 95,
Sam Benson, 95, 75, 49, 75, 73,
To read the data from the file you can create a function like this:
1
2
3
4
5
6
void readStudentData (ifstream& inputfile, string student_name[10], int student_score[10][5])
{
   // your code to read the data
   // names into student_name
   // score into student_score.
}


In main you declare your variables and pass them to the function
1
2
3
4
5
string student_names[10];
  int student_scores[10][5];
  ifstream inputfile("dataIP1.txt");
  // check if file is opened
  readStudentData (inputfile, student_names, student_scores);


There is no need to save the array. If it is in main() it will be there as long as the program is running and you can easily pass it to a function to search or print it.
Try this :
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
// function main()
int count = 0;
string student_name[10];
double student_score[10][5];

ifstream inputfile("dataIP1.txt");

if(!inputfile.is_open())
cout << "Error : dataIP1.txt not found" << endl;

for (int k = 0; k < 10; k++)
if(getline(inputfile, student_name[count], ','))				
{ 
    for (int i = 0; i < 5; i++)
    {
        char delimiter;
        inputfile >> student_score[count][i];
        inputfile >> delimiter;
    }
    inputfile >> ws;
    count++;
}
inputfile.close();

cout << "Print all contents : " << endl;
for (int k = 0; k < count; k++)
{
    cout << student_name[k] << ", ";
    for (int i = 0; i < 5; i++)
    {
        cout << student_score[k][i];
        if(i < 4) cout << ", ";
     }
    cout << endl;
}
Last edited on
Does that help you? :)
I edited my solution. Now everything works fine.
The program should run fine with :
Mary Peterson, 95, 93, 77, 94, 77,
Jake Andersen, 90, 90, 95, 93, 49,
Susan Cooper, 79, 94, 44, 90, 73,
Mike Smith, 95, 93, 30, 79, 97,
Jim Blair, 53, 45, 97, 39, 59,
Clark Lee, 70, 95, 45, 39, 77,
Kennedy Davis, 77, 34, 55, 74, 93,
Kim Bronson, 93, 94, 99, 77, 97,
Sunny Hill, 79, 95, 59, 93, 95,
Sam Benson, 95, 75, 49, 75, 73,
ah okay i see my problem, i didn't bother to pass the ifstream into the function, thank you both so much!
Last edited on
Glad it helped :)
Topic archived. No new replies allowed.