Thread: Need help on Switch statement!

  1. #1
    Registered User
    Join Date
    Oct 2014
    Posts
    14

    Need help on Switch statement!

    I am attempting to write a program that asks the user to enter an integer and writes each digit in English.
    For example: 234 would be two three four or 12 would be one two

    This is what I have so far.

    Code:
    #include <stdio.h>
    
    int main (void)
    { 
       int x;
    
       printf ("\tPlease enter an integer:");
       scanf ("%d", &x);
       printf ("\tYou have entered:\n");
    
       switch (x) {
        case 1:
        printf ("one");
        break;
        case 2:
        printf ("two");
        break;
        case 3:
        printf ("three");
        break;
        case 4:
        printf ("four");
        break;
        case 5:
        printf ("five");
        break;
        case 6:
        printf ("six");
        break;
        case 7:
        printf ("seven");
        break;
        case 8:
        printf ("eight");
        break;
        case 9:
        printf ("nine");
        break;
      }
        printf ("\n");
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Did you have a specific question?

    Or at least an idea of how you might approach this problem?

  3. #3
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    No I dont know what I'm doing wrong. The problem works right only when I enter one digit but when I enter two digits it responds with nothing.
    Last edited by Tanieka; 11-05-2014 at 10:43 AM.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    You should read the number in as a string and loop through the digits.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You can peel off digits from the right side using / and %, but will require a bit more work to take digits from the left side.

    Elkvis' suggestion of using a string would probably be easiest.

  6. #6
    Registered User
    Join Date
    Nov 2014
    Posts
    2

    Please try the below code

    Hi Tanieka, I am new to the forum and learning C from scrap.
    Hope the below code does what you want.. I have done it using a string as above people suggests...i have limited the number of digits to ten, if you want you can increase or decrease them.
    Please let me know.. if it does good to you..or need some more help.

    Code:
    //Program written by RajeshSS on 11:40PM 5-NOV-2014
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    void printinwords(char c);
    int main()
    {
    int i,j;
     char x[10];
     printf("Please enter a number: \n");
     gets(x);
     printf("number is %s \n",x);
     printf("number in words is \n");
     j=strlen(x);
    for(i=0;i<j;i++)
    {
        printinwords(x[i]);
        printf(" ");
    }
    
    
    }
    printinwords(char c)
    {
        switch(c)
        {
        case '0':
            printf("zero");
            break;
        case '1':
            printf("one");
            break;
        case '2':
            printf("two");
            break;
        case '3':
            printf("three");
            break;
        case '4':
            printf("four");
            break;
        case '5':
            printf("five");
            break;
        case '6':
            printf("six");
            break;
        case '7':
            printf("seven");
            break;
        case '8':
            printf("eight");
            break;
        case '9':
            printf("nine");
            break;
        }
    
    
    
    
    }

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    @rajeshss: Welcome to the forum. While it's good you want to help, you should avoid handing out complete solutions as this robs the OP of a learning experience. We try to give out hints that lead people in the right direction, so they can arrive at the answers themselves.

    Also, be sure to check your compiler warnings:

    Code:
    main.c|25|warning: return type defaults to 'int'|
    main.c|24|warning: conflicting types for 'printinwords'|
    main.c|6|note: previous declaration of 'printinwords' was here|
    You forgot the return type in your function definition.

    Code:
    gets(x);
    Don't use "gets()":
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    Welcome aboard!

  8. #8
    Registered User
    Join Date
    Oct 2014
    Posts
    14
    Thanks guys for the help. I have a few questions though. What does the #include <sting.h> mean? and Is the for loop helping with entering more than one digit?

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you don't need string.h or strlen().

    try something like this:

    Code:
    int i = 0;
    while (char c = x[i++])
    {
        printinwords(c);
        printf(" ");
    }
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  10. #10
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What does the #include <sting.h> mean?
    It's "string" (not "sting"), and it's a standard header file that defines string-related functions:

    <cstring> (string.h) - C++ Reference

    and Is the for loop helping with entering more than one digit?
    If you're referring to the code posted by rajeshss, then no - it has nothing to do with entering any input. Try reading the code slowly and carefully, and you should be able to see what is actually happening.

  11. #11
    Registered User
    Join Date
    Nov 2014
    Posts
    2
    Quote Originally Posted by Matticus View Post
    @rajeshss: Welcome to the forum. While it's good you want to help, you should avoid handing out complete solutions as this robs the OP of a learning experience. We try to give out hints that lead people in the right direction, so they can arrive at the answers themselves.

    Also, be sure to check your compiler warnings:

    Code:
    main.c|25|warning: return type defaults to 'int'|
    main.c|24|warning: conflicting types for 'printinwords'|
    main.c|6|note: previous declaration of 'printinwords' was here|
    You forgot the return type in your function definition.

    Code:
    gets(x);
    Don't use "gets()":
    FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com
    FAQ > Get a line of text from the user/keyboard (C) - Cprogramming.com

    Welcome aboard!
    Hi, Matticus. Thank you for your warm welcome. First of all sorry about revealing the entire code. I was very enthusiast to post the code once it got the required result. I will try to follow the forum rules and guidelines from senior members henceforth. And Thank you for your comments on gets() etc... I will read to it.
    Thank you very much!

    Quote Originally Posted by Elkvis View Post
    you don't need string.h or strlen().

    try something like this:

    Code:
    int i = 0;
    while (char c = x[i++])
    {
        printinwords(c);
        printf(" ");
    }
    Hi Elkvis, really wondering that there exist more ways to get common function result instead of adding header files, Thank you for helping me to think on using minimal header file addition. I have a question does that really bring better efficiency or any better performance for the program..if i decrease the header file addtions and use own functions for smaller tasks as you did?

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by rajeshss View Post
    does that really bring better efficiency or any better performance for the program..if i decrease the header file addtions and use own functions for smaller tasks as you did?
    It will improve performance, because it will only have to loop through the characters in the string once. Strlen loops until it finds an ASCII NUL in the string. Since that is what I did in my example, the call to strlen() can be removed.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  13. #13
    Registered User Mike Garber's Avatar
    Join Date
    Sep 2013
    Posts
    59
    Quote Originally Posted by Elkvis View Post
    you don't need string.h or strlen().

    try something like this:

    Code:
    int i = 0;
    while (char c = x[i++])
    {
        printinwords(c);
        printf(" ");
    }
    really cool solution here, E. I didn't know we could set the while loop up like that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement help
    By Tim Yap in forum C++ Programming
    Replies: 27
    Last Post: 10-25-2013, 01:34 AM
  2. Switch Statement Help
    By IMMORTALX in forum C Programming
    Replies: 8
    Last Post: 09-06-2011, 08:24 PM
  3. Switch Statement
    By DarkDot in forum C++ Programming
    Replies: 11
    Last Post: 03-28-2007, 10:11 PM
  4. switch statement
    By crash88 in forum C Programming
    Replies: 4
    Last Post: 06-29-2006, 12:49 AM
  5. The Switch Statement
    By gozlan in forum C Programming
    Replies: 2
    Last Post: 04-19-2002, 12:44 AM

Tags for this Thread