Reputation:
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;
}
Upvotes: 0
Views: 219
Reputation: 170
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;
Upvotes: 1