Page 1 of 1

error with switch statement

Posted: Mon May 10, 2010 12:38 pm
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;
	}

Re: error with switch statement

Posted: Mon May 10, 2010 12:48 pm
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;
	}

Re: error with switch statement

Posted: Mon May 10, 2010 1:05 pm
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;

Re: error with switch statement

Posted: Mon May 10, 2010 1:14 pm
by pritam
Oh right, the semicolon isn't necessary in #define statements. Probably why your code wasn't working right as well.

Re: error with switch statement

Posted: Mon May 10, 2010 5:00 pm
by K-Bal
Actually both versions are valid switch statements.

Re: error with switch statement

Posted: Mon May 10, 2010 5:05 pm
by GroundUpEngine
Dont you just love semi-colons! :lol:

Re: error with switch statement

Posted: Mon May 10, 2010 5:41 pm
by pritam
K-Bal wrote:Actually both versions are valid switch statements.
Oh really? I had no idea, never seen that before.

Re: error with switch statement

Posted: Mon May 10, 2010 5:46 pm
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;: ;)

Re: error with switch statement

Posted: Tue May 11, 2010 2:33 am
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]

Re: error with switch statement

Posted: Tue May 11, 2010 5:44 am
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.