+ 1

Please help, output shows all "quotes"

#include <iostream> using namespace std; int main() { int mark; cin >> mark; if (mark >= 50) { cout << "You passed." << endl; if (mark == 100) { cout <<"Perfect!" << endl; } } if (mark == 0) { cout << "stupid" << endl; } else { cout << "You failed." << endl; } return 0; } https://code.sololearn.com/c9ySaHWgwaON/?ref=app

9th Nov 2020, 1:34 PM
Manjunatha K
Manjunatha K - avatar
2 Réponses
+ 3
If you want the cases to be independant of each other, you have to use else if ( mark == 0 ) ... Right now, you have two if-statements, one that checks if "mark" is bigger than or equal to 50, and the second which checks if "mark" is equal to 0 or not, where the else belongs to the latter one. Using "else if" constructs a single if-statement instead. As another option, you could also use a structure similar to the first part, with a nested if-statement, e.g. else { cout << "You failed." << endl; if ( mark == 0 ) ... } It just depends on what you want. With the first solution, a mark of 0 would result in "stupid" being printed only, since the else part wouldn't be executed, whereas the second solution would result in "You failed. stupid" for the same mark. Otherwise, their are interchangeable.
9th Nov 2020, 1:38 PM
Shadow
Shadow - avatar
+ 2
thank you
9th Nov 2020, 1:41 PM
Manjunatha K
Manjunatha K - avatar