Problems with the switch statement
I am merrily programming along, and click on "Build and Run" which gives me an error. I am using a switch statement as depicted below, but am getting an error that says "case label not within switch statement" and "Break statement not within Loop or switch statement". What am I doing wrong?
Code:
- (void)drawRect:(NSRect)rect
{
switch([game state])
{
case STATE_TITLE:
[self displayTitle:rect];
break;
case STATE_PLAYING:
[self displayPlaying:rect];
break;
case STATE_GAME_OVER:
[self displayGameOver:rect];
break;
}
}
@end
There's nothing wrong with the type of variable itself.
You're using an enum type, and the switch knows you're missing one. In other words,
You'll get that error for not including case ThingThree: or a default:
You're using an enum type, and the switch knows you're missing one. In other words,
Code:
typedef enum {
ThingOne,
ThingTwo,
ThingThree
} Thing;
Thing thing = ThingOne;
switch (thing)
{
case ThingOne:
break;
case ThingTwo:
break;
}You'll get that error for not including case ThingThree: or a default:
I'm guessing some syntax error earlier in the file...
Heh. My brain totally interpreted that first error message differently. It's likely not what I wrote.
OneSadCookie Wrote:I'm guessing some syntax error earlier in the file...
I actually did have a parse error that I can't figure out, and a "Undeclared Function" error. Would that have something to do with it?
quite likely. You'll need to post your entire file probably, and all of your error messages.
-wyrmmage
-wyrmmage
Worlds at War (Current Project) - http://www.awkward-games.com/forum/
Also, always add default. I've never done this before, but say this happened:
You see? if you left the default out, in this case the switch would not know what to do—none of the conditionals fit the problem. So the compiler yells at you.
Also, about the undefined function and stuff, always track them down. Usually they're indicative of a larger problem—i.e. you spelled the function wrong, forgot to include a header (or included the wrong one), or something else. Lastly, use OSC's compiler flags. They make your code cleaner and generally avoid nasty compiler battle headaches.
On a totally unrelated note: Why aren't urls stylized in some manner by default?
Code:
typedef enum {
ThingOne,
ThingTwo,
ThingThree
} Thing;
Thing thingy = (Thing)4;
switch(thingy)
{
case ThingOne:
case ThingTwo:
case ThingThree:
printf("Woot!\n");
break;
default: // this is what will happen
printf("how the heck did we get here? we never declared anything aside from the aforementioned values!\n");
break;
}You see? if you left the default out, in this case the switch would not know what to do—none of the conditionals fit the problem. So the compiler yells at you.
Also, about the undefined function and stuff, always track them down. Usually they're indicative of a larger problem—i.e. you spelled the function wrong, forgot to include a header (or included the wrong one), or something else. Lastly, use OSC's compiler flags. They make your code cleaner and generally avoid nasty compiler battle headaches.
On a totally unrelated note: Why aren't urls stylized in some manner by default?
It's not magic, it's Ruby.
What if you're not using enums? Sometimes I just want to act if a variable is set to a couple of different values, for example:
If, for example, you just needed to run code when the game is starting and ending. Do you still have to include a default? My compiler doesn't tell me that I have to, so I've always just figured it ignored the switch statement if theInt was set to a value other than 0 or 5.
-wyrmmage
Code:
switch(theInt)
{
case 0:
cout << "game initializing";
break;
case 5:
cout << "game ending";
break;
}-wyrmmage
Worlds at War (Current Project) - http://www.awkward-games.com/forum/
You don't have to, it's just to trap something unexpected in case you want to do something (like return an error because it's not 0 or 5).=
Sorry about the short reply's I'm kind of hard-pressed for time to reply. Alright, so first of all, Thank you for all the reply's! It's really helpful. Secondly, I don't know how to add compiler flags, so if you could tell me how to do that, that would be nice. I can't figure out the parse error/undeclared function, so I've attached the source code for that file. Thanks again for your help!
and the errors/warnings
Code:
#include "GameView.h"
#include "Game.h"
#import "GameController.h"
@implementation GameView
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
}
return self;
}
- (void)displayTitle: (NSRect)rect
{
}
- (void)displayPlaying:(NSRect)rect
{
[[NSColor blueColor] set];
NSRectFill([self bounds]);
NSBezierPath *pathCells = [NSBezierPath bezierPath]; //path for drawing the Cells
int row, column;
for(row = 0; row < BOARD_SIZE; row++)
{
for(column = 0; column< BOARD_SIZE; column++)
{
if([[game cellAtRow:row
AndColumn:column] owner] == 1)
{
[pathCells appendBezierPathWithOvalInRect:NSMakeRect(
(row * CELL_SIZE) + 4 + (CELL_SIZE / 2),
(column * CELL_SIZE) + 4 + (CELL_SIZE / 2),
CELL_SIZE - 4,
CELL_SIZE - 4)];
}
}
}
[[NSColor whiteColor] set];
[pathCells fill];
[pathCells removeAllPoints];
for(row = 0; row < BOARD_SIZE; row++)
{
for(column = 0; column < BOARD_SIZE; column++)
{
if([[game cellAtRow:row
AndColumn:column] owner] == 1)
{
[pathCells appendBezierPathWithOvalnRect:NSMakeRect(
(row * CELL_SIZE) + 4 + (CELL_SIZE / 2),
(column * CELL_SIZE) + + (CELL_SIZE / 2),
CELL_SIZE - 4,
CELL_SIZE - 4)];
}
}
}
[[NSColor blackColor] set];
[pathCells fill];
- (void)displayGameOver:(NSRect)rect
{
}
- (void)drawRect:(NSRect)rect
{
switch([game state])
{
case STATE_TITLE:
[self displayTitle:rect];
break;
case STATE_PLAYING:
[self displayPlaying:rect];
break;
case STATE_GAME_OVER:
[self displayGameOver:rect];
break;
}
}
@endand the errors/warnings
Code:
Building target “FlipSquare†of project “FlipSquare†with configuration “Debug†— (16 errors, 7 warnings)
cd /Users/lincolngreen/FlipSquare
/usr/bin/gcc-4.0 -x objective-c -arch i386 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -g -O0 -Wreturn-type -Wunused-variable -fmessage-length=0 -fzero-link -mfix-and-continue -mmacosx-version-min=10.4 -I/Users/lincolngreen/FlipSquare/build/FlipSquare.build/Debug/FlipSquare.build/FlipSquare.hmap -F/Users/lincolngreen/FlipSquare/build/Debug -I/Users/lincolngreen/FlipSquare/build/Debug/include -I/Users/lincolngreen/FlipSquare/build/FlipSquare.build/Debug/FlipSquare.build/DerivedSources -isysroot /Developer/SDKs/MacOSX10.4u.sdk -c /Users/lincolngreen/FlipSquare/GameView.m -o /Users/lincolngreen/FlipSquare/build/FlipSquare.build/Debug/FlipSquare.build/Objects-normal/i386/GameView.o
/Users/lincolngreen/FlipSquare/GameView.m: In function '-[GameView displayPlaying:]':
/Users/lincolngreen/FlipSquare/GameView.m:83: warning: 'NSBezierPath' may not respond to '-appendBezierPathWithOvalnRect:'
/Users/lincolngreen/FlipSquare/GameView.m:83: warning: (Messages without a matching method signature
/Users/lincolngreen/FlipSquare/GameView.m:83: warning: will be assumed to return 'id' and accept
/Users/lincolngreen/FlipSquare/GameView.m:83: warning: '...' as arguments.)
/Users/lincolngreen/FlipSquare/GameView.m:91: error: 'displayGameOver' undeclared (first use in this function)
/Users/lincolngreen/FlipSquare/GameView.m:91: error: (Each undeclared identifier is reported only once
/Users/lincolngreen/FlipSquare/GameView.m:91: error: for each function it appears in.)
/Users/lincolngreen/FlipSquare/GameView.m:91: error: parse error before ':' token
/Users/lincolngreen/FlipSquare/GameView.m:106: error: break statement not within loop or switch
/Users/lincolngreen/FlipSquare/GameView.m:108: error: case label not within a switch statement
/Users/lincolngreen/FlipSquare/GameView.m:112: error: break statement not within loop or switch
/Users/lincolngreen/FlipSquare/GameView.m:115: error: case label not within a switch statement
/Users/lincolngreen/FlipSquare/GameView.m:119: error: break statement not within loop or switch
/Users/lincolngreen/FlipSquare/GameView.m: At top level:
/Users/lincolngreen/FlipSquare/GameView.m:122: error: parse error before '}' token
/Users/lincolngreen/FlipSquare/GameView.m:124: warning: incomplete implementation of class 'GameView'
/Users/lincolngreen/FlipSquare/GameView.m:124: warning: method definition for '-displayGameOver:' not found
/Users/lincolngreen/FlipSquare/GameView.m:83: warning: 'NSBezierPath' may not respond to '-appendBezierPathWithOvalnRect:'
/Users/lincolngreen/FlipSquare/GameView.m:83: warning: (Messages without a matching method signature
/Users/lincolngreen/FlipSquare/GameView.m:91: error: 'displayGameOver' undeclared (first use in this function)
/Users/lincolngreen/FlipSquare/GameView.m:91: error: parse error before ':' token
/Users/lincolngreen/FlipSquare/GameView.m:106: error: break statement not within loop or switch
/Users/lincolngreen/FlipSquare/GameView.m:108: error: case label not within a switch statement
/Users/lincolngreen/FlipSquare/GameView.m:112: error: break statement not within loop or switch
/Users/lincolngreen/FlipSquare/GameView.m:115: error: case label not within a switch statement
/Users/lincolngreen/FlipSquare/GameView.m:119: error: break statement not within loop or switch
/Users/lincolngreen/FlipSquare/GameView.m: At top level:
/Users/lincolngreen/FlipSquare/GameView.m:122: error: parse error before '}' token
/Users/lincolngreen/FlipSquare/GameView.m:124: warning: incomplete implementation of class 'GameView'
/Users/lincolngreen/FlipSquare/GameView.m:124: warning: method definition for '-displayGameOver:' not found
cd /Users/lincolngreen/FlipSquare
/usr/bin/gcc-4.0 -x objective-c -arch i386 -pipe -Wno-trigraphs -fpascal-strings -fasm-blocks -g -O0 -Wreturn-type -Wunused-variable -fmessage-length=0 -fzero-link -mfix-and-continue -mmacosx-version-min=10.4 -I/Users/lincolngreen/FlipSquare/build/FlipSquare.build/Debug/FlipSquare.build/FlipSquare.hmap -F/Users/lincolngreen/FlipSquare/build/Debug -I/Users/lincolngreen/FlipSquare/build/Debug/include -I/Users/lincolngreen/FlipSquare/build/FlipSquare.build/Debug/FlipSquare.build/DerivedSources -isysroot /Developer/SDKs/MacOSX10.4u.sdk -c /Users/lincolngreen/FlipSquare/Game.m -o /Users/lincolngreen/FlipSquare/build/FlipSquare.build/Debug/FlipSquare.build/Objects-normal/i386/Game.o
/Users/lincolngreen/FlipSquare/Game.m: In function '-[Game init]':
/Users/lincolngreen/FlipSquare/Game.m:16: error: parse error before numeric constant
/Users/lincolngreen/FlipSquare/Game.m:16: error: parse error before ')' token
/Users/lincolngreen/FlipSquare/Game.m:22: error: parse error before '{' token
/Users/lincolngreen/FlipSquare/Game.m:25: warning: control reaches end of non-void function
/Users/lincolngreen/FlipSquare/Game.m: In function '-[Game dealloc]':
/Users/lincolngreen/FlipSquare/Game.m:41: error: parse error before 'for'
/Users/lincolngreen/FlipSquare/Game.m:41: error: 'row' undeclared (first use in this function)
/Users/lincolngreen/FlipSquare/Game.m:41: error: (Each undeclared identifier is reported only once
/Users/lincolngreen/FlipSquare/Game.m:41: error: for each function it appears in.)
/Users/lincolngreen/FlipSquare/Game.m:41: error: parse error before ')' token
/Users/lincolngreen/FlipSquare/Game.m:45: error: parse error before ')' token
/Users/lincolngreen/FlipSquare/Game.m:37: warning: unused variable 'roe'
/Users/lincolngreen/FlipSquare/Game.m: At top level:
/Users/lincolngreen/FlipSquare/Game.m:50: warning: method possibly missing a [super dealloc] call
/Users/lincolngreen/FlipSquare/Game.m:60: error: parse error before '[' token
/Users/lincolngreen/FlipSquare/Game.m: In function '-[Game resetBoard]':
/Users/lincolngreen/FlipSquare/Game.m:81: warning: 'Cell' may not respond to '-setOwner:'
/Users/lincolngreen/FlipSquare/Game.m:81: warning: (Messages without a matching method signature
/Users/lincolngreen/FlipSquare/Game.m:81: warning: will be assumed to return 'id' and accept
/Users/lincolngreen/FlipSquare/Game.m:81: warning: '...' as arguments.)
/Users/lincolngreen/FlipSquare/Game.m:16: error: parse error before numeric constant
/Users/lincolngreen/FlipSquare/Game.m:16: error: parse error before ')' token
/Users/lincolngreen/FlipSquare/Game.m:22: error: parse error before '{' token
/Users/lincolngreen/FlipSquare/Game.m:25: warning: control reaches end of non-void function
/Users/lincolngreen/FlipSquare/Game.m:41: error: parse error before 'for'
/Users/lincolngreen/FlipSquare/Game.m:41: error: 'row' undeclared (first use in this function)
/Users/lincolngreen/FlipSquare/Game.m:41: error: parse error before ')' token
/Users/lincolngreen/FlipSquare/Game.m:45: error: parse error before ')' token
/Users/lincolngreen/FlipSquare/Game.m:37: warning: unused variable 'roe'
/Users/lincolngreen/FlipSquare/Game.m: At top level:
/Users/lincolngreen/FlipSquare/Game.m:50: warning: method possibly missing a [super dealloc] call
/Users/lincolngreen/FlipSquare/Game.m:60: error: parse error before '[' token
/Users/lincolngreen/FlipSquare/Game.m:81: warning: 'Cell' may not respond to '-setOwner:'
/Users/lincolngreen/FlipSquare/Game.m:81: warning: (Messages without a matching method signature
Build failed (16 errors, 7 warnings)
Start by indenting your code properly, that way you'll be able to see at a glance whether you've got the right number of {s and }s...
Wow, that's a lot of stuff.
A. There's many more problems than just that switch statement.
b. it's appendBezierPathWithOvalInRect, not appendBezierPathWithOvalnRect.
c. Look at the bracketing to displayPlaying: you close it WAY after it should have been closed.
That's all I could glean, but I'm sure there's stuff I'm missing.
A note about bracketing: let the ide do the automatic indentation. That way it's pretty easy to figure out if you're missing a bracket somewhere.
A. There's many more problems than just that switch statement.
b. it's appendBezierPathWithOvalInRect, not appendBezierPathWithOvalnRect.
c. Look at the bracketing to displayPlaying: you close it WAY after it should have been closed.
That's all I could glean, but I'm sure there's stuff I'm missing.
A note about bracketing: let the ide do the automatic indentation. That way it's pretty easy to figure out if you're missing a bracket somewhere.
It's not magic, it's Ruby.
Nayr Wrote:Wow, that's a lot of stuff.
A. There's many more problems than just that switch statement.
b. it's appendBezierPathWithOvalInRect, not appendBezierPathWithOvalnRect.
c. Look at the bracketing to displayPlaying: you close it WAY after it should have been closed.
That's all I could glean, but I'm sure there's stuff I'm missing.
A note about bracketing: let the ide do the automatic indentation. That way it's pretty easy to figure out if you're missing a bracket somewhere.
b. That fixed 8 of my errors

c. When should I have closed it?

How do you let it do automatic indentation?
c: Try indenting properly and it'll be obvious. It's obvious even without indenting. In what you pasted, you have code that appears to be outside of a method. That's obviously wrong. What it really is, is bad indenting and an unclosed method.
You were also missing '4' between some plus signs.
Use a lowercase 'a' in AndColumn. It's convention.
You were also missing '4' between some plus signs.
Code:
#include "GameView.h"
#include "Game.h"
#import "GameController.h"
@implementation GameView
- (id)initWithFrame:(NSRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) != nil) {
// Add initialization code here
}
return self;
}
- (void)displayTitle: (NSRect)rect
{
}
- (void)displayPlaying:(NSRect)rect
{
[[NSColor blueColor] set];
NSRectFill([self bounds]);
NSBezierPath *pathCells = [NSBezierPath bezierPath]; //path for drawing the Cells
int row, column;
for (row = 0; row < BOARD_SIZE; row++) {
for (column = 0; column< BOARD_SIZE; column++) {
if ([[game cellAtRow:row andColumn:column] owner] == 1) {
[pathCells appendBezierPathWithOvalInRect:
NSMakeRect( (row * CELL_SIZE) + 4 + (CELL_SIZE / 2),
(column * CELL_SIZE) + 4 + (CELL_SIZE / 2),
CELL_SIZE - 4,
CELL_SIZE - 4 )];
}
}
}
[[NSColor whiteColor] set];
[pathCells fill];
[pathCells removeAllPoints];
for (row = 0; row < BOARD_SIZE; row++) {
for (column = 0; column < BOARD_SIZE; column++) {
if ([[game cellAtRow:row andColumn:column] owner] == 1) {
[pathCells appendBezierPathWithOvalInRect:
NSMakeRect( (row * CELL_SIZE) + 4 + (CELL_SIZE / 2),
(column * CELL_SIZE) + 4 + (CELL_SIZE / 2),
CELL_SIZE - 4,
CELL_SIZE - 4 )];
}
}
}
[[NSColor blackColor] set];
[pathCells fill];
}
- (void)displayGameOver:(NSRect)rect
{
}
- (void)drawRect:(NSRect)rect
{
switch([game state]) {
case STATE_TITLE:
[self displayTitle:rect];
break;
case STATE_PLAYING:
[self displayPlaying:rect];
break;
case STATE_GAME_OVER:
[self displayGameOver:rect];
break;
}
}
@endUse a lowercase 'a' in AndColumn. It's convention.
Hmm, I was typing what was in the tutorial. But I will start indenting properly(if indeed what was in the tutorial was wrong.)

