Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Darkroom

Guest
Original poster
Dec 15, 2006
2,445
0
Montréal, Canada
so i'm studying beginner C but i've got mad problems with this switch function in xcode:

New Project > Command Line Utility > Standard Tool

Code:
#include <stdio.h>

main ()
	{
	int number;
	printf("Make a choice (1 / 2 / 3): ");
		do
		{
		scanf("%d", &number);
		switch (number);	
			{
			case (1) : printf("You chose #1\n");
			break;
			case (2) : printf("You chose #2\n");
			break;
			case (3) : printf("You chose #3\n");
			break;

			default : printf("Invalid Choice (\"%d\").  Please Select Again.\n", number);
			}
		}
		while ((number < 1) || (number > 3));
	
	return 0;
	}

the errors i'm receiving state "Case Label Not Within Switch Statement", but they totally are, aren't they?!

i also get that weird warning "Return Type Defaults To Int" if i write "main()" instead of "int main()"...
 

Muster Mark

macrumors newbie
Sep 9, 2008
8
0
Natick, MA.
I am also a beginner C programmer,

and make mistakes like that all the time. One thing I noticed, other than the semicolon, is that you have parenthesis around your case numbers. I don't know if this would cause a problem, but I do know it would work without them.

switch (number){
case 1: printf(--------);
break;
case 2: printf(--------);

etc...}

Cheers!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Watch for this with your loops, too:
Code:
int x = 0;
for(x = 0; x < 10; x++);
{
  printf("X is: %d\n",x);
}

This is perfectly valid code, but the output will be:
X is 10

Not at all what you were hoping for.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.