calculator program

can anyone tell me why my GPA comes out wrong?

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
// ------------------------------------------------------------------
// File name:   calculateGPA.cpp
//
// Assign ID:   PROG11
//
// Due Date:    March 29 at 11pm
//
// Purpose:     Modify the mygpa program from assignment PROG3.
//              Write a program to calculate GPA based on the number
//              of As, Bs, Cs, Ds and Fs earned by a student. Also 
//              calculate the number of credit hours taken.
//
// Author:      bfields Byron Fields
//
// ------------------------------------------------------------------

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
   //-| ----------------------------------------------------------------
   //-| Declare Variables
   //-| ----------------------------------------------------------------

   string Name;        //-| Name of student
   int ID;             //-| Student's ID number
   float GPA;          //-| Student's GPA
   char letterGrades;  //-| The students letter grade
   int creditHours;    //-| Credit Hours for each course
   //-| ----------------------------------------------------------------------------
   //-| 1. 1. Initialize: letter grade counts, hoursTaken, coursesTaken, qualityPts.
   //-| ----------------------------------------------------------------------------
   int Acount = 0;
   int Bcount = 0;
   int Ccount = 0;
   int Dcount = 0;
   int Fcount = 0;
   int hoursTaken = 0;
   float coursesTaken = 0;
   float qualityPts = 0;
   //-| ----------------------------------------------------------------
   //-| 2a. Read student ID and full name. 2b. Prompt: Enter ID and Name:
   //-| ----------------------------------------------------------------
   cout << "Enter ID and Name: (No space between ID and Name, ex: 1234Byron C Fields)" << endl;
   cin >> ID;
   getline(cin, Name);
   //-| ---------------------------------------------------------------------------------------------------------
   //-| 3a. Read data for one course: creditHours, letterGrade 3b. Prompt:  Enter credit hours and letter grade:
   //-| ---------------------------------------------------------------------------------------------------------
   cout << "Enter credit hours and letter grade:" << endl; 
   cin >> creditHours >> letterGrades;
   //-| ----------------------------------------------------------------
   //-| 4. Repeat while (not the END MARKER)
   //-|   4a. update coursesTaken
   //-|   4b. update hoursTaken
   //-|   4c. update qualityPts and grade counts based on letter grade 
   //-|       and creditHours 
   //-| ----------------------------------------------------------------
   while (creditHours != 0 || letterGrades != 'X')
    {coursesTaken = coursesTaken++;
     hoursTaken = hoursTaken + creditHours;
     if (letterGrades == 'A')
     { Acount = Acount + 1;}
     if (letterGrades == 'B')
     { Bcount = Bcount + 1;}
     if (letterGrades == 'C')
     { Ccount = Ccount + 1;}
     if (letterGrades == 'D')
     { Dcount = Dcount + 1;}
     if (letterGrades == 'F')
     { Fcount = Fcount + 1;}
     qualityPts = qualityPts + (4*Acount+3*Bcount+2*Ccount+1*Dcount);
     cin >> creditHours;
     cin >> letterGrades;
}
   //-| ---------------------------------------------------------------------
   //-| 5. Compute GPA = qualityPts / hoursTaken; //Avoid divide by zero
   //-| ---------------------------------------------------------------------
      GPA = qualityPts / coursesTaken;
   //-| ---------------------------------------------------------------------
   //-| 6. Print the GPA report in this format:
   //-|
   //-|   ===========================================
   //-|   STUDENT ID:   xxxxxxxxx
   //-|   STUDENT NAME: xxxxxxxxxxxxxxxxxxxxxxx
   //-| 
   //-|   LETTER GRADE:  A   B   C   D   F
   //-|   COUNT:        xx  xx  xx  xx  xx
   //-|
   //-|   COURSES TAKEN:
   //-|   HOURS TAKEN:   xxx
   //-|   STUDENT GPA:  x.xx
   //-|   ===========================================
   //-| 
   //-| ----------------------------------------------------------------------
   cout << "===========================================";
   cout << endl;
   cout << "STUDENT ID:   " << ID;
   cout << endl;
   cout << "STUDENT NAME: " << Name;
   cout << endl;
   cout << "LETTER GRADE:  " << "A   " << "B   " << "C   " << "D   " << "F   ";
   cout << endl;
   cout << "COUNT:         " << Acount << "   " << Bcount << "   " << Ccount << "   " << Dcount << "   " << Fcount;
   cout << endl;
   cout << "COURSES TAKEN: " << coursesTaken << endl;
   cout << "HOURS TAKEN:   " << hoursTaken;
   cout << endl;
   cout << "STUDENT GPA:   " << fixed << setprecision(2) << GPA << " ";
   cout << endl; 
   cout << "===========================================";
   cout << endl;



   return 0;
}
Possibly because of this.

line 62
{coursesTaken = coursesTaken++;

Change to

{coursesTaken++;

or

{coursesTaken = coursesTaken+1;

or

{coursesTaken += 1;

Also this line may need to be moved outside the while loop.

qualityPts = qualityPts + (4*Acount+3*Bcount+2*Ccount+1*Dcount);

Last edited on
thanks man. moving qualityPts out fixed it
Topic archived. No new replies allowed.