Multiple if or if/else statement with only one cout

I cannot figure out how to write this without using multiple cout statements for the output of the letter grade.

Write a C++ program which takes in a numeric grade and displays a letter grade.
Last edited on
Store whatever is necessary in one or more variables. After putting the required result into a variable, you can later print it out.

You probably need at least two variables, one to hold the number which was input. Another variable to store the letter which is to be output. I don't think you need any more than that in terms of variables.

At the start, presumably you can use cout to prompt for the user to enter the numeric grade. Use cin to read it into a variable.

At the end, use cout to output the letter.

In between there will be some logic but you don't need any cin or cout for that part.

Thank you, I will try to figure this out.
You need only 1 cin statement to get the input, some if-else statements to calculate the output and finally one statement for the output. Are you prohibited to use more than one output statement?
1
2
3
4
5
6
7
8
9
10
11
int main()
{
  int numericGrade = 0;
  char letterGrade = 0;

  cin >> numericGrade;

  // your logic to determine the result

  cout << "Grade is: " << letterGrade << "\n\n"; 
}
.
Last edited on
I cannot get the output of the original numeric grade with the letter grade when I compile and enter a number. This is what I am messing with.

int main()
{

double grade;
double letterGrade;

const int A = ((grade >= 90) && (grade <= 100));
const int B = ((grade >= 80) && (grade <= 89));
const int C = ((grade >= 70) && (grade <= 79));
const int D = ((grade >= 60) && (grade <= 69));
const int F = ((grade >= 0) && (grade <= 59));

letterGrade = A, B, C, D, F;

cout << "Enter your grade: " << flush;


while(!(cin >> grade))
{
cin.clear();
cin.ignore(10000,'\n');
cout << "***Invalid, please enter a numeric value: ";
}

if ((grade == A) || (grade == B) || (grade == C) || (grade == D) || (grade == F))
{
cout << "your grade of: " << grade
<< "\nIs a letter grade of: " << letterGrade;
}
if ((grade < 0) || (grade >= 101))
{
cout << "\n0 - 100 only\n";
}
return 0;
}
Hmm some useful code there - as well as some which doesn't look like it can work.

First the straightforward stuff. Somewhere along the line, your letter grade became a type double ? It should be a char, as already stated by Thomas1965.

This test
 
    if ((grade < 0) || (grade >= 101)) 
rather strangely tests for values >= 101. It should be just > 100, shouldn't it? (assuming percentages in usual range of 0 to 100).


All the const int A to const int F serves to document your intentions. I treat it as comments because it isn't needed in that form as actual code.

I re-arranged the code a little, first get the input, validate that it is in the range 0 to 100 inclusive, Only then, with a clean input value, go on to try to determine the letter for that number. Strictly speaking, all of the foregoing is adding extra features which were not mentioned in the original question in any case.

So now we get to the heart of the problem. I made a start at lines 24 to 35 below. That is the focus of the original problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>

using namespace std;

int main()
{
    double grade;
    
    cout << "Enter your grade: " << flush;
    
    while (!(cin >> grade))
    {
        cin.clear();
        cin.ignore(10000,'\n');
        cout << "***Invalid, please enter a numeric value: ";
    }    
    
    if ((grade < 0) || (grade > 100))
    {
        cout << "\n0 - 100 only\n";
        return 0;
    }
    
    char letterGrade = 'X';
    
    if (grade >= 90) 
        letterGrade = 'A';
    else if (grade >= 80)
        letterGrade = 'B';
        
//  ...            
//  ...            

    else 
        letterGrade = 'F';

           
    cout << "your grade of: " << grade
         << "\nIs a letter grade of: " << letterGrade;

} 

Last edited on
Out of interest, I looked at something suggested by the earlier code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

using namespace std;

int main()
{
    cout << "Enter your grade: " << flush;
    double grade = -1.0;
    cin >> grade;

    bool A = (grade >= 90) && (grade <= 100);
    bool B = (grade >= 80) && (grade <   90);
    bool C = (grade >= 70) && (grade <   80);
    bool D = (grade >= 60) && (grade <   70);
    bool F = (grade >=  0) && (grade <   60);    
    
    char letterGrade = 'X';

    if      (A) letterGrade = 'A';
    else if (B) letterGrade = 'B';
    else if (C) letterGrade = 'C';
    else if (D) letterGrade = 'D';
    else if (F) letterGrade = 'F';

    cout << "Grade: " << grade << " is letter grade: " << letterGrade << '\n';
} 


It is usually better to avoid expressing the same number multiple times in different places - for example the number 70 is used on both lines 13 and 14. That is extra work and increases the possibility of errors, especially if the ranges need to be adjusted and one value is changed without also changing the other.

The original code had an additional problem:
1
2
const int C = ((grade >= 70) && (grade <= 79));
const int D = ((grade >= 60) && (grade <= 69));

There are multiple gaps, for example a grade of 69.5 does not match either of the above ranges, so it is even more problematic.


Thanks, only problem is I can only use if and else. I cannot use nested if or else if.
Perhaps it would be better to state the full restrictions and requirements. There may be something important not yet mentioned.
One if/else statement. I assumed 10 points ... that is 60-69 is D, 70-79 is C, .. etc if you need to round/handle/something for floating point between grades, it is not hard to do that from here. If modification of grade is a problem, use a copy of the value.


1
2
3
4
5
6
  grade = std::min(grade, 99);
    if(grade < 60) 
        result = 'F';
     else 
           result = (char)( 'F' -  ((grade-49)/10.0) );
	
