Thread: problem on switch

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    13

    problem on switch

    i am writing a menu by using switch
    however it is not successful
    i typed the option 1; but it seems it could not recognize and output [Please enter again]. Then i can't input anything, the screen flashed away...

    also is it a bit clumsy for the code?

    besides, i have one more question
    i've attached a printscreen,
    i would like to know how can i make the words printed in center? [green circle]

    thanks!


    Code:
    #include <stdio.h>
    
    void mypause ( void ) 
    { 
      printf ( "\n\nPlease press [Enter] to continue . . ." );
      fflush ( stdout );
      getchar();
    } 
    
    
    main()
    {
    
    printf("%c%c 2007-2008",006,006);
    printf("%14c%c\n",006,006);
         
    printf("%c%c Hello",006,006);
    printf("%18c%c\n\n",006,006);
    
    printf("%c%c World",006,006);
    printf("%18c%c\n",006,006);
    
    printf("%c%c Welcome",006,006);
    printf("%16c%c\n\n\n\n\n",006,006);
    
    
    printf("%c%c merry christmas! ",006,006);
    printf("%6c%c\n",006,006);
    
    mypause();
    
    
    printf("\n\n\nPlease choose your options [ 1-6 ]\n\n");
    printf("|| 1.");
    printf("%10c%c\n",'|','|');
    printf("|| 2.");
    printf("%10c%c\n",'|','|');
    printf("|| 3.");
    printf("%10c%c\n",'|','|');
    printf("|| 4.");
    printf("%10c%c\n",'|','|');
    printf("|| 5.");
    printf("%10c%c\n",'|','|');
    printf("|| 6.");
    printf("%10c%c\n",'|','|');
    
    
    int options;
     	  printf("\n\n\nOptions:");
     	  scanf("%d",&options);
    
    //after choosing
    
    switch (options) {
    	   case '1':
    	   		printf("u choose 1");
    	   		break;
           case '2':
                printf("u choose 2");
                break;
           case '3':
    	   		printf("u choose 3");
    	   		break;
           case '4':
    	   		printf("u choose 4");
    	   		break;
           case '5':
    	   		printf("u choose 5");
    	   		break;
           case '6':
    	   		printf("u choose 6");
    	   		break;
           default:
    	        printf("Please enter again [ 1-6 ]");
           }
                
    	  
    	 system("PAUSE");
    	  }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You're reading options as an int and then comparing it to the character representations of numbers. This is wrong. Either read it as a char and compare it as you're doing, or leave the int as it is and change the comparison to compare to numbers. I would do the latter.

    Hint: 1 is 1. '1' is the character representation of 1, which is 49 in ASCII.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The normal way to center text is:

    1) get the length of the string of text you want to center, including spaces.

    2) Use the equation 40 - (string length/2 - 1) == number of spaces before the first char of the string should be printed.

    3) print that number of spaces, then print the text + /n

    You can't perfectly center a line with an even number of char's, when in a console (text) window.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    i can do the first one u suggested

    Code:
    char options;
     	  printf("\n\n\nOptions:");
     	  scanf("%c",&options);
    yes, it can run successfully.
    but i have no idea on your latter suggestion...

    it seems impossible if i change in case...
    switch (options) {
    case '49':
    printf("u choose 1");

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    scanf("&#37;d",&options);
    
    ....
    
    case 1: /* NOT NOT NOT NOT case '1' */
    As I said, 1 is 1. '1' is 49.

  6. #6
    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;

  7. #7
    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);

  8. #8
    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!

  9. #9
    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"

  10. #10
    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 ?

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    13
    thanks for help!
    i know how to do

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I also have some word of advice for you...
    First, main should explicitly return int. main() is bad, int main() is correct.
    Second, it's not recommended to mix tabs and spaces. It will mess up formatting when you copy it out of your editor/IDE. So use one and stick with it.
    Work a little on the indentation. Each block should be indented once. So all the code inside each { and } should get another indent from the code outside.
    And while we're at it, keep indentation consistent. Use only X number of tabs or X number of spaces everywhere to indent one level. 1 tab or 4 spaces are recommended.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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