Need some quick help. (structs)
can anyone tell me why my STU.GPA calculations always end up as "114"
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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
|
// ------------------------------------------------------------------
// File name: academicsA.cpp
// Assign ID: PROG3a_Academics
// Due Date: 02/02/13 at 11pm
//
// Purpose: Give student experience working with user-defined struct data types.
//
// Author: bfields Byron Fields
//
// ------------------------------------------------------------------
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
// ----------------------------------------------------------------------
// Declare variables
// ----------------------------------------------------------------------
int Acount = 0;
int Bcount = 0;
int Ccount = 0;
int Dcount = 0;
int Fcount = 0;
int GPAa = 0;
struct STUDENT
{
long StudID; // 9-digit number.
string Last;
string First;
int CumHours;
float GPA;
};
STUDENT STU;
struct ENROLLMENT
{
long StudID;
string Semester;
string CrsPrefix;
int CrsNumber;
int CreditHrs;
char Grade;
};
ENROLLMENT E1, E2, E3;
//-| ----------------------------------------------------------------------
//-| Print the copyright notice declaring authorship.
//-| ----------------------------------------------------------------------
cout << endl << "(c) 2013, bfields Byron Fields" << endl << endl;
//-| ----------------------------------------------------------------------
//-| 1. Read student data into a STUDENT record.
//-| ----------------------------------------------------------------------
cin >> STU.StudID >> STU.Last >> STU.First; // Input data sequence: StudID, Lastname, Firstname
//-| ----------------------------------------------------------------------
//-| 2. Read three ENROLLMENT records.
//-| ----------------------------------------------------------------------
cin >> E1.Semester >> E1.CrsPrefix >> E1.CrsNumber >> E1.CreditHrs >> E1.Grade; // Input data sequence: Semester CrsPrefix CrsNumber CreditHrs Grade
E1.StudID = STU.StudID; // NOTE: Copy the StudID from the STUDENT record into each ENROLLMENT record.
cin >> E2.Semester >> E2.CrsPrefix >> E2.CrsNumber >> E2.CreditHrs >> E2.Grade; // Input data sequence: Semester CrsPrefix CrsNumber CreditHrs Grade
E2.StudID = STU.StudID; // NOTE: Copy the StudID from the STUDENT record into each ENROLLMENT record.
cin >> E3.Semester >> E3.CrsPrefix >> E3.CrsNumber >> E3.CreditHrs >> E3.Grade; // Input data sequence: Semester CrsPrefix CrsNumber CreditHrs Grade
E3.StudID = STU.StudID; // NOTE: Copy the StudID from the STUDENT record into each ENROLLMENT record.
//-| ----------------------------------------------------------------------
//-| 3. Compute the #hours student has taken.
//-| ----------------------------------------------------------------------
STU.CumHours += E1.CreditHrs + E2.CreditHrs + E3.CreditHrs;
//-| ----------------------------------------------------------------------
//-| 4. Compute the GPA based on course grade and credit hours.
//-| ----------------------------------------------------------------------
if(E1.Grade == 'A')
{
Acount += 4 * E1.CreditHrs;
}
else if(E1.Grade == 'B')
Bcount += 3 * E1.CreditHrs;
else if(E1.Grade == 'C')
Ccount += 2 * E1.CreditHrs;
else if(E1.Grade == 'D')
Dcount += 1 * E1.CreditHrs;
else
Fcount += 1;
GPAa += (Acount + Bcount + Ccount + Dcount);
Acount = 0;
Bcount = 0;
Ccount = 0;
Dcount = 0;
if(E2.Grade == 'A')
{
Acount += 4 * E2.CreditHrs;
}
else if(E2.Grade == 'B')
Bcount += 3 * E2.CreditHrs;
else if(E2.Grade == 'C')
Ccount += 2 * E2.CreditHrs;
else if(E2.Grade == 'D')
Dcount += 1 * E2.CreditHrs;
else
Fcount += 1;
GPAa += (Acount + Bcount + Ccount + Dcount);
Acount = 0;
Bcount = 0;
Ccount = 0;
Dcount = 0;
if(E3.Grade == 'A')
{
Acount += 4 * E3.CreditHrs;
}
else if(E3.Grade == 'B')
Bcount += 3 * E3.CreditHrs;
else if(E3.Grade == 'C')
Ccount += 2 * E3.CreditHrs;
else if(E3.Grade == 'D')
Dcount += 1 * E3.CreditHrs;
else
Fcount += 1;
GPAa += (Acount + Bcount + Ccount + Dcount);
STU.GPA = GPAa/STU.CumHours;
//-| ----------------------------------------------------------------------
//-| 5. Print the STUDENT record in the format below:
//-| ----------------------------------------------------------------------
cout << left << setw(10) << "STUDENT:" << left << setw(9) << STU.StudID << right << setw(12) << STU.First << ' ' << left << setw(12) << STU.Last << right << setw(3) << STU.CumHours << right << setw(4) << fixed << setprecision(3) << STU.GPA << endl;
//-| ----------------------------------------------------------------------
//-| 6. Print the three ENROLLMENT records in the format below:
//-|
//-| COURSES TAKEN <== Header
//-| ============= <== Header
//-| <== Header
//-| STUDENTID^^SEMESTER^^PREFIX^^NUMBER^^HOURS^^GRADE <== Header
//-| =========^^========^^======^^======^^=====^^===== <== Header
//-| xxxxxxxxx xxxxxxxx xxx xxxx xx x <== Data
//-| xxxxxxxxx xxxxxxxx xxx xxxx xx x
//-| xxxxxxxxx xxxxxxxx xxx xxxx xx x
//-| ----------------------------------------------------------------------
cout << "COURSES TAKEN" << endl;
cout << "=============" << endl;
cout << endl;
cout << "STUDENTID SEMESTER PREFIX NUMBER HOURS GRADE" << endl;
cout << "========= ======== ====== ====== ===== =====" << endl;
cout << left << setw(9) << E1.StudID << " " << left << setw(8) << E1.Semester << " " << left << setw(6) << E1.CrsPrefix << " " << left << setw(6) << E1.CrsNumber << " " << right << setw(5) << E1.CreditHrs << " " << E1.Grade << endl;
cout << left << setw(9) << E2.StudID << " " << left << setw(8) << E2.Semester << " " << left << setw(6) << E2.CrsPrefix << " " << left << setw(6) << E2.CrsNumber << " " << right << setw(5) << E2.CreditHrs << " " << E2.Grade << endl;
cout << left << setw(9) << E3.StudID << " " << left << setw(8) << E3.Semester << " " << left << setw(6) << E3.CrsPrefix << " " << left << setw(6) << E3.CrsNumber << " " << right << setw(5) << E3.CreditHrs << " " << E3.Grade << endl;
//-| ----------------------------------------------------------------------
//-| Print the copyright notice declaring authorship again.
//-| ----------------------------------------------------------------------
cout << endl << "(c) 2013, bfields Byron Fields" << endl << endl;
return 0;
}//main
|
Don't forget to add the string library.
Consider using a switch statement when checking for grades:
1 2 3 4 5 6 7 8
|
switch(E3.Grade)
{
case 'A':
Acount += 4 * E3.CreditHrs;
break;
case 'B':
etc..
|
I tried the switch statements and it gave me "104" this time.
Do you think there is something off about my calculations?
Topic archived. No new replies allowed.