0

I am an absolute beginner. I tried to create a switch statement. But I failed to run it successfully. Can someone please help me

Please suggest me if I am doing anything wrong. #include <stdio.h> int main() { char day[100], a[100]; gets(day); a = tolower(day); switch (day) { case "monday": case "tuesday": case "thurseday": printf("One, Two, or Three.\n"); break; case "wednessday": case "friday": case "saturday": printf("Go to music class. \n"); break; default: printf("Go to Yoga class. \n"); } }

31st Jul 2020, 12:06 AM
Pankaj Kumar Panda
Pankaj Kumar Panda - avatar
5 odpowiedzi
+ 1
Do which one? 1. Use a loop. In each loop repetition call tolower() passing the n-th character from the 0-th (first one) forward until string terminator is found (end of string). Use isalpha() function to be sure we convert only alphabetic characters. http://www.cplusplus.com/reference/cctype/isalpha/ 2. I don't know any easy workaround to using string with `switch` statement. But you are welcome to check the following 👇 https://stackoverflow.com/questions/4014827/best-way-to-switch-on-a-string-in-c
31st Jul 2020, 4:10 AM
Ipang
+ 2
1. tolower() works with characters, to change a string to all lowercase you call tolower() for each alphabetic character in the string. 2. `switch` statement in C doesn't have built-in support for string evaluation. Use `if...else if...else` conditional instead.
31st Jul 2020, 2:23 AM
Ipang
+ 1
You need to put quotes surrounding case answers, thats all 😉😊!
31st Jul 2020, 12:25 AM
Sali65
Sali65 - avatar
+ 1
But how can I do that?
31st Jul 2020, 2:56 AM
Pankaj Kumar Panda
Pankaj Kumar Panda - avatar
0
I put quotes. But still error occurred.
31st Jul 2020, 12:53 AM
Pankaj Kumar Panda
Pankaj Kumar Panda - avatar