Your classmate has started the following C program to read a
Your classmate has started the following C++ program to read a le of data on 100 students
from le. Because keeping track of four arrays is getting complicated, he is not sure how to
proceed with the computations to be performed on the data.
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream input_file(\"student_data.txt\");
const int SIZE = 100;
string names[SIZE];
int uids[SIZE];
double gpas[SIZE];
int num_of_courses[SIZE];
for(int i = 0; i < SIZE; i++) {
input_file >> names[i] >> uids[i] >> gpas[i] >> num_of_courses[i];
}
// not sure how to proceed from here . . .
}
Help your classmate simplify the program by using just one array of structures. Rewrite the
program below.
Thanks
Solution
Hi,
I have updated the code with one array of structures. Highlighted the code changed below.
#include <iostream>
#include <fstream>
using namespace std;
struct STUDENT_DATA
{
string name;
int uids;
double gpas;
int num_of_courses;
} ;
int main() {
ifstream input_file(\"student_data.txt\");
const int SIZE = 100;
STUDENT_DATA data[SIZE];
//string names[SIZE];
//int uids[SIZE];
//double gpas[SIZE];
//int num_of_courses[SIZE];
for(int i = 0; i < SIZE; i++) {
input_file >> data[i].name >> data[i].uids >>data[i].gpas >> data[i].num_of_courses;
}
}
