switch statement

This is a discussion on switch statement within the C Programming forums, part of the General Programming Boards category; what is wrong with the following snippet? are intergers not allowed in switch statements? Code: #include <stdio.h> int main() { ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    31

    switch statement

    what is wrong with the following snippet? are intergers not allowed in switch statements?
    Code:
    #include <stdio.h>
    
    int
    main()
    {
            int i;
    
            while (1) {
                    printf("1 prints 1\n2 prints 2\n3 prints 3\n\n");
                    scanf("%i", i);
                    switch (i) {
                            case 1: printf("one");
                                            break;
                            case 2: printf("two");
                                            break;
                            case 3: printf("three");
                                            break;
                            default: printf("error");
                                            break;
                    }
            }
            return 0;
    }
    I get a segmentation fault when I run it.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You are running in a never-ending loop. Remove that while() loop.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    sorry, I posted this the lazy way. the loop is needed in the program this is for (this is just the part that's not working. In the actual program, each case statement has a function.

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,069
    Code:
    scanf("%i", &i);
    Sent from my iPadŽ

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    31
    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mutli Switch statement help
    By elwad in forum C Programming
    Replies: 9
    Last Post: 05-09-2009, 03:19 AM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 02:14 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. char switch statement
    By jmarsh56 in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2006, 05:04 PM
  5. Efficiency with the switch() statement...
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 10-14-2001, 02:47 PM

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