0

C++ switch statement beginner exercise

Can anyone please explain to me where is the error? I can't really understand😂 This is the exercise: "Write a program that allows the user to enter the grade scored in a programming class (0-100). Modify the program so that it will notify the user of their letter grade using switch statement. 0-59 F 60-69 D 70-79 C 80-89 B 90-100 A." And this is my code: #include <iostream> using namespace std; int main() { int grade=0; cin>>grade; switch (grade) { case 50&&59: cout<<"You've got a F"; break; case 60&& 69: cout<<"You've got a D"; break; case 70&&79: cout<<"Yuo've got a C"; break; case 80&&89: cout<<"Yuo've got a B"; break; case 90&&99: cout<<"Yuo've got an A"; break; } return 0; }

24th Nov 2018, 2:44 PM
Martina
2 Answers
+ 5
Switch as per the standards does not work for ranges. A&&B is of type bool, and bool variables can hold either a true or a false. These can be converted to a 1 or a 0. So your switch block translates to : switch (grade) { case 1: cout<<"You've got a F"; break; case 1: cout<<"You've got a D"; break; case 1: cout<<"Yuo've got a C"; break; case 1: cout<<"Yuo've got a B"; break; case 1: cout<<"Yuo've got an A"; break; } Since all the case values are the same, the compiler throws an error. To solve this problem, use an if else-if ... else combination. if(grade<=59&&grade>=50) cout<<"You've got a F"; else if(grade<=69&&grade>=60) cout<<"You've got a D"; // Other cases follow. // Base case for the last option. else cout<<"You've got an A"; If you have GNU GCC as your compiler, you can also use case ranges unique to GCC: switch(grade) { case 50...59: cout<<"You've got a F"; break; // The rest of the cases follow. }
24th Nov 2018, 2:54 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
0
#include <iostream> using namespace std; int main() { int marks; cout<<"Enter your Marks: "<<endl; cin>>marks; switch(marks) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 21: case 22: case 23: case 24: case 25: case 26: case 27: case 28: case 29: case 30: case 31: case 32: case 33: case 34: case 35: case 36: case 37: case 38: case 39: case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49: case 50: case 51: case 52: case 53: case 54: case 55: case 56: case 57: case 58: case 59: cout<<"Yuo've got an F"; break; case 60: case 61: case 62: case 63: case 64: case 65: case 66: case 67: case 68: case 69: cout<<"Yuo've got an D"; break; case 70: case 71: case 72: case 73: case 74: case 75: case 76: case 77: case 79: cout<<"Yuo've got an C"; break; case 80: case 81: case 82: case 83: case 84: case 85: case 86: case 87: case 88: case 89: cout<<"Yuo've got an B"; break; case 90: case 91: case 92: case 93: case 94: case 95: case 96: case 97: case 98: case 99: case 100: cout<<"Yuo've got an A"; break; default: cout<<"Invalid number"<<endl; break; } return 0; }
22nd Aug 2024, 9:59 PM
Ahmed Gamal
Ahmed Gamal - avatar