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"); } }
5 Réponses
+ 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
+ 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.
+ 1
You need to put quotes surrounding case answers, thats all 😉😊!
+ 1
But how can I do that?
0
I put quotes. But still error occurred.