Need help getting program print out the digit in words

This is a discussion on Need help getting program print out the digit in words within the C Programming forums, part of the General Programming Boards category; I need to have the out come of Enter a digit between 0 and 9: 4 You entered the number ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    13

    Need help getting program print out the digit in words

    I need to have the out come of
    Enter a digit between 0 and 9: 4
    You entered the number four

    So far I have come up with this

    insert [/
    Code:
    *
    File: wa2.c 
    Description: Written Assignment #2 for COS-116. Have the program print out the digit in words 
    Programmer: Thomas Garnick
    Date: April 23rd 2008
    */
    #include <stdio.h>
    int main()
    {
        char one, two, three, four, five; 
        int number;
    
    printf("Input Number: ");
    scanf("%d",&number);
    if (number == '1')
    number = one;
    if (number == '2')
    number = two;
    else if (number == '3')
    number = three;
    else if (number == '4')
    number = four;
    else if (number == '5')
    number = five;
    
    printf("you entered the number %c", number);
    
    return 0;
    }
    ]

    When I enter a number it gives me a special character as my output. We are still in the basics and have not gotten into character strings. Any help would be greatly apritiated

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If you haven't gotten into character strings you can't do the problem.

    I'm assuming you have gotten into string literals; you should note that "one" and one are not the same thing, and that "one" cannot be fit into a char variable.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    13
    for any one that is currious I finally solved it.
    Code:
    /*
    File: wa2.c 
    Description: Written Assignment #2 for COS-116. Dispays #'s 1-9 as a word
    Programmer: Thomas Garnick
    Date: April 23rd 2008
    */
    #include <stdio.h>
    int main()
    {
        int number;
    
    printf("Input a number 1-9: ");
    scanf("%d", &number);
    if (number == 0)
    printf("\nYou entered the number Zero\n");
    else if (number ==1)
    printf("\nYou entered the number One\n");
    else if (number == 2)
    printf("\nYou entered the number Two\n");
    else if (number == 3)
    printf("\nYou entered the number Three\n");
    else if (number == 4)
    printf("\nYou entered the number Four\n");
    else if (number == 5)
    printf("\nYou entered the number Five\n");
    else if (number == 6)
    printf("\nYou entered the number Six\n");
    else if (number == 7)
    printf("\nYou entered the number Seven\n");
    else if (number == 8)
    printf("\nYou entered the number Eight\n");
    else if (number == 9)
    printf("\nYou entered the number Nine\n");
    else 
    printf("\nSorry invalid number\n" 
                    "\nSince I'm a simple program I wont be able to tell you the answer\n"
                    "\nThanks for trying\n");
    
    return 0;
    }

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    I think switch statement will look better here
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Yet another possibility is to use an array of strings. If the number is in the range [0,9], display the corresponding array element, otherwise display the error message.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    laserlight has an excellent idea. The code might look something like this. Just to clarify.

    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
            int number=0 ;
            char numbers[10][6] = { "Zero", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine" };
    
            do {
                    printf("Input a number [0-9]: ");
                    scanf("&#37;d", &number);
    
                    if ( !(number >= 0 && number <=9) )
                            printf("Sorry input outside of valid range: Try again Please\n");
    
            } while ( !(number >= 0 && number <=9) );
    
            printf("You entered the number %s\n", numbers[number] );
    
    return 0;
    }

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    20,954
    Actually, I would rather create numbers as:
    Code:
    const char *numbers[] = {"Zero", "One", "Two", "Three", "Four",
                             "Five", "Six", "Seven", "Eight", "Nine"};
    since it is an array of pointers to the first characters of ten string literals.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    cute, ok. I agree totally... plus some error checking ;-)

    Code:
    #include <stdio.h>
    
    int main()
    {
            int number=10;
            const char *numbers[] = { "Zero", "One", "Two",   "Three", "Four", 
                                      "Five", "Six", "Seven", "Eight", "Nine"  };
    
            printf("Input a number [0-9]: ");
    
            while ( !scanf("&#37;d", &number) || !(number >= 0 && number <=9) )
            {
                    printf("Sorry input outside of valid range: Try again Please\n");
                    printf("Input a number [0-9]: ");
                    fflush(stdin);
            }
            printf("You entered the number %s\n", numbers[number] );
    
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    83

    Hai Use switch statement also...

    Code:
    #include<stdio.h>
    int main()
    {
      
        int choice;
        printf("enter the any number between 0 and 9\n");
        scanf("%d", &choice);
        switch(case)
       {
         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;
        defualt: printf("You are entered wrong number\n");
    }

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,424
    Quote Originally Posted by sillyman View Post
    cute, ok. I agree totally... plus some error checking ;-)
    You should review your error checking


    The return value of scanf() is the number of variables that were successfully assigned values, or EOF if there is an error.
    So your code perfoms the same way when scanf returns 1 or EOF. I doubt you want it that way
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    Why would a college student working on an exercise type in EOF on a simple programming example? Can you give me an example of how to enter EOF into the program?

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    Don't flush stdin!
    http://cpwiki.sourceforge.net/Common...kes_and_errors

    Quote Originally Posted by sillyman View Post
    Why would a college student working on an exercise type in EOF on a simple programming example? Can you give me an example of how to enter EOF into the program?
    You don't enter EOF. EOF is returned if there was an error. For example, if the user entered "Hi there!" instead of 1.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    Quote Originally Posted by Elysia View Post
    Don't flush stdin!
    Okay, thanks. I never tried it before. So a better option would be to use non-buffered input?

  14. #14
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,165
    If you need to clear the input buffer, there's an alternative way. Check the link. It contains links to all you need to know.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    Don't flush stdin!
    http://cpwiki.sourceforge.net/Common...kes_and_errors


    You don't enter EOF. EOF is returned if there was an error. For example, if the user entered "Hi there!" instead of 1.
    Actually, according to the specs, EOF is returned "If the first attempt to read a character returns EOF" (or end of string in the case of sscanf). This means that EOF will only happen if:
    1. input is coming from a text-file (and the program tries to read something when there's nothing left in the file).
    2. the user types CTRL-Z (or CTRL-D in a Unix/Linux environment).

    If nothing could be read in, then it will return 0. For example when a number is requested and "blah" is given. [That is for decimal numbers, hex numbers would accept "b" as input, and lah would be left in the buffer].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  2. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  3. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  4. Beginners Contest #2 For those who wanted more!!
    By ILoveVectors in forum Contests Board
    Replies: 16
    Last Post: 08-12-2005, 12:03 AM
  5. why does my program print off the wrong answers?
    By kl3pt0 in forum C Programming
    Replies: 18
    Last Post: 06-10-2004, 06:52 PM

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