problem on switch
i'm written the following code
Code:
#include <iostream.h> Code:
switch.cpp:10:12: warning: multi-character character constant thanks |
You're not going to be able to use switch() on a day name. You could rewrite it expecting the user to type a day number though (e.g. Sunday = 0, Monday = 1, etc.)
You might want to review how to write case conditions, because yours aren't quite right. Each case condition has to be an integer. Also, 'mon' isn't even valid code. "mon" would be valid, but you can't test against "mon" with switch/case. Well, you could, but no one in their right mind would. It wouldn't work the way you'd expect. |
...and after each "case ...." there must be a colon ( : ), not a semicolon ( ; ).
|
if you do want ot use names like Monday instead of integers than just use the enum keyword
|
Try this example
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv[]) { const char *dow="sun!mon!tue!wed!thu!fri!sat!"; char *p, buf[256]; int n; printf("Enter one of the following\nsun mon tue wed thu fri sat\n"); if (!fgets(buf,sizeof(buf),stdin)) return 1; p=buf+strlen(buf); while (p>buf && *(p-1)<=' ') *(--p)='\0'; strcat(buf,"!"); p=strstr(dow,buf); if (p) { n=(p-dow)/4; switch (n) { case 0: printf("You entered Sunday.\n"); break; case 1: printf("You entered Monday.\n"); break; case 2: printf("You entered Tuesday.\n"); break; case 3: printf("You entered Wednesday.\n"); break; case 4: printf("You entered Thursday.\n"); break; case 5: printf("You entered Friday.\n"); break; case 6: printf("You entered Saturday.\n"); break; } } else { printf("bad input\n"); } return 0; } |
All times are GMT -5. The time now is 03:20 PM. |