Like Tree1Likes
  • 1 Post By grumpy

Switch Statement (For Beginner)

This is a discussion on Switch Statement (For Beginner) within the C Programming forums, part of the General Programming Boards category; Sorry, beginning programmer. Assignment : Take an integer keyed in from the terminal and extract and display each digit of ...

  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,528
    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".
    Crossfire likes this.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy in reply to you, it is likely you deserve it. Suck it up, sunshine, 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, 12:50 PM
  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, 04:35 AM
  5. switch statement
    By crash88 in forum C Programming
    Replies: 4
    Last Post: 06-29-2006, 01:49 AM

Tags for this Thread


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