Thread: Switch Statement (For Beginner)

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    7

    Switch Statement (For Beginner)

    Sorry, beginning programmer. Assignment: Take an integer keyed in from the terminal and extract and display each digit of the integer in English. Ex. 932 --> nine three two

    Code:
     /*This program takes an integer keyed in from the terminal and extracts and displays each digit of the integer in English.*/
    
    
    #include<stdio.h>
    int main(void)
    {
            //DECLARE VARIABLES
            int num;
    
    
            //ASK USER FOR INPUT; STORE
            printf("Please enter an integer: ");
            scanf("%d", &num);
    
    
            //SWITCH STATMENT
            switch (num)
            {
    
    
                    case 0:
                            printf("zero\n");
                            break;
                    case 1:
                            printf("one\n");
                            break;
                    case 2:
                            printf("two\n");
                            break;
                    case 3:
                            printf("three\n");
                            break;
                    case 4:
                            printf("four\n");
                            break;
                    case 5:
                            printf("five\n");
                            break;
                    case 6:
                            printf("six\n");
                            break;
                    case 7:
                            printf("seven\n");
                            break;
                    case 8:
                            printf("eight\n");
                            break;
                    case 9:
                            printf("nine\n");
                            break;
                    default:
                            printf("none");
                            break;
            }
    
            return 0;
    }
    I don't know how the program works if the integer is more than one digit. Any hints would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The way it works now is that, if a value of 10 or more (i.e. more than one digit) is entered, the code will print out "none".

    You need to implement a loop that repeatedly divides by 10 and extracts modulo 10 to extract the individual digits. The catch is the "natural" way of doing that will print the digits in reverse order. That means you need to store each digit in (say) an array, so you can then output the results in the desired order.

    And, since this is a homework problem, that's all the advice you'll get. This site has a homework policy that amounts to "You have do do your own homework, not expect it to be done".
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch statement problems: beginner in C
    By cameuth in forum C Programming
    Replies: 4
    Last Post: 11-21-2011, 04:46 PM
  2. Need help with a switch statement
    By k_smith in forum C Programming
    Replies: 2
    Last Post: 07-12-2011, 11:50 AM
  3. Switch statement
    By BSmith4740 in forum C Programming
    Replies: 29
    Last Post: 02-28-2008, 08:28 PM
  4. Switch statement help
    By GeorgeV in forum C++ Programming
    Replies: 6
    Last Post: 08-07-2007, 03:35 AM
  5. switch statement
    By crash88 in forum C Programming
    Replies: 4
    Last Post: 06-29-2006, 12:49 AM

Tags for this Thread