Need to create an integer-to-word program

This is a discussion on Need to create an integer-to-word program within the C Programming forums, part of the General Programming Boards category; "Write a program that takes a two-digit integer (-99 to +99) as an input and outputs the number in words. ...

  1. #1
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31

    Need to create an integer-to-word program

    "Write a program that takes a two-digit integer (-99 to +99) as an input and outputs the number in words. If the input is outside this range, the program should print that it is outside the range."

    To be honest guys, I'm pretty much clueless on how to do this. I know I need to use the if and switch statements, but anything beyond that is over my head. Can anyone help me get started?

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,451
    start with writing numbers by hand
    see which word you are using
    and create a static array containing all these words
    then start to write conditions how to select correct word from your "dictionary"
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    So it looks like I'm gonna need

    one - nineteen (manually)
    twenty, thirty, forty, fifty, sixty, seventy, eighty, and ninety

    And somehow I need to have the program identify which inputs are in which places. I.e. if it sees 09, it needs to be able to see that the first digit is blank, and the 9, being the second digit, is output as "nine" and not "ninety"

  4. #4
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    I actually have one such implementation at hand as we speak.
    I created a table for the numbers 0-19. And a table for the numbers 20 to 90.
    I would then go for various tests, and build up a string with strcat() during the process.
    E.g.
    Code:
    char result[1024];
    memset(result, 0, 1024);
    
    if(x>=0 && x<=9)
    {
        strcat(result, num0_20[x]);
    } else
    if(x>=10 && x<=19)
    {
        strcat(result, num0_20[x]);
    }
    Figure out how to do the rest.
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  5. #5
    Registered User toadkiwi's Avatar
    Join Date
    Feb 2008
    Posts
    31
    I dont think we've learned all the things your program employs

  6. #6
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    It was not supposed to be a solution but a hint to a solution.
    Code:
    char *num0_20[] = {"zero","one",...};
    is the missing declaration that confuses you.
    The rest of the answer is trivial, and has to do with you reading the number and converting it to true number, and using it to select which string to concatenate.
    Think for a moment how to use the above code if x is 39?
    How about finding how many times 10 divides x and choosing an appropriate prefix, eg. "thirty".
    How would you do that?
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  7. #7
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    /*
    compile and run:
      gcc -lm this_program_code.c
      ./a.out
    
    I tested all of the numbers with this bash command:
      for i in `seq -99 100` ; do ./a.out $i ; done
    
    Disclaimer:
    I am exempt from using C comments, because I like you.
    */
    #include <stdio.h>
    #include <math.h>
    
    #define SHORTCUT_BIATCH \
    switch(second_digit){\
      case 1:\
        puts("one");break;\
      case 2:\
        puts("two");break;\
      case 3:\
        puts("three");break;\
      case 4:\
        puts("four");break;\
      case 5:\
        puts("five");break;\
      case 6:\
        puts("six");break;\
      case 7:\
        puts("seven");break;\
      case 8:\
        puts("eight");break;\
      case 9:\
        puts("nine");break;\
    }
    
    
    // Let's get this party started
    int main(int argc, char **argv){
      int number_from_user;
      int first_digit, second_digit;
      double temporary_number;
    
      // ask for a number -- from command line or command-line input
      if(argc == 2)
        sscanf(argv[1], "%d", &number_from_user);
      else{
        printf("Just give me a freakin' number--  ");
        fscanf(stdin, "%d", &number_from_user);
      }
    
      // is the number outside of the range?
      if(number_from_user > 99 || number_from_user < -99){
        puts("Error: OUT OF RANGE -- hacker alert!!! lolllly pop");
        return 0;
      }
    
      printf("%d: ", number_from_user);
    
      // is the number negative?
      if(number_from_user < 0){
        printf("negative ");
        number_from_user = -1 * number_from_user;
      }
    
      // get the first digit  Xx (the big X)
      // (for example, if 34 was the user's input, '3' would be the first digit)
      first_digit = (int) floor(number_from_user / 10.0);
      //printf("First digit: %d\n", first_digit);  //-- debugging bull........
    
      // get the second digit xX (the big X)
      second_digit = number_from_user - first_digit * 10;
      //printf("Second digit: %d\n", second_digit);  // -- debugging crap
      //puts("---------------");
    
      // output the number in words
      // based on the first and second digits of the numbers they entered.
      switch(first_digit){
        case 0:
          if(second_digit == 0)
            puts("zero");
          SHORTCUT_BIATCH
          break;
        case 1:
          switch(second_digit){
            case 0:
              puts("ten");break;
            case 1:
              puts("eleven");break;
            case 2:
              puts("twelve");break;
            case 3:
              puts("thirteen");break;
            case 4:
              puts("fourteen");break;
            case 5:
              puts("fiveteen");break;
            case 6:
              puts("sixteen");break;
            case 7:
              puts("seventeen");break;
            case 8:
              puts("eighteen");break;
            case 9:
              puts("nineteen");break;
          }
          break;
        case 2:
          printf("twenty ");
          if(second_digit == 0)puts("");
          SHORTCUT_BIATCH
          break;
        case 3:
          printf("thirty ");
          if(second_digit == 0)puts("");
          SHORTCUT_BIATCH
          break;
        case 4:
          printf("fourty ");
          if(second_digit == 0)puts("");
          SHORTCUT_BIATCH
          break;
        case 5:
          printf("fiftey ");
          if(second_digit == 0)puts("");
          SHORTCUT_BIATCH
          break; 
        case 6:
          printf("sixty ");
          if(second_digit == 0)puts("");
          SHORTCUT_BIATCH
          break;
        case 7:
          printf("seventy ");
          if(second_digit == 0)puts("");
          SHORTCUT_BIATCH
          break;
        case 8:
          printf("eighty ");
          if(second_digit == 0)puts("");
          SHORTCUT_BIATCH
          break;
        case 9:
          printf("ninety ");
          if(second_digit == 0)puts("");
          SHORTCUT_BIATCH
          break;
      }
    
      return 0;  // successorz :D  yay
    }

  8. #8
    uint64_t...think positive xuftugulus's Avatar
    Join Date
    Feb 2008
    Location
    Pacem
    Posts
    355
    This reminds me of some of the slowest sorting algorithms iMalc had on his page, only this one goes for the longest unnecessary solution candidate :P No offense just joking....
    Code:
    ...
        goto johny_walker_red_label;
    johny_walker_blue_label: exit(-149$);
    johny_walker_red_label : exit( -22$);
    A typical example of ...cheap programming practices.

  9. #9
    Stressed Student :(
    Join Date
    Feb 2008
    Location
    Berkeley, CA
    Posts
    73
    Code:
      switch(first_digit){
        case 0:
          if(second_digit == 0)
            puts("zero");
          SHORTCUT_BIATCH
          break;
        case 1:
    lmao, oh by the way after posting i still laughed out loud, good #define

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  3. Replies: 8
    Last Post: 09-04-2008, 09:56 AM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. Integer creation program
    By HLMetroid in forum C Programming
    Replies: 4
    Last Post: 09-22-2007, 12:57 AM

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