error with switch statement

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

error with switch statement

Post by mary »

I keep getting an error message at a switch statement I made, from what I can tell the code looks right

error message

Code: Select all

main.cpp:41: error: expected ‘:’ before ‘;’ token
main.cpp:41: error: expected primary-expression before ‘:’ token
main.cpp:41: error: expected ‘;’ before ‘:’ token
code (lines 39 - 46)
gameState is a char, and equals STATE_TITLE
STATE_TITLE is #define STATE_TITLE 0;

Code: Select all

switch(gameState)
	{
		case STATE_TITLE:
		{
		draw_surface(title,0,0,0);
		}
		break;
	}
pritam
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 991
Joined: Thu Nov 13, 2008 3:16 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Amiga, PSOne, NDS
Programming Language of Choice: C++
Location: Sweden

Re: error with switch statement

Post by pritam »

A switch statement should look more like this:

Code: Select all

switch( value ) {
	case 1:
		//Do something
		break;
	default:
		// Do something else
}
So, in your case:

Code: Select all

switch(gameState)
	{
	case STATE_TITLE:
		draw_surface(title,0,0,0);
		break;
	}
mary
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 33
Joined: Tue Apr 27, 2010 2:13 pm

Re: error with switch statement

Post by mary »

thanks for the help pritam, I found another problem, where I declared

Code: Select all

#define STATE_TITLE 0
as

Code: Select all

#define STATE_TITLE 0;
pritam
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 991
Joined: Thu Nov 13, 2008 3:16 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Amiga, PSOne, NDS
Programming Language of Choice: C++
Location: Sweden

Re: error with switch statement

Post by pritam »

Oh right, the semicolon isn't necessary in #define statements. Probably why your code wasn't working right as well.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: error with switch statement

Post by K-Bal »

Actually both versions are valid switch statements.
User avatar
GroundUpEngine
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 835
Joined: Sun Nov 08, 2009 2:01 pm
Current Project: mixture
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Location: UK

Re: error with switch statement

Post by GroundUpEngine »

Dont you just love semi-colons! :lol:
pritam
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 991
Joined: Thu Nov 13, 2008 3:16 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Amiga, PSOne, NDS
Programming Language of Choice: C++
Location: Sweden

Re: error with switch statement

Post by pritam »

K-Bal wrote:Actually both versions are valid switch statements.
Oh really? I had no idea, never seen that before.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: error with switch statement

Post by Ginto8 »

pritam wrote:
K-Bal wrote:Actually both versions are valid switch statements.
Oh really? I had no idea, never seen that before.
{ and } just indicate a block, which is pretty much any piece of code with its own scope. So his code is syntactically valid, except for the case STATE_TITLE: line, because that gets preprocessed to be case 0;: ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: error with switch statement

Post by K-Bal »

<object width="480" height="385"><param name="movie" value="http://www.youtube.com/v/HqljNQmWo-0&hl ... ram><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/HqljNQmWo-0&hl=de_DE&fs=1&" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="480" height="385"></embed></object>[/youtube]
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: error with switch statement

Post by lotios611 »

Ginto8 wrote:
pritam wrote:
K-Bal wrote:Actually both versions are valid switch statements.
Oh really? I had no idea, never seen that before.
{ and } just indicate a block, which is pretty much any piece of code with its own scope. So his code is syntactically valid, except for the case STATE_TITLE: line, because that gets preprocessed to be case 0;: ;)
To illustrate this, here's some code.

Code: Select all

int main()
{
    i = 10;
    someFunction();
    {
        otherFunction(i);
    }
}
I've seen this a few times, I really don't like it because it makes it look like what it's doing is defining a function write where it's called.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
Post Reply