Click here to Skip to main content
15,891,423 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
i had to make a code for a GPA converter. every thing works fine when I put in a number like 83. But when I input a number with a decimal like 83.5 it prints out the else message for me. What am I missing?

What I have tried:

C++
#include <iostream>
#include <string>
using namespace std;

int main()
{

    float grade;
    cout.precision(2);

    cout << "GPA Conversion" << endl;
    cout << "--------------" << endl;
    cout << "Enter your grade: ";
    cin >> grade;
    cout << endl;

    if ( grade <=100 && grade  >=96)
        cout << "You will receive a letter grade of A with a 4.00 GPA.";

    else if ( grade <=95 && grade  >=90)
        cout << "You will receive a letter grade of A- with a 3.70 GPA.";

    else if ( grade <=89 && grade  >=87)
        cout << "You will receive a letter grade of B+ with a 3.30 GPA.";

    else if ( grade <=86 && grade  >=84)
        cout << "You will receive a letter grade of B with a 3.00 GPA.";

    else if ( grade <=83 && grade  >=80)
        cout << "You will receive a letter grade of B- with a 2.70 GPA.";

    else if ( grade <=79 && grade  >=77)
        cout << "You will receive a letter grade of C+ with a 2.30 GPA.";

    else if ( grade <=76 && grade  >=74)
        cout << "You will receive a letter grade of C with a 2.00 GPA.";

    else if ( grade <=73 && grade  >=70)
        cout << "You will receive a letter grade of C- with a 1.70 GPA.";

    else if ( grade <=69 && grade  >=67)
        cout << "You will receive a letter grade of D+ with a 1.30 GPA.";

    else if ( grade <=66 && grade  >=64)
        cout << "You will receive a letter grade of D with a 1.00 GPA.";

    else if ( grade <=63 && grade  >=60)
        cout << "You will receive a letter grade of D- with a 0.70 GPA.";

    else if ( grade <=59 && grade  >=0)
        cout << "You will receive a letter grade of F with a 0.00 GPA.";

    else
        cout << "You have not entered a grade between 0 and 100. Ending program.";


return 0;
}</string></iostream>
Posted
Updated 25-Feb-16 21:01pm
v2
Comments
[no name] 25-Feb-16 21:47pm    
Look at your conditions. Ranges are left out. Try "else if ( grade <= 86 && grade > 83)". Now learn to use debugger.
Member 12329656 25-Feb-16 22:23pm    
unfortunately don't have access to a debugger becuase i created the code in a cpp file not a project file
[no name] 26-Feb-16 5:41am    
Nonsense.
F-ES Sitecore 26-Feb-16 3:50am    
Which "else" condition do you *think* 83.5 should be falling under? Which "if" does it match?

You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

You trap yourself, look at your code
C++
else if ( grade <=86 && grade  >=84)
    cout << "You will receive a letter grade of B with a 3.00 GPA.";

else if ( grade <=83 && grade  >=80)
    cout << "You will receive a letter grade of B- with a 2.70 GPA.";

83.5 don't receive a 3.00 because it is not between 86 and 84.
83.5 don't receive a 2.70 because it is not between 83 and 80.

By running your code line by line on debugger, you would see where is the problem by yourself.
 
Share this answer
 
Comments
Arthur V. Ratz 20-Mar-16 11:38am    
5.
If grade is really a float then you have to make your ranges complete, e.g.
Quote:
if ( grade <=100 && grade >=96)
cout << "You will receive a letter grade of A with a 4.00 GPA.";

else if ( grade <=95 && grade >=90)
cout << "You will receive a letter grade of A- with a 3.70 GPA.";

must be instead
C++
if ( grade <=100.0f && grade  >=96.0f)
        cout << "You will receive a letter grade of A with a 4.00 GPA.";
 
else if ( grade  >=90.0f) // you don't need to check for  (grade < 96.0f) here
        cout << "You will receive a letter grade of A- with a 3.70 GPA.";


and so on.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900