Question: /* Learn how to use struct to load and manipulate data. The input file is named StudentRecords.txt. (The file is also uploaded.) Step 1: In
/*
Learn how to use struct to load and manipulate data. The input file is named StudentRecords.txt. (The file is also uploaded.)
Step 1: In the while loop of the program instantiate a student struct object. In each iteration, the system should read 5 pieces
of data, which are student ID, name, course, credit and score. Save the data in the Student object. Use cout to display
the values of the object. Carefully examine the output and you can see that there is an extra line of output at the bottom.
(That is, the last two lines are the same, or almost the same.) What cause the problem to occur? Use the debugger to trace
the program and figure out what causes the bug.
Step 2: Let us assume the maximum amount of the students is 999. Since there are hundreds of students, we should use another
data structure to store the student records, so that we can manipulate the data. What data structure could be a nice choice
to use so that all data can be properly stored?
Step 3: After storing the data in the data structures, use the following table to calculate the GPA of each student.
Range Grade
======== =====
90 -- 100 ---> 4.0
80 -- 89 ---> 3.0
70 -- 79 ---> 2.0
60 -- 69 ---> 1.0
0 -- 59 ---> 0.0
We should take the credit of the course into account. For example, the following equation is how we calculate Amy's GPA.
(3.0 * 4 + 4.0 * 4 + 4.0 *3)/(4 + 4 + 3) = 3.64
Step 4: Course listing and GPA for a student should consist of three parts:
(1) ID and name,
(2) A listing of all courses, and
(3) The calculated GPA
For example, Amy's report should look like this:
12546 Amy
CS1 4 81 3.0
CS2 4 90 4.0
CS3 3 90 4.0
======================
GPA 3.64
The last column is the converted grades calculated from the scores.
Step 5: The output should display all students' courses and GPAs.
*/
#include
#include
using namespace std;
struct Student
{
int ID;
string Name;
string Course;
int Credit;
int Score;
};
int main()
{
fstream inputFile;
string fileName = "StudentRecords.txt";
string token;
inputFile.open(fileName.c_str(), ios::in);
if (inputFile.is_open())
{
while(!inputFile.eof())
/*
Step 1: In the while loop of the program instantiate a student struct object. In each iteration, the system should read 5 pieces
of data, which are student ID, name, course, credit and score. Save the data in the Student object. Use cout to display
the values of the object. Carefully examine the output and you can see that there is an extra line of output at the bottom.
(That is, the last two lines are the same, or almost the same.) What cause the problem to occur? Use the debugger to trace
the program and figure out what causes the bug.*/
{
Student info;
inputFile >>info.ID;
cout << info.ID;
inputFile >> info.Name;
cout << info.Name;
inputFile >> info.Course;
cout << info.Course;
inputFile >> info.Credit;
cout << info.Credit;
inputFile >> info.Score;
cout << info.Score;
inputFile >> token;
cout << token << endl;
}
inputFile.close();
}
else
cout << "File cannot be opened.";
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
