I'm currently writing a program that consists of outputing students' ID, Name, Course, Credit, and Score. The data is all in this text file:
"StudentRecords.txt"
12546 Amy CS1 4 81
13455 Bill CS1 4 76
14328 Jim CS1 4 64
14388 Henry CS3 3 80
15667 Peter CS3 3 45
12546 Amy CS2 4 90
13455 Bill CS2 4 85
14328 Jim CS2 4 71
12546 Amy CS3 3 90
13455 Bill CS3 3 75
14328 Jim CS3 3 69
The following table was used to calculate the GPA(just a reference):
Range Grade:
90 -- 100 > 4.0
80 -- 89 > 3.0
70 -- 79 > 2.0
60 -- 69 > 1.0
0 -- 59 > 0.0
The problem I'm having right now is my output. I'm trying to get it to match my expected output, but I can't seem to figure it out.
It probably has to do with some missing else if() statements in the second for loop. If anyone can provide me some advice/hints on how to get my input to work and display my expected output, I would appreciate it!
My Current Code:
#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
struct Student
{
int ID = -1;
string Name = "";
string Course = "";
int Credit = -1;
int Score = -1;
};
const int SIZE = 99;
int main()
{
ifstream inputFile;
string fileName = "StudentRecords.txt";
Student studArr[SIZE];
inputFile.open(fileName.c_str(), ios::in);
int n = 0;
if (inputFile.is_open())
{
while(!inputFile.eof())
{
Student st;
inputFile >> st.ID;
inputFile >> st.Name;
inputFile >> st.Course;
inputFile >> st.Credit;
inputFile >> st.Score;
studArr[n] = st;
n++;
}
inputFile.close();
}
else
{
cout << "File cannot be opened.";
return 1;
}
// sorts the array by ID and Course
for (int i = 0; i < n; i++)
{
for(int j = i + 1; j < n; j++)
{
if(studArr[i].ID > studArr[j].ID)
{
Student temp = studArr[i];
studArr[i] = studArr[j];
studArr[j] = temp;
}
else if(studArr[i].Course > studArr[j].Course)
{
Student temp = studArr[i];
studArr[i] = studArr[j];
studArr[j] = temp;
}
}
}
int check = 0;
float dividend = 0;
float divisor = 0;
for(int i = 0; i < n; i++)
{
if(studArr[i].ID != studArr[check].ID)
{
cout << "======================\nGPA "
<< round((dividend / divisor)) << endl << endl;
}
else if(i == 0)
{
cout << studArr[i].ID << " " << studArr[i].Name
<< endl << endl;
dividend = 0;
divisor = 0;
}
float gradepoints;
if(studArr[i].Score < 60)
{
gradepoints = 0.0;
}
else if(studArr[i].Score < 70)
{
gradepoints = 1.0;
}
else if(studArr[i].Score < 80)
{
gradepoints = 2.0;
}
else if(studArr[i].Score < 90)
{
gradepoints = 3.0;
}
else if(studArr[i].Score < 100)
{
gradepoints = 4.0;
}
dividend += gradepoints * studArr[i].Credit;
divisor += studArr[i].Credit;
cout << studArr[i].Course << " "
<< studArr[i].Score << " "
<< gradepoints << ".0" << endl << endl;
}
cout << "======================\nGPA "
<< round((dividend / divisor)) << endl << endl;
cout << endl << endl;
return 0;
}
My Current Output:
12546 Amy
CS1 81 3.0
CS2 90 4.0
CS3 90 4.0
======================
GPA 4
CS1 76 2.0
======================
GPA 3
CS2 85 3.0
======================
GPA 3
CS3 75 2.0
======================
GPA 3
CS1 64 1.0
======================
GPA 3
CS2 71 2.0
======================
GPA 3
CS3 69 1.0
======================
GPA 2
CS3 80 3.0
======================
GPA 3
CS3 45 0.0
======================
GPA 2
Expected Output:
12546 Amy
CS1 4 81 3.0
CS2 4 90 4.0
CS3 3 90 4.0
======================
GPA 3.64
======================
13455 Bill
CS1 4 76 2.0
CS2 4 85 3.0
CS3 3 75 2.0
======================
GPA 2.36
======================
14328 Jim
CS1 4 64 1.0
CS2 4 71 2.0
CS3 3 69 1.0
======================
GPA 1.36
======================
14388 Henry
CS3 3 80 3.0
======================
GPA 3
======================
15667 Peter
CS3 3 45 0.0
======================
GPA 0