0

I am new to C++ and wrote a simple GPA calculator program.
It basically asks for the amount of classes you have and takes the grade of each.
It then converts the letter grades to a numeric.
Finally, it sums the numerical grades and in order to average it.
My question is, how exactly would I get the GPA result in to 4.00, 3.00, 2.00, etc. form?

#include <iostream>
using namespace std;

int main(){
    int grades_size;
    cout << "Enter the number of classes you are taking: ";
    cin >> grades_size;

    int *grades;
    grades = new int[grades_size];
    float *conversion;
    conversion = new float[grades_size];

    float sum = 0;

    cout << endl << "Enter your grade percentage of each class as a number 0-100." << endl;

    for (int i = 0; i < grades_size; i++) {
        cout << "Grade " << i + 1 << ": ";
        cin >> grades[i];

        if (grades[i] <= 100 && grades[i] >= 90) {
            conversion[i] = 4.00;
        } else if (grades[i] < 90 && grades[i] >= 80) {
            conversion[i] = 3.00;
        } else if (grades[i] < 80 && grades[i] >= 70) {
            conversion[i] = 2.00;
        } else if (grades[i] < 70 && grades[i] >= 60) {
            conversion[i] = 1.00;
        } else if (grades[i] < 60 && grades[i] >= 0) {
            conversion[i] = 0.00;
        }

        sum += conversion[i];
    }

    float GPA = sum / grades_size;
    cout << endl << "--- Your GPA is: " << GPA;
}
  • 1
    Assuming you're just talking about showing a certain number of decimal places: http://stackoverflow.com/questions/22515592/how-to-use-setprecision-in-c – jeff carey Jan 01 '17 at 01:43
  • To show decimal you can use the #include library and specifically the fixed and setprecision(x). For instance: cout << " Printing this number up to two decimal places" << fixed << setprecision(2) << number << endl; – Omid CompSCI Jan 01 '17 at 01:48
  • Use 3 digit numbers to represent the gpa number values then divide by 100... for example 321 /100.00 = 3.21... or 300 / 100.00 = 3.00 – aguertin Jan 01 '17 at 01:51

1 Answers1

1

std::setprecision (in the iomanip header), as well as std::fixed (included by iostream already), might be of use here.

setprecision: Sets the decimal precision when printing floating point values.

fixed: Float values are printed in fixed notation. When used with setprecision, the printed number is always that many digits long.

#include <iomanip> // std::setprecision
...
cout << endl << "--- Your GPA is: " << std::fixed << std::setprecision(2) << GPA;
Taylor Hansen
  • 170
  • 1
  • 9