LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-31-2004, 04:20 AM   #1
huno
Member
 
Registered: May 2002
Distribution: Slackware 9.1
Posts: 161

Rep: Reputation: 30
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
 
Old 10-31-2004, 04:30 AM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
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.
 
Old 10-31-2004, 07:39 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
...and after each "case ...." there must be a colon ( : ), not a semicolon ( ; ).
 
Old 10-31-2004, 08:54 AM   #4
vegpl
LQ Newbie
 
Registered: Jan 2004
Location: ~
Posts: 28

Rep: Reputation: 15
if you do want ot use names like Monday instead of integers than just use the enum keyword
 
Old 10-31-2004, 01:13 PM   #5
randyding
Member
 
Registered: May 2004
Posts: 552

Rep: Reputation: 31
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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with KVM switch davcefai Linux - Hardware 2 10-30-2005 03:47 AM
thread switch results in kernel stack switch superstition Linux - General 1 05-17-2005 11:48 PM
console switch problem fireflyanant Linux - Newbie 3 04-06-2005 10:20 PM
Network problem with switch glenismyname Linux - Networking 4 06-25-2004 03:26 PM
set up DSL thru SWITCH - winXp connected to SWITCH too husz Linux - Newbie 5 04-22-2004 12:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:11 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration