I am trying to create a grading program that allows the user to input his/her grade, which tells the user where they stand, as in what letter grade did he/she get. The problem is when I (user) enter my grade all the if statements are executed instead of the one that should be. It turns out that expression like these (if statement conditions) have no mathematical meaning in c++ programming. So how do I create range???
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int grade;
cout << "Enter Your Grade" << endl;
cin >> grade;
if (0 <= grade <= 59) {
cout << "Your letter grade is: F" <<endl;
}
if (60 <= grade <= 69) {
cout << "Your letter grade is: D" <<endl;
}
if (70 <= grade <= 79) {
cout << "Your letter grade is: C" << endl;
}
if (80 <= grade <= 89) {
cout << "Your letter grade is: B" << endl;
}
if (90 <= grade <= 99) {
cout << "Your letter grade is: A" << endl;
}
if (grade == 100) {
cout << "Your letter grade is: A++" << endl;
}
Doing that executed 2 statements rather than 1 statement in which the number range is. Please review my code above and let me know what I need to do in order achieve the goal mentioned above.
I already told you how to fix your issues, to combine two or more comparisons in an if statement you need to use the correct logical operator, and then use complete comparisons on both sides of the logical operator, as I illustrated above.
The code I provided is not something you can just copy and paste into your program, you will need to insure the logic is what you want. I just posted random code to show you the syntax, not the logic.
YAY I figured it out, it works now!!! By the way @jlb thanks for letting me know how to set range. The only reason why it was not working was that I forgot to place breakpoint after each if statement which is why it was not working.
@Thomas1965 Copy & paste your code in the comment between these (b/w 2 square brackets write code and then copy & paste your code, use square brackets and in it type /code) and it will print your code as this>>>
1 2 3 4 5 6
#include <iostream>
usingnamespace std;
int main () {
cout << "Hello Thomas";
return 0;
}