Thread: Nested SWITCH statement is not working for unknown reason

  1. #1
    Registered User
    Join Date
    Feb 2018
    Posts
    8

    Question Nested SWITCH statement is not working for unknown reason

    Hi guys.

    For some unknown reason, my code is not working. It works during the first switch, but after scanning for the first input, the program terminates unexpectedly.

    Can you give some feedback about what is wrong with it?

    Thanks in advanced.


    Code:
    /*
    
        Write a program in BASES.C which offers the user a choice of converting integers between
        octal, decimal and hexadecimal. Prompt the user for either o, d or h and read the number
        in the chosen format. Then prompt the user for the output format (again o, d or h) and print
        the number out accordingly.
        A nice enhancement would be to offer the user only the different output formats, i.e. if o is
        chosen and an octal number read, the user is offered only d and h as output format.
    
    
    */
    
    
    #include <stdio.h>
    
    
    void main ()
    {
        int number;
        char option1, option2;
    
    
    
    
        printf("Choose an input number format:\n");
        printf("\td - decimal\n\to - octal\n\th - hexadecimal\n");
    
    
        scanf("%c", &option1);
    
    
        switch (option1)
        {
            case 'd':
                printf ("Enter the a number in the chosen format:\n");
                scanf ("%d", &number);
                printf ("Choose an output number format:\n");
                printf ("\to - octal\n\th - hexadecimal\n");
                scanf ("%c", &option2);
                switch (option2)
                {
                    case 'o':
                        printf("The decimal number %d in octal base is: %o\n", number, number);
                        break;
                    case 'h':
                        printf("The decimal number %d in hexadecimal base is: %x\n", number, number);
                        break;
                    default:
                        printf ("Unknown option chosen.\n");
                        break;
                }
                break;
            case 'o':
                printf ("Enter the a number in the chosen format:\n");
                scanf ("%o", &number);
                printf("Choose an output number format:\n");
                printf("\td - decimal\n\th - hexadecimal\n");
                scanf("%c", &option2);
                switch (option2)
                {
                    case 'd':
                        printf("The octal number %o in decimal base is: %d\n", number, number);
                        break;
                    case 'h':
                        printf("The octal number %o in hexadecimal base is: %x\n", number, number);
                        break;
                    default:
                        printf ("Unknown option chosen.\n");
                        break;
                }
                break;
            case 'h':
                printf ("Enter the a number in the chosen format:\n");
                scanf ("%x", &number);
                printf("Choose an output number format:\n");
                printf("\to - octal\n\td - decimal\n");
                scanf("%c", &option2);
                switch (option2)
                {
                    case 'o':
                        printf("The hexadecimal number %x in octal base is: %o\n", number, number);
                        break;
                    case 'd':
                        printf("The hexadecimal number %x in decimal base is: %d\n", number, number);
                        break;
                    default:
                        printf ("Unknown option chosen.\n");
                        break;
                }
                break;
            default:
                printf ("Unknown option chosen.\n");
                break;
        }
    
    
        // getchar ();
    }

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,775
    I saw this line and stopped looking

    Code:
    scanf ("%c", &option2);
    When a newbie uses "%c" with scanf I always suggest they lookup what " %c" does instead when used with scanf. It fixes over half of the program input issues.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Registered User
    Join Date
    Feb 2018
    Posts
    8
    I've found the solution. The problem was in how the input in scanf was being introduced. A blank space was needed before the format specifier %c

    This is:

    Code:
    scanf(" %c", &option2);
    Notice the blank space before %c

    I found a good explanation here.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-20-2017, 03:53 PM
  2. switch-case statement not working properly
    By Shinzu911 in forum C Programming
    Replies: 11
    Last Post: 04-25-2012, 10:41 AM
  3. Replacing switch statement with a nested if/else
    By dev123 in forum C Programming
    Replies: 27
    Last Post: 09-06-2010, 12:30 AM
  4. cin.get(); not working with my switch statement
    By tenor_jazz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-12-2008, 10:33 PM
  5. default switch statement not working
    By gL_nEwB in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2006, 10:13 AM

Tags for this Thread