-1

C++ beginner here. So, I'm trying to make this code function like a finals grade calculator, but it isn't working. It gives back the value of 100 every time. What's wrong?

#include <iostream>

using namespace std;

int main()
{
    double grade, des, worth, per, grade1, tgrade, fgrade;
    cout<<"What is your current grade in the class?"<<endl;
    cin>>grade;
    cout<<"What is your desired grade in the class?"<<endl;
    cin>>des;
    cout<<"How much is your final exam worth? (Percent of total grade)"<<endl;
    cin>>worth;
    per=1-(worth/100);
    grade1=grade*per;
    fgrade=100;
    while ((tgrade-0.01)<=des<=(tgrade+0.01)) {
        fgrade=fgrade-1;
        tgrade=grade1+(fgrade*(worth/100));
    }    
    cout<<"You need a grade of "<<fgrade<<"% on your final exam to reach your desired grade in your class. Good Luck!"<<endl;
    return 0;
}
bmgballer
  • 1
  • 2
  • Does this answer your question? [Double inequality while comparying three floating point numbers](https://stackoverflow.com/questions/34389221/double-inequality-while-comparying-three-floating-point-numbers) – Aykhan Hagverdili Dec 13 '19 at 05:30
  • `grade1` is only assigned to and `per` is only used to calculate it. – AndersK Dec 13 '19 at 05:57

1 Answers1

0

I can see the logic you're trying to achieve in the loop condition, however <= is a binary operator, so you need to have two conditions which you combine with &&.

while((tgrade-0.01) <= des && des <= (tgrade+0.01)) 
robthebloke
  • 9,331
  • 9
  • 12