Switch

This is a discussion on Switch within the C Programming forums, part of the General Programming Boards category; I got the foilowing code: Code: #include <stdio.h> main() { int x; printf("Write a number: \n"); scanf("%d", &x); switch (x); ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    99

    Switch

    I got the foilowing code:
    Code:
    #include <stdio.h>
    
    main()
    {
    
            int x;
            printf("Write a number: \n");
            scanf("%d", &x);
            switch (x);
            {
                    case 1: {  printf("A\n"); break; }
                    case 2: {  printf("AA\n"); break; }
                    case 3: {  printf("AAA\n"); break; }
                   default: {  printf("Wrong Number\n"); break; }
            }
    
    }
    i don't know what is the problem but when try compile i get this:

    gcc -o switch switch.c
    switch.c: In function `main':
    switch.c:11: case label not within a switch statement
    switch.c:12: case label not within a switch statement
    switch.c:13: case label not within a switch statement
    switch.c:14: `default' label not within a switch statement
    switch.c:11: break statement not within loop or switch
    switch.c:12: break statement not within loop or switch
    switch.c:13: break statement not within loop or switch
    switch.c:14: break statement not within loop or switch

    any ideas?

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    get rid of the {} braces on each case statement
    and you have an extra semi-colon in there
    Last edited by kermit; 12-23-2004 at 11:05 AM.

  3. #3
    Epo
    Epo is offline
    Registered User
    Join Date
    Jun 2003
    Posts
    361
    Code:
    switch (x);
            {
                    case 1: {  printf("A\n"); break; }
                    case 2: {  printf("AA\n"); break; }
                    case 3: {  printf("AAA\n"); break; }
                   default: {  printf("Wrong Number\n"); break; }
            }
    You don't need the ";" after the switch (x)


    EDIT: Ah, Kermit, too quick for me.

  4. #4
    Registered User
    Join Date
    Jul 2004
    Posts
    99
    thanks done,
    the problem was the " ; "

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    we know
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  2. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 07:06 AM
  3. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21