Camparisons

Sep 29, 2013 at 7:09pm
My question is how to write programs using only one comparison with if statements with out logical operators.

Exampe if i wanted to compare a users input i would write

If (userinput >= 70 && userinput <= 80)

cout << condition >>

My professor how ever only wants me use one Comparison.

Last edited on Sep 29, 2013 at 7:14pm
Sep 29, 2013 at 7:22pm
Note sure if I am getting what you are saying.

You want to check if a number is between 70 and 80 with only one operator? I don't think that's possible. But maybe I mis-understood.

you could do this:

1
2
3
bool condition = (70 <= userinput && userinput <= 80);
if (condition)
    cout << condition;
Sep 29, 2013 at 8:58pm
Im trying to learn how to use nested if else statements to do this.

Its a grading program. The user enters a percentage then the program displays the grade and how many points each grade is worth. The highest grade is 100

This is what i have so far

#include <iostream>
using namespace std ;

Int main {

double userinput;

cin >> userinput

If (userinput > 60 && userinput < 63)

cout << " Your grade is" << userinput<< "your grade is D<< "points:0.65" endl;

return 0;
}

Im supposed to write it using only one comparison assuming the highest grade is 100
Sep 29, 2013 at 9:06pm
Ooooohh. so one operator per statement.

This should help. It's not nested, but it avoids multiple operators per conditional statement.

1
2
3
4
5
6
7
8
9
10
    if (percent < 50)
        grade = 'F';
    else if (percent < 60)
        grade = 'D';
    else if (percent < 70)
        grade = 'C';
    else if (percent < 80)
        grade = 'B';
    else 
        grade = 'A';
Last edited on Sep 29, 2013 at 9:07pm
Sep 29, 2013 at 9:23pm
thanks. it did
Topic archived. No new replies allowed.