A value in my program is being calculated incorrectly, even though the supporting math is correct

75 views Asked by At

In a module for my class we are writing a program that asks for the class name, the grade, and number of credits. At the end of a certain number of classes, it is requesting that we calculate the GPA to 2 decimal points using setprecision(2). All of my program functions EXCEPT, it calculates the GPA to be 3.00 instead of 3.14. My totalPoints came to 22, and totalCredits were 7, so it rightfully should be 3.14 with the setprecision. Any insight as to why I am getting 3.00 instead? Any help is appreciated, as I am very new to this.

#include iostream
#include iomanip
#include string
//I took out the < and > because it made the words disappear

using namespace std;

int main(){
    cout<< std::fixed << std::setprecision(2);

    int totalCredits = 0;
    int gradePoints = 0;

    string courseName;
    cout<<"Enter a course name: ";
    getline (cin, courseName);
    cout<<courseName <<endl;

    int credits;
    cout<<"Enter number of credits: ";
    cin>>credits;
    cout<<credits <<endl;
    totalCredits = totalCredits + credits;

    string grade;
    cout<<"Enter your grade (A, B, C, D, F): ";
    cin>>grade;
    cout<<grade <<endl;

    if (grade == "A"){
      gradePoints = gradePoints + (4.00 * credits);
    }
    else if (grade == "B"){
      gradePoints = gradePoints + (3.00 * credits);
    }
    else if (grade == "C"){
      gradePoints = gradePoints + (2.00 * credits);
    }
    else if (grade == "D"){
      gradePoints = gradePoints + (1.00 * credits);
    }


    string answer;
    cout<<"Continue ('Yes' or 'No')? ";
    cin>>answer;
    cout<<answer <<endl;

    while (answer == "Yes"){
      cout<<"Enter a course name: ";
      cin.ignore();
      getline (cin, courseName);
      cout<<courseName <<endl;

      cout<<"Enter number of credits: ";
      cin>>credits;
      cout<<credits <<endl;
      totalCredits = totalCredits + credits;

      cout<<"Enter your grade (A, B, C, D, F): ";
      cin>>grade;
      cout<<grade <<endl;

      if (grade == "A"){
         gradePoints = gradePoints + (4.00 * credits);
      }
      else if (grade == "B"){
         gradePoints = gradePoints + (3.00 * credits);
      }
      else if (grade == "C"){
         gradePoints = gradePoints + (2.00 * credits);
      }
      else if (grade == "D"){
         gradePoints = gradePoints + (1.00 * credits);
      }


      cout<<"Continue ('Yes' or 'No')? ";
      cin>>answer;
      cout<<answer <<endl;
    }

    cout<<"Total grade points: " <<gradePoints <<endl;
    cout<<"Total credits attempted: " <<totalCredits <<endl;

   float gpa = 0;
   gpa = gradePoints/totalCredits;
   cout<<"Your GPA is " <<gpa <<endl;



   return 0;  
}
1

There are 1 answers

3
CeePlusPlus On

int divided by int results in an int. Cast one of them by adding (double) before the variable name by the division statement.


Other considerations:

  • You are repeating almost your entire program just to make sure it executes one time. You can simply set answer to "Yes" and you know it will execute.
  • cin << gets really finicky. Your generally better off using getLine() and parsing as needed, more info. (Probably overkill for homework...) The quickfix in this case to clear the stream with ignore(), and reset the stream with clear(). Sample working program here.