0

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;  
}
Uclydde
  • 1,414
  • 3
  • 16
  • 33

1 Answers1

2

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.
CeePlusPlus
  • 803
  • 1
  • 7
  • 26
  • When I tried doing that, it broke the whole program. I don't know exactly how to change it from int to double. – Jessica Westbrook Nov 25 '19 at 04:28
  • `(double) gradePoints/totalCredits`. A few other tips: You are repeating almost your entire program just to make sure it executes one time. This goes against basics of programming, you should (almost) never repeat yourself. One solution is a do while loop which will execute once. I personally like this second option: Just set answer to "Yes" and you know it will execute. IMHO much cleaner. – CeePlusPlus Nov 26 '19 at 00:02
  • When you said everything is breaking, this was due to an infinte loop due to the `cin` stream getting stuck before continuing. (cin can get very finicky in c++). Edited answer to include this. – CeePlusPlus Nov 26 '19 at 00:02