Hello, I am trying to make a GPA calculator, the code below works fine and the output is also fine the only problem is if the GPA is 4.0 or 3.0 or 2.0 or 1.0 it gives me output as 4, 3, 2, 1 i had used variable as float then also I am having this problem is the GPA is 3.5 then it gives me output as 3.5 the only problem is with round numbers such as 3.0 or 4.0....

_____________________________________________________

#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
// CODE STARTS HERE
    string HEADING = "ttttGPA CALCULATOR";
    cout << HEADING<<endl;
    int x;
    float GPA, TotalHrs = 0, GradeEqaPoints = 0,CrHours;
    char Grade, A, B, C, D, E, F;
    char choice = 'Y';
    while (choice == 'Y' || choice == 'y')

    {

        cout << "nENTER YOUR GRADE ---> ";
     cin >> Grade;
     cout << "nENTER CREDIT HOUR FOR THE GRADE ENTERED ---> ";
     cin >> CrHours;
if
         (Grade == 'A' || Grade == 'a')
                GradeEqaPoints = GradeEqaPoints + 12;
if
         (Grade == 'B' || Grade == 'b')
                GradeEqaPoints = GradeEqaPoints + 9;
if
         (Grade == 'C' || Grade == 'c')
                GradeEqaPoints = GradeEqaPoints + 6;
if 
         (Grade == 'D' || Grade == 'd')
                GradeEqaPoints = GradeEqaPoints + 3;
if 
         (Grade == 'F' || Grade == 'f')
                GradeEqaPoints = GradeEqaPoints + 0; 

            TotalHrs = TotalHrs + CrHours ;
                GPA = GradeEqaPoints/TotalHrs;
   {
                cout <<endl;
    cout << "nDO YOU WISH TO CONTINUE? (Y/N) ---> ";
    cin >> choice;
    cout << endl;
   }
   }
    cout << "ntttTOTAL CREDIT HOURS TAKEN FOR THIS SEMISTER ---> "<<TotalHrs;
    cout << endl;
    cout << "ntttTOTAL QUALITY POINTS ---> "<<GradeEqaPoints;
    cout<< endl;
    cout << "ntttYOUR CUMULATIVE GPA ---> "<<GPA;
    cout << endl;
   {
    cout << "nENTER (X) TO EXIT --> ";
    cin >> x;
    cout << endl;
    return (0);
    }
}
// CODE ENDS HERE

Edited 3 Years Ago by Dani: Formatting fixed

You need to set the stream flags, look below :

cout<<fixed; //make it so that 3.000 comes out instead of 3
	cout.precision(2); // make it so that 3.00 comes out

Now when you print something like cout << GPA << endl; and if GPA is a whole number (0,1,2,3,4) then it will print
it out like 0.00,1.00,2.00,3.00,4.00.

Put that code before you print out the GPA.

Edited 7 Years Ago by firstPerson: n/a

THANKS MAN, GOT IT DONE WITH YOUR HELP.
THANK YOU VERY MUCH.
Maverick.[

You need to set the stream flags, look below :

cout<<fixed; //make it so that 3.000 comes out instead of 3
cout.precision(2); // make it so that 3.00 comes out

Now when you print something like cout << GPA << endl; and if GPA is a whole number (0,1,2,3,4) then it will print
it out like 0.00,1.00,2.00,3.00,4.00.

Put that code before you print out the GPA.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

This question has already been answered. Start a new discussion instead.