LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   problem on switch (https://www.linuxquestions.org/questions/programming-9/problem-on-switch-249375/)

huno 10-31-2004 04:20 AM

problem on switch
 
i'm written the following code

Code:

#include <iostream.h>
int main ()
{
        char sh;
cout << "enter the name of today ( -1 to end )";
cin >> sh;
while ( sh != EOF ) {
switch ( sh )
{
    case 1 'sat' ; cout << " huh go to school "; break;
    case 2 'sun' ; cout << " huh go to school "; break;
    case 3 'mon' ; cout << " huh go to school "; break;
    case 4 'tue' ; cout << " huh go to school "; break;
    case 5 'wed' ; cout << " huh go to school "; break;
    case 6 'Thu' ; cout << " have nice weekend "; break;
    case 7 'fri' ; cout << "have nice weekend"; break;
    default
    cout << "wrong name ";
}
}
cout << " good luck ";
return 0;
}

when i compile it .. the compiler show the following problems
Code:

switch.cpp:10:12: warning: multi-character character constant
switch.cpp: In function `int main()':
switch.cpp:10: error: syntax error before '\x736174'
switch.cpp:11:12: warning: multi-character character constant
switch.cpp:11: error: syntax error before '\x73756e'
switch.cpp:12:12: warning: multi-character character constant
switch.cpp:12: error: syntax error before '\x6d6f6e'
switch.cpp:13:12: warning: multi-character character constant
switch.cpp:13: error: syntax error before '\x747565'
switch.cpp:14:12: warning: multi-character character constant
switch.cpp:14: error: syntax error before '\x776564'
switch.cpp:15:12: warning: multi-character character constant
switch.cpp:15: error: syntax error before '\x546875'
switch.cpp:16:12: warning: multi-character character constant
switch.cpp:16: error: syntax error before '\x667269'
switch.cpp:18: error: syntax error before `<<' token
switch.cpp:10: warning: unreachable code at beginning of switch statement

what is the mistake on the code .. then why ..

thanks

itsme86 10-31-2004 04:30 AM

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.

Hko 10-31-2004 07:39 AM

...and after each "case ...." there must be a colon ( : ), not a semicolon ( ; ).

vegpl 10-31-2004 08:54 AM

if you do want ot use names like Monday instead of integers than just use the enum keyword

randyding 10-31-2004 01:13 PM

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.