Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i made a program that gives you a letter grade and average when you enter a number from 0 to 100. I'm missing one piece where the user inputs a number outside of 0 to 100. the output is suppose to look like this

Example output 1 (invalid input, input is in bolded italics):
GPA Conversion
- - - - - - -
Enter your grade: -10.2
You have not entered an grade between 0 and 100. Ending program.

every code i tried doesn't result in what im suppose to get above if i put an invalid number below 0 or over 100.

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.";

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

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

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

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

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

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

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

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

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

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

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






    return 0;
}
Posted
Updated 23-Feb-16 2:51am
v2
Comments
Jochen Arndt 23-Feb-16 6:43am    
You should learn about the C/C++ keyword 'else'.
glen205 23-Feb-16 6:51am    
Your code contains no less than twelve conditional -> output i.e.
if (condition to test)
cout << "output string";

After writing that 12 times, can you see a pattern and infer that it's possible to write the test/conditional statements (and output strings) for grade > 100 and grade < 0?
Aescleal 23-Feb-16 7:00am    
You might want to see what the output is happens if someone enters 76.5

See Statements and flow control - C++ Tutorials[^] to learn about the selection statements if and else.
 
Share this answer
 
The 'find right interval' is usually implemented this way:

C++
int grade; // looking at you code, grade should be an integer. 
//...
if (grade > 100) 
{// error, input out of range, too high
}
else if (grade >= 96) 
{// this handles 96 <= grade <= 100
  cout << "You will receive a letter grade of A with a 4.00 GPA.";
}
else if (grade >= 90)
{// this handles 90 <= grade <= 95
 //..
}
//..
else if ( grade >= 0)
{ // this handles 0 <= grade <= 59
}
else
{ // error, input out of range, too low
}
 
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