Thread: Changing numbers to words

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    102

    Changing numbers to words

    How do you do it? Like for example, you have 1234 and you want one-two-three-four to printed out. I was trying to use cases but I couldn't do much since am very new to the whole thing. Any suggestions are welcome. Yes I searched the forum but couldn't find a very good answer to my question. Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Funny, because I'm sure this has been discussed at least a dozen times since I became a member less than two years ago.

    What part are you struggling with? Splitting a number into digits, or making words from digits?

    --
    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.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    First of all, to be more specific, I have to change four digit numbers into words. The part I am struggling is making words out of the digits.

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    (1) Convert number to string
    (2) For each digit in string, print corresponding word

    You could use if/else statements here, or simply store the words in an array where digit - '0' yields the appropriate index.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If it's an integer, why not simply use the integer divide/modulo method - then use index into an array? There's no benefit in making it a string first, as I can see.

    --
    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.

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    That works, but then the output would have to be reversed.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Here is what I got so far. Please note that I can't use arrays because I don't know them. I am a complete beginner. So I just want use to loops and I thought if is the more appropriate one. My problem is in getting to print out the words of more than one digit. So if I have 4327, I want four-three-two-seven to be printed out. That is where I am stuck and I don't know arrays or pointers, just a bit of loops.

    Code:
    #include <iostream>
    using namespace std;
    int main (){
        int num,digit1,digit2,digit3,digit4;
        cout<<"Please enter a four digit number";
        cin>>num;
        digit1=(num/1000);
        digit2=(num/100)%10;
        digit3=(num/10)%10;
        digit4=num%10;
        if (digit1,digit2,digit3,digit4==0) cout<<"zero";
        else if (digit1,digit2,digit3,digit4==1) cout<<"one";
        else if (digit1,digit2,digit3,digit4==2) cout<<"two";
        else if (digit1,digit2,digit3,digit4==3) cout<<"three";
        else if (digit1,digit2,digit3,digit4==4) cout<<"four";
        else if (digit1,digit2,digit3,digit4==5) cout<<"five";
        else if (digit1,digit2,digit3,digit4==6) cout<<"six";
        else if (digit1,digit2,digit3,digit4==7) cout<<"seven";
        else if (digit1,digit2,digit3,digit4==8) cout<<"eight";
        else if (digit1,digit2,digit3,digit4==9) cout<<"nine";
        return 0;
    }
    Last edited by Dontgiveup; 03-28-2009 at 03:58 PM.

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    I just get last digit printed out. If I put in 4357 for example, I will only get seven. How can I make it to get me four-three-five-seven?

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I suppose the question that must be asked is why do you think "digit1,digit2,digit3,digit4" is going to do what you want?

  10. #10
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by tabstop View Post
    I suppose the question that must be asked is why do you think "digit1,digit2,digit3,digit4" is going to do what you want?
    I think it is because I have to print the word for each digit? I mean if someone inputs 1963, then I want one-nine-six-three printed out. That is just what I thought and I can be wrong.

  11. #11
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Dontgiveup View Post
    I think it is because I have to print the word for each digit? I mean if someone inputs 1963, then I want one-nine-six-three printed out. That is just what I thought and I can be wrong.
    Then you would need to do them in order, no? First digit first, then second digit second, etc.

  12. #12
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by Dontgiveup View Post
    Here is what I got so far. Please note that I can't use arrays because I don't know them. I am a complete beginner. So I just want use to loops and I thought if is the more appropriate one. My problem is in getting to print out the words of more than one digit. So if I have 4327, I want four-three-two-seven to be printed out. That is where I am stuck and I don't know arrays or pointers, just a bit of loops.

    Code:
        if (digit1,digit2,digit3,digit4==0) cout<<"zero";
    that's equivalent to if (digit4 == 0) cout << "zero";

    the proper statement would be

    Code:
    if (digit1 == 0 && digit2 == 0 && digit3 == 0 && digit4 == 0) cout << "zero"
    I don't think you want to use that method of reasoning though, because you program will only be able to handle 0000, 1111, 2222, etc...

    HOW TO MAKE AN ARRAY:
    Code:
     
    int digit[4]; // this will make an array with 4 cells, cell 0, 1, 2, & 3
    digit[0] = 1; // this will store 1 inside the first cell in digit
    digit[1] = 2; // this will store 2 inside the 2nd cell in digit
    digit[4] = 5; // BE CAREFUL this cell doesn't exist! make sure that your program doesn't try to access cells outside the bounds of the array
    what you really want your program to do is this:
    Code:
    int main()
    {
        int num, digit[4], i;
        cout<<"Please enter a four digit number";
        cin>>num;
        digit[0]=(num/1000);
        digit[1]=(num/100)%10;
        digit[2]=(num/10)%10;
        digit[3]=num%10;
        
        for (i = 0; i < 4; i++) { 
          if (digit[i] == 9) cout << "nine";
          else if(digit[i] == 8) cout << "eight";
          else if(digit[i] == 7) cout << "seven";
          // etc
          
          if (i) cout << "-"; // prints - as long as i != 0
        }
    }
    Last edited by ಠ_ಠ; 03-28-2009 at 05:57 PM.
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    102
    Quote Originally Posted by tabstop View Post
    Then you would need to do them in order, no? First digit first, then second digit second, etc.
    Thank you, you really helped me understand it at last. Here is my full code and it works. I guess it is going through a very long way but I guess it is what a beginner deserves? Is there any shorter way using loops? I haven't s till learn pointers and arrays though I heard they can be used as short-cuts. But thank you so much.
    Code:
    #include <iostream>
    using namespace std;
    int main (){
        int num,digit1,digit2,digit3,digit4;//
        cout<<"Please enter a four digit number";
        cin>>num;
        digit1=(num/1000);
        digit2=(num/100)%10;
        digit3=(num/10)%10;
        digit4=num%10;
        if (digit1==0) cout<<"zero";
        else if (digit1==1) cout<<"one";
        else if (digit1==2) cout<<"two";
        else if (digit1==3) cout<<"three";
        else if (digit1==4) cout<<"four";
        else if (digit1==5) cout<<"five";
        else if (digit1==6) cout<<"six";
        else if (digit1==7) cout<<"seven";
        else if (digit1==8) cout<<"eight";
        else if (digit1==9) cout<<"nine";
        if (digit2 ==0) cout<<"zero";
        else if (digit2==1) cout<<"one";
        else if (digit2==2) cout<<"two";
        else if (digit2==3) cout<<"three";
        else if (digit2==4) cout<<"four";
        else if (digit2==5) cout<<"five";
        else if (digit2==6) cout<<"six";
        else if (digit2==7) cout<<"seven";
        else if (digit2==8) cout<<"eight";
        else if (digit2==9) cout<<"nine";
        if (digit3==0) cout<<"zero";
        else if (digit3==1) cout<<"one";
        else if (digit3==2) cout<<"two";
        else if (digit3==3) cout<<"three";
        else if (digit3==4) cout<<"four";
        else if (digit4==5) cout<<"five";
        else if (digit3==6) cout<<"six";
        else if (digit3==7) cout<<"seven";
        else if (digit3==8) cout<<"eight";
        else if (digit3==9) cout<<"nine";
        if (digit4==0) cout<<"four";
        else if (digit4==1) cout<<"one";
        else if (digit4==2) cout<<"two";
        else if (digit4==3) cout<<"three";
        else if (digit4==4) cout<<"four";
        else if (digit4==5) cout<<"five";
        else if (digit4==6) cout<<"six";
        else if (digit4==7) cout<<"seven";
        else if (digit4==8) cout<<"eight";
        else if (digit4==9) cout<<"nine";
        cin>>num;
        return 0;
    }

  14. #14
    Registered User BuzzBuzz's Avatar
    Join Date
    Feb 2009
    Posts
    89
    Not really a solution to your problem directly as I am also a newbie to c++ programming, but a good resource that I'm currently using is "C++ Primer Plus" by Stephen Prata. Unlike many of the other C++ books out there it introduces arrays and pointers quite early on (not that they made much sense at first).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  2. Converting Numbers to Words
    By denizengt in forum C Programming
    Replies: 20
    Last Post: 11-05-2003, 09:19 PM
  3. converting numbers into words
    By Kozam in forum C Programming
    Replies: 2
    Last Post: 09-30-2003, 07:49 AM
  4. C++ program help(converting numbers to english words)
    By bama1 in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2002, 01:17 AM
  5. A (complex) question on numbers
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 02-03-2002, 06:38 PM