Sep 19, 2019 at 8:43am Sep 19, 2019 at 8:43am UTC
I'm currently writing a program that consists of displaying students' ID, Name, Course, Credit, and Score. The data is all in this text file:
"StudentRecords.txt"
1 2 3 4 5 6 7 8 9 10 11 12
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):
1 2 3 4 5 6
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:
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
#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;
}
else if () // possibility of error in current output
{
}
else if () // possibility of error in current output
{
}
else if (i == 0)
{
cout << studArr[i].ID << " " << studArr[i].Name << 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 << endl;
}
cout << "======================\nGPA "
<< round((dividend / divisor)) << endl;
cout << endl;
return 0;
}
My Current Output:
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
12546 Amy
CS1 81 3
CS2 90 4
CS3 90 4
======================
GPA 4
CS1 76 2
======================
GPA 3
CS2 85 3
======================
GPA 3
CS3 75 2
======================
GPA 3
CS1 64 1
======================
GPA 3
CS2 71 2
======================
GPA 3
CS3 69 1
======================
GPA 2
CS3 80 3
======================
GPA 3
CS3 45 0
======================
GPA 2
Expected Output:
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
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
Last edited on Sep 20, 2019 at 10:52pm Sep 20, 2019 at 10:52pm UTC
Sep 19, 2019 at 8:47am Sep 19, 2019 at 8:47am UTC
Looks like every time you output an
endl
, you should have output TWO of them.
So do that. Everywhere you output an
endl
, add another.
Looks like every time you ouput gradepoints, you need it to be to one decimal place.
One way:
https://stackoverflow.com/questions/5907031/printing-the-correct-number-of-decimal-points-with-cout
Another way (since the gradepoints is always an integer value):
1 2 3 4
divisor += studArr[i].Credit;
cout << studArr[i].Course << " "
<< studArr[i].Score << " "
<< gradepoints << ".0" << endl;
Last edited on Sep 19, 2019 at 8:50am Sep 19, 2019 at 8:50am UTC
Sep 19, 2019 at 9:07am Sep 19, 2019 at 9:07am UTC
That seemed to fix my gradepoints error and the structure of the output. Now I just need to figure out how to display the other students' ID, Name, Course, Credit, and Score as well as their GPA
Thanks for your help so far @Repeater!
My Updated Code:
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
#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" ;
string token;
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 Updated Output:
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
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
Last edited on Sep 19, 2019 at 9:10am Sep 19, 2019 at 9:10am UTC