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
mary
Chaos Rift Newbie
Posts: 33 Joined: Tue Apr 27, 2010 2:13 pm
Post
by mary » Mon May 10, 2010 12:38 pm
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
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
Post
by pritam » Mon May 10, 2010 12:48 pm
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
Posts: 33 Joined: Tue Apr 27, 2010 2:13 pm
Post
by mary » Mon May 10, 2010 1:05 pm
thanks for the help pritam, I found another problem, where I declared
as
pritam
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
Post
by pritam » Mon May 10, 2010 1:14 pm
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
Posts: 701 Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:
Post
by K-Bal » Mon May 10, 2010 5:00 pm
Actually both versions are valid switch statements.
GroundUpEngine
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
Post
by GroundUpEngine » Mon May 10, 2010 5:05 pm
Dont you just love semi-colons!
pritam
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
Post
by pritam » Mon May 10, 2010 5:41 pm
K-Bal wrote: Actually both versions are valid switch statements.
Oh really? I had no idea, never seen that before.
Ginto8
ES Beta Backer
Posts: 1064 Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java
Post
by Ginto8 » Mon May 10, 2010 5:46 pm
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.
lotios611
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++
Post
by lotios611 » Tue May 11, 2010 5:44 am
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