+ 1

Hi , who can help me to find my error

#include <stdio.h> int main() { int temp; scanf("%d", &temp); switch (temp) { case (temp <= 20): printf("Ooooohhhh! Damn cool!\n"); case (temp > 20 && temp <= 30): printf("Rain rain here again!\n"); case (temp > 30 && temp <= 40): printf("Wish I am on Everest\n"); default: printf("Good old nagpur weather\n"); } return 0; }

10th May 2020, 8:10 AM
Dostonbek
Dostonbek - avatar
4 Respostas
+ 2
There should be break statement and also your cases are wrong. In switch you have passed int value but in your cases there is Boolean value which will give error.
10th May 2020, 8:28 AM
A͢J
A͢J - avatar
+ 2
Thank you bro
10th May 2020, 8:50 AM
Dostonbek
Dostonbek - avatar
+ 1
I put break statements but it says the problem is in case statements , ( temp cannot appear in a constant-expression) can we usually write case(temp<=20) something like that?
10th May 2020, 8:35 AM
Dostonbek
Dostonbek - avatar
+ 1
Dostonbek Your program is incorrect. It should be look like this. The data type of cases should be same as passed in switch #include <stdio.h> int main() { int temp; int a = 0; scanf("%d", &temp); if(temp <= 20) a = 1; else if (temp > 20 && temp <= 30) a = 2; else if (temp > 30 && temp <= 40) a = 3; switch (a) { case 1: printf("Ooooohhhh! Damn cool!\n"); break; case 2: printf("Rain rain here again!\n"); break; case 3: printf("Wish I am on Everest\n"); break; default: printf("Good old nagpur weather\n"); break; } return 0; }
10th May 2020, 8:39 AM
A͢J
A͢J - avatar