Thread: Switch and Case

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Switch and Case

    I read over the tutorials that were posted, but i'm having a little problem....

    In my program i'm prompting the user to enter a two digit number...so pretty much 10-99...and we are "assuming" they are going to enter that two digit number.
    Code:
       int num;
    
    printf("Enter a Two-Digit Number: \n");
    scanf("%d", &num);
    
    printf("The number Entered is: \n", num);
    
    switch (num) {
    case 10: printf("ten");       break;
    case 20: printf("twenty");    break;
    case 30: printf("thirty");    break;
    case 40: printf("fourty");    break;
    case 50: printf("fifty");     break;
    How would i set the program up to where if i enter 21 it prints twenty one.....

    i know it will grab twenty from the "case 20:" and i know i have to create a switch, case for single integers such as case 1: printf("one"); break;
    Last edited by ednnd; 10-07-2005 at 08:24 PM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ednnd
    How would i set the program up to where if i enter 21 it prints twenty one.....
    Code:
    case 21: printf("twenty-one"); break;
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Quote Originally Posted by Dave_Sinkula
    Code:
    case 21: printf("twenty-one"); break;


    i can't have a long program, so that elimnates me using


    Code:
    case 21: printf("twentyone")
    case 22: printf("twentytwo")
    case 23: printf("twentythree")

    if i did that the program would be long

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Well, then you'll have to break things down to significant digits. Perhaps a search of the board might be fruitful.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    Quote Originally Posted by Dave_Sinkula
    Well, then you'll have to break things down to significant digits. Perhaps a search of the board might be fruitful.

    so in order to do that it will look somthing like this...


    Code:
    #include <stdio.h>
    
      main()
    {
    
       int num;
    
       printf("Enter a Two-Digit Number:  \n");
       scanf(""%d", &num);
          printf("You entered the number:  \n", num);
    
    switch (num) {
    case 10: printf("ten"); break;
    case 20: printf("twenty); break;
    }
    
    switch (num) {
    case 1: printf("one"); break;
    }
    }

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    If num is 21, it will be neither 20 nor 1. To break it down, you'll need intermediate values for digits.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    I'm still new to this what do you mean?

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Haven't you done the obligatory, "break a number into digits using the % (mod) operator" assignment? If not, there are a few search terms.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    we are only on the 6th chapter ...i know if you take a number % (mod) another number your just finding a remainder .....

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    So what woud 20 / 10 be and 20 % 10 be? 2 and 1 perhaps?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    20 % 10 is 1 so how does that fit into my switch and cases?

  12. #12
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Perhaps one switch for tens and another for ones digits?

    [edit]Again, though, a search might be handy.
    Last edited by Dave_Sinkula; 10-07-2005 at 09:46 PM.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  13. #13
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    you divide by 10 to get the first digit, then remainder by 10 to get the second digit...like he said SEARCH there have been numerous posts on this very subject, that will give far more information than simply using modulus.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  4. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 02:08 PM
  5. opengl program as win API menu item
    By SAMSAM in forum Game Programming
    Replies: 1
    Last Post: 03-03-2003, 07:48 PM