Fix Error! | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
+ 2

Fix Error!

While loop - #include <iostream> using namespace std; int main() { int mark = 100; if (mark >= 50 && <= 59) { cout << "passed." << endl; if (mark >= 60 || <= 69) cout << "Good." << endl ; if (mark >= 70 || <= 79) cout << "Very Good." << endl ; if (mark >= 80 || <= 89) cout << "Excellent." << endl ; if (mark >= 90 || <= 99) cout << "Superb." << endl ; if (mark == 100) { cout <<"Perfect!" << endl; }

4th Dec 2016, 1:53 AM
Khalil Ur Rehman
Khalil Ur Rehman - avatar
16 ответов
+ 2
Checking conditions can't be done in switch statements. Switch statements only work with constant values, and doesn't do conditional checking I'm afraid. You cannot shorten what you have there.
4th Dec 2016, 3:02 AM
Cohen Creber
Cohen Creber - avatar
+ 18
int main() { int mark = 100; if (mark >= 50 && mark <= 59) { cout << "passed." << endl;} if (mark >= 60 && mark <= 69){ cout << "Good." << endl ;} if (mark >= 70 && mark <= 79){ cout << "Very Good." << endl ;} if (mark >= 80 && mark <= 89){ cout << "Excellent." << endl ;} if (mark >= 90 && mark <= 99){ cout << "Superb." << endl ;} if (mark == 100) { cout <<"Perfect!" << endl; } } // edinting and explaining a bit. First you need to write mark on both sides, the compiler is not so smart to guess what you mean by (mark >= 50 && <= 59). It' s like it will think "what is less or equal 59 ?" Then you need to open and close each if statement with braces {}, I suggest you use else if. Then finally if you use the operator or || and you set value to 100 it will print all the if statements because they will be all true. And there is no while loop :)
4th Dec 2016, 2:22 AM
R2-D2
R2-D2 - avatar
+ 3
A couple things. • Make all the if statements separate from eachother; you're putting them all in one. With what you got, if make wasn't between 50 and 59, then nothing would be output. • When using logical operators AND && and OR ||, you need to redeclare what variable you're comparing: if (mark >= 10 && mark <= 19) { } Like this • Use && for what you want, not ||. If we used || in the if statement above? Any number would be accepted, it will always be true. Because whatever number you enter, it will always either be greater than 10, or less than 19. If we use &&, we want it to be greater than or equal to 10 AND less than or equal to 19. • Although it wouldn't make too much of a difference in this situation, it would be better practice to use if, else-if, else in this case, rather than loads of ifs. This just makes it clear that only one of the blocks are going to be run, and there is no chance of multiple blocks being run. It should look something like this: if (marks < 10) cout << "Fail"; else if (marks >= 1 && marks <= 24) cout << "Bad"; else if (marks >= 25 && marks <= 49) cout << "Okay"; else if (marks >= 50 && marks <= 74) cout << "Good"; else if (marks >= 75 && marks <= 99) cout << "Very good"; else cout << "Perfect";
4th Dec 2016, 2:21 AM
Cohen Creber
Cohen Creber - avatar
+ 2
You need to put the variable name in the second part of the if (when you put && or ||). Check this, It works: (you still need to check the >= and <= operators to give the result you want, so you have some homework to do :P ) #include <iostream> using namespace std; int main() { int mark = 100; if (mark >= 50 && mark <= 59) cout << "passed." << endl; if (mark >= 60 || mark <= 69) cout << "Good." << endl ; if (mark >= 70 || mark <= 79) cout << "Very Good." << endl ; if (mark >= 80 || mark <= 89) cout << "Excellent." << endl ; if (mark >= 90 || mark <= 99) cout << "Superb." << endl ; if (mark == 100) cout << "Perfect!" << endl; }
4th Dec 2016, 2:17 AM
Nahuel
Nahuel - avatar
+ 2
I made it #include <iostream> using namespace std; int main() { int marks ; cout << "Please enter your marks between 1 to 100:"<<endl; cin >> marks ; if (marks < 40) cout << "Fail"; else if (marks >= 40 && marks <= 49) cout << "Poor"; else if (marks >= 50 && marks <= 59) cout << "Fair"; else if (marks >= 60 && marks <= 69) cout << "Good"; else if (marks >= 70 && marks <= 79) cout << "Very Good"; else if (marks >= 80 && marks <= 89) cout << "Excellent"; else if (marks >= 90 && marks <= 99) cout << "Brilliant Student"; else if ( marks == 100) cout << "PERFECT!"; else cout << "Invalid input!"; return 0; }
4th Dec 2016, 2:58 AM
Khalil Ur Rehman
Khalil Ur Rehman - avatar
+ 2
@GoOd/\DaY in switch this code will be very very long. Why? Because: 1. In if if(mark >= 50 && mark <= 59) cout<<"Fair"; 2. In switch switch(mark) { case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59: cout<<"Fair"; break; } Choose less lenghty :) PS if (i make any mistake) Sorry for my English :) ; else Cheers from Poland :) ;
5th Dec 2016, 9:47 AM
Łukasz Nizioł
Łukasz Nizioł - avatar
+ 1
you just solve my problem. I want to convert that code into switch statement. thanks Niziol
5th Dec 2016, 10:22 AM
Khalil Ur Rehman
Khalil Ur Rehman - avatar
+ 1
Yes, it's possible with a switch statement.. if you wanted to use hundreds of lines of code, 1 line per mark... ...or just use conditionals in an if statement?
5th Dec 2016, 3:42 PM
Cohen Creber
Cohen Creber - avatar
+ 1
1) You didn't request for user input for grade via std::cin. 2) Why are you using a switch statement with an integer parameter, then using character cases? int grade can't have a value of 'A' or 'a', unless you want to get in to ASCII values, which is not your intended purpose here.
5th Dec 2016, 10:50 PM
Cohen Creber
Cohen Creber - avatar
+ 1
cin>>grade; should be added before switch statement. Also you could use || operator in case statement.
7th Dec 2016, 7:24 PM
Mohammed Nabeel
Mohammed Nabeel - avatar
0
Thanks
4th Dec 2016, 2:16 AM
Khalil Ur Rehman
Khalil Ur Rehman - avatar
0
Thanks R2-D2
4th Dec 2016, 2:19 AM
Khalil Ur Rehman
Khalil Ur Rehman - avatar
0
How can I reduce the size of that code. It become so lengthy. How can I write this code in switch statement. please answer
4th Dec 2016, 3:00 AM
Khalil Ur Rehman
Khalil Ur Rehman - avatar
0
Where is the mistake? #include <iostream> using namespace std; int main() { int grade; cout << "Please enter your marks: \n"; switch(grade) { case A: case a: if (grade == 100) { cout << "Perfect"; } break; case B: case b: if (grade <= 99 && >= 84 ) { cout << "Brilliant"; } break; case C: case c: if (grade <= 85 && >= 75 ) { cout << "Excellent"; } break; case D: case d: if (grade <= 74 && >= 65 ) { cout << "Good"; } break; case F: case f: if (grade <= 64 && >= 54) { cout << "Fail"; } break; default: cout << "Invalid input"; } return 0; }
5th Dec 2016, 10:47 PM
Khalil Ur Rehman
Khalil Ur Rehman - avatar
0
cin>>grade
7th Dec 2016, 7:22 PM
Mohammed Nabeel
Mohammed Nabeel - avatar
0
1. Declare and Define a function by the name int Grade(int marks) and write C++ program to display a letter grade in upper case (Capital Letter) when Grade() function is called and return a value to the caller. The program is expected to accept marks as an input. Use the following input and output information to write the program. Input Output (marks>=90) and (marks<=100) A (marks>=70) and (marks<90) B (marks>=55) and (marks<70) C (marks>=35) and (marks<55) D (marks>=10) and (marks<35) F otherwise I
4th Aug 2022, 8:50 PM
Beba
Beba - avatar