Thread: problem on switch

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    Quote Originally Posted by MacGyver View Post
    Code:
    scanf("%d",&options);
    
    ....
    
    case 1: /* NOT NOT NOT NOT case '1' */
    As I said, 1 is 1. '1' is 49.
    oh is it like this?
    i typed 1,it really ouput u choose 1
    however, i found that if i type in a character
    it output " u choose 2"
    why?

    Code:
    int options;
     	  printf("\n\n\nOptions:");
     	  scanf("%d",&options);
    
    
    switch (options) {
    	   case 1:
    	   		printf("u choose 1");
    	   		break;

  2. #2
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    however, i found that if i type in a character
    it output " u choose 2"
    why?
    This is what we call "undefined behaviour". The switch statement branch to "case 2:" because the value of option was 2 at the time of the test. Why it was 2 ? Well it could have been anything else. If you try reading a number with scanf but the user enter something else, then nothing happens to your "storing variable".

    If you want the user to be able only to enter numbers, then this little piece of code will help you:

    Code:
    int i;
    int option;
    
    // ...
    
    do
    {
        printf("Options: ");
        i = scanf("%d", &option);
        while (getchar() != '\n');             // Empty the input buffer
    } while (i != 1);

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    Quote Originally Posted by foxman View Post
    This is what we call "undefined behaviour". The switch statement branch to "case 2:" because the value of option was 2 at the time of the test. Why it was 2 ? Well it could have been anything else. If you try reading a number with scanf but the user enter something else, then nothing happens to your "storing variable".

    If you want the user to be able only to enter numbers, then this little piece of code will help you:

    Code:
    int i;
    int option;
    
    // ...
    
    do
    {
        printf("Options: ");
        i = scanf("%d", &option);
        while (getchar() != '\n');             // Empty the input buffer
    } while (i != 1);



    yes, i can really prevent the characters with your code
    however if i type 7 (which is out of 1-6), it shows please enter again,
    then no matter what i input, the exe. window close itself
    it there something wrong with my switch ?

    thank you very much!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    A few of your problems could be solved by browsing and reading some of the FAQ entries.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Chinese pâté foxman's Avatar
    Join Date
    Jul 2007
    Location
    Canada
    Posts
    404
    Like iMalc said, you should look at the FAQ, there's useful stuff in there.

    no matter what i input, the exe. window close itself
    It does so because your program is structured that way. If you want it to ask for user input until the user gives a certain input, then you need to use a loop.

    however if i type 7 (which is out of 1-6), it shows please enter again
    Ok. Let's say you want the user to enter a number between 1 and 6 inclusively. Well it's quite simple:
    Code:
    int i;
    int option;
    
    // ...
    
    do
    {
        printf("Options: ");
        i = scanf("%d", &option);
        while (getchar() != '\n');             // Empty the input buffer
    } while (i != 1 || option < 1 || option > 6);
    That said, here is how i write a typical menu function:

    Code:
    int continu = 1;
    
    // ...
    
    do
    {
        // Print menu option here
        puts("1. Do something");
        puts("2. Exit");
    
        do
        {
            // Read user input here (as previously shown)
        } while (i != 1 || choix < 1 || choix > 2);
    
        switch(choix)
        {
        case 1:
            // Do something here
            break;
        default:
            continu = 0;
            break;
        }
    
    } while (continu);
    Also, note that you can output the "spade character" this way: printf("\6");.

    iMalc: You changed your avatar, right ? I think the last one was showing heapsort; is this one something like shaker sort ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  2. Switch Problem
    By Tynnhammar in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2004, 11:57 AM
  3. Replies: 1
    Last Post: 08-31-2004, 04:07 AM
  4. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM