0
Run this one and tell me the problem.
#include <iostream> using namespace std; int main() { int marks; cout << "Enter your marks" << endl; cin >> marks; if(marks>=90){ cout << "A- Grade"; if(marks>=80){ cout << "B-Grade"; if(marks>=70){ cout << "C-Grade"; } } } else if(marks<=69){ cout << "Just passed"; } else cout << "Fail"; return 0; } I get all the conditions played suppose if my input is 91.
2 odpowiedzi
+ 3
The first 3 if conditions will all be true because you're only checking if the value is higher than 70/80/90.
You can check between a range of values using the AND (&&) operator, eg:
if (marks > 90 && marks <= 100) {
//
} else if (marks > 80 && marks <= 90) {
//
} else if...
You also have an error in your logic - the way you have the loops nested means any value between 70 and 89 will run the else statement and print 'fail' (don't nest the loops or reverse the order).
Also, anything 69 and below will print as 'just passed' (I guess that should be fail).
0
you can write like this.(i think you should learn more about if sentence)
#include <iostream>
using namespace std;
int main() {
int marks;
cout << "Enter your marks" << endl;
cin >> marks;
if(marks>=90)
cout << "A- Grade";
else if(marks>=80)
cout << "B-Grade";
else if(marks>=70)
cout << "C-Grade";
else if(marks>=60)
cout << "Just passed";
else
cout << "Fail";
return 0;
}