Text file into arrays

Hello,
So I have to write a program which reads the text files into the arrays, and then is able to use the students' data to sort, and calculate the averages, etc. Right now all I've been trying to do for the past few hours is trying to read and output the txt file's data into the arrays, which I will then use to sort and do other stuff.

this is the assignment:
This program will create a system to report and analyze student scores. I have stored 15 students’ recordes
including SSN, first name, last name and score in a data file called “students.txt”. Your program first
reads the student information from this file, and then uses four global parallel arrays to store the student
information. You are required to use an int array to store SSNs, a string array to store first names, a
string array to store last names, and a double array to store scores.

The text file is students.txt and this is the information:
628130189 Paul James 92
237698211 Daniel Cook 86
201895367 Mellisa Garza 78
491066285 Jessica Barbara 62
168606868 Elizabeth Bruce 90
378205732 Sarah Lee 91.5
118453900 David Brian 87
583192186 Cody Garza 92
226665118 Gage Lewis 78
175382843 James Collins 69.5
816231095 Ann White 88.5
376651608 Mark Jackson 72
508234567 Mark Freeman 86
763211099 Jack William 52
286204723 John Rodrguez 69.5

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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

void mainmenu();
void printInformation(ifstream inFile, string students[], int size);
void highestScore(const double [], int size);
void lowestScore(const double [], int size);
void averageScore(const double [], int size);


int main()
{
	int choice;
	int SID[15];
	char fname [15][30];
	char lastName[15][30];
	double scores[15];
	
	ifstream inFile;
	ofstream outFile("students.txt"); 
    inFile.open("students.txt");
	
	if (!inFile)
	{
		cout << "File not found!" << endl;
		return 1;
	} 
	
	while(cin >> SID >> fname >> lastName >> scores)
	{
    	outFile << SID << " " << lastName << " " << fname << " " << scores << endl;
  	}
	
	
	//string students = "students.txt";
	//infile.open(students.c_str());
	//getline(cin, students);
	//cout << students;*/

	do
	{
		mainmenu();
		cin >> choice;
		cout << endl;
	
	
	switch (choice)
	{
		case 'L':
		case 'l':
		case 1: printInformation(SID, firstName, lastName, scores); break;
		case 'H':
		case 'h':
		case 2: highestScore(scores, 15); break;
		case 'O':
		case 'o':
		case 3: lowestScore(scores, 15); break;
		case 'A':
		case 'a':
		case 4:	averageScore(); break;
		case 0: break;
		default: 
			cout << "Wrong choice!" << endl;
	}	
	}
	while (choice!=0);
	
	system("PAUSE");
	return 0;
}

void mainmenu()
{
	cout << "Main Menu" << endl;
	cout << "1. List students' information (L)" << endl;
	cout << "2. find the Highest score (H)" << endl;
	cout << "3. find the Lowest score (O)" << endl;
	cout << "4. calculate the Average score (A)" << endl;
	cout << "5. sort students by SSN (S)" << endl;
	cout << "6. sort students by Name (N)" << endl;
	cout << "7. sort students by Score (C)" << endl;
	cout << "0. Exit" << endl;
	cout << "Please select one option: ";
}

void printInformation(ifstream &inFile, string students[15], int size)
{
	cout << right << setw(6) << "SSN" << setfill(' ') << right << setfill(' ');
    cout << setw(5) << " " << setfill(' ');
    cout << left << setw(10) << "Last-Name" << setfill(' ');
    cout << left << setw(11) << "First-Name" << setfill(' ');
    cout << right << setw(5) << "Score" << setfill (' ') << endl;
}

Last edited on
First I would declare the arrays as in the instructions:

Your program first reads the student information from this file, and then uses four global parallel arrays to store the student information. You are required to use
- an int array to store SSNs,
- a string array to store first names,
- a string array to store last names, and
- a double array to store scores.


Then I would write a function LoadFile(string filename) that loads the data and puts it in to the arrays.

your main() would look like 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
36
37
38
39
40
41
42
43

#define NUM_STUDENTS 15

int SSN[ NUM_STUDENTS];
string FirstNames[ NUM_STUDENTS];
string LastNames[ NUM_STUDENTS];
double Scores[ NUM_STUDENTS];

int main()
{
	LoadFile("students.txt");
	do
	{
		mainmenu();
		cin >> choice;
		cout << endl;
	
	
	switch (choice)
	{
		case 'L':
		case 'l':
		case 1: printInformation(SID, firstName, lastName, scores); break;
		case 'H':
		case 'h':
		case 2: highestScore(scores, 15); break;
		case 'O':
		case 'o':
		case 3: lowestScore(scores, 15); break;
		case 'A':
		case 'a':
		case 4:	averageScore(); break;
		case 0: break;
		default: 
			cout << "Wrong choice!" << endl;
	}	
	}
	while (choice!=0);
	
	system("PAUSE");
	return 0;
}


Next you need to write the function to output all the student data - as a test to see if you load all data.

1
2
3
4
void printInformation()
{
  // output all the data from the arrays
}


Topic archived. No new replies allowed.