Last edited on
> I can only use if and else. I cannot use nested if or else if.

Compare from lower grade upwards, and you won't need nested if statements.
1
2
3
4
5
char letterGrade = 'F';
if( grade >= 60 ) letterGrade = 'D';
if( grade >= 70 ) letterGrade = 'C';
if( grade >= 80 ) letterGrade = 'B';
if( grade >= 90 ) letterGrade = 'A';


Avoid this kind of palooka stuff: result = (char)( 'F' - ((grade-49)/10.0) );
(though it may work as expected on some of the implementations).
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
   char letter[] = { 'F', 'D', 'C', 'B', 'A' };
   int m;
   cout << "Input mark: ";   cin >> m;
   cout << "Grade: " << letter[ min( 4, max( 0, ( m - 50 ) / 10 ) ) ];
}


1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;

int main()
{
   char letter[] = { 'F', 'D', 'C', 'B', 'A' };
   int m;
   cout << "Input mark: ";   cin >> m;
   cout << "Grade: " << letter[ ( m >= 60 ) + ( m >= 70 ) + ( m >= 80 ) + ( m >= 90 ) ];
}
Last edited on
For some reason I misread that as only 1 if/else statement. If he can use multiples, the hack approach is probably not useful. I am not generally a fan of unreadable gibberish code, but if someone piles enough arbitrary limitations on you ....

lastchance's code is similar except I just used the ascii table directly instead of making a new one, which made mine a little weird because of the custom of skipping the letter E. Trivia... my high school was 70+ to pass with C, else fail. D did not exist.
Last edited on
This is what i have so far. I can't seem to get this to work with what I have below

else ((grade < 0) || (grade > 100))
{
cout << "0 - 100 only";
}








#include <iostream>
using namespace std;

int main( )
{
char letterGrade;
double grade;

cout << "Enter your grade: " << flush;
while(!(cin >> grade))
{ cin.clear();
cin.ignore(10000,'\n');
cout << "***Invalid, please enter a numeric value: "; // Request retry.
} //end of error_recovery
if ((grade >= 90) && (grade <= 100))
letterGrade = 'A';
if ((grade >= 80) && (grade <= 89))
letterGrade = 'B';
if ((grade >= 70) && (grade <= 79))
letterGrade = 'C';
if ((grade >= 60) && (grade <= 69))
letterGrade ='D';
if ((grade >= 0) && (grade <= 59))
letterGrade = 'F';
{
cout << "\nYour grade of: " << grade
<< " \nIs a letter grade of: " << letterGrade;
}

return 0;
}
you need to splice something like this into it:

1
2
3
4
5
6
7
8
double grade = -1; //initialize variables!!
while((grade < 0) || (grade > 100))
{
 cout << "prompt stuff";
cin >> grade;
if((grade < 0) || (grade > 100))
  cout << "bad user! try again" << endl;
}


Last edited on
This is what i have so far.

1
2
3
4
5
6
7
8
9
10
if ((grade >= 90) && (grade <= 100))
letterGrade = 'A';
if ((grade >= 80) && (grade <= 89))
letterGrade = 'B';
if ((grade >= 70) && (grade <= 79))
letterGrade = 'C';
if ((grade >= 60) && (grade <= 69))
letterGrade ='D';
if ((grade >= 0) && (grade <= 59))
letterGrade = 'F'; 

This code is broken as before. What letter grade is assigned if the number grade is 69.5?

Even if grade must always be an integer, you should still avoid writing code with (even potential) gaps in it.

See suggestion from JLBorges above for one possible approach
http://www.cplusplus.com/forum/general/222416/#msg1020462

closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
    char letterGrade = 'A';
    double grade = 0.0;
    
    std::cout << "What grade?: ";
    std::cin >> grade;
    
    if( grade > 100 ) letterGrade = '*'; // 'PERHAPS '*' SHOWS INPUT ERROR
    
    if( grade < 100 ) letterGrade = 'A';
    if( grade <  90 ) letterGrade = 'B';
    if( grade <  80 ) letterGrade = 'C';
    if( grade <  70 ) letterGrade = 'D';
    if( grade <  60 ) letterGrade = 'F';
    
    if( grade <   0 ) letterGrade = '*'; // 'PERHAPS '*' SHOWS INPUT ERROR
    
    std::cout << letterGrade << '\n';
    
    return 0;
}
closed account (48T7M4Gy)
Or if you want to test it this way of doing it by cascading from the top, which obviously isn't the only way to do it, except for it's convenience.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

int main()
{
    char letterGrade = 'A';
    double grade = 0.0;
    
    //std::cout << "What grade?: ";
    //std::cin >> grade;
    
    double inc = 0.5;
    grade = - 10;
    
    for(int i = -10; i < 220; ++i)
    {
        if( grade > 100 ) letterGrade = '*'; // 'PERHAPS '*' SHOWS INPUT ERROR
        
        if( grade < 100 ) letterGrade = 'A';
        if( grade <  90 ) letterGrade = 'B';
        if( grade <  80 ) letterGrade = 'C';
        if( grade <  70 ) letterGrade = 'D';
        if( grade <  60 ) letterGrade = 'F';
        
        if( grade <   0 ) letterGrade = '*'; // 'PERHAPS '*' SHOWS INPUT ERROR
        
        std::cout << letterGrade << '\t' << grade << '\n';
        grade += inc;
    }
    
    return 0;
}
Topic archived. No new replies allowed.