hello guys i got a simple project but not that simple at the same time. lol The user needs to input a number such as : 512 and the code needs to output the number but in letters(five hundread and twelve). Any tips? :)

First figure out how you break the number into the respective single digits.

Then, how do you display the various combinations - be sure to account for the -teens and for zero values.

Let's look at a simple example of how you might do this. Imagine you were give the number 72061.

The first thing you should do is determine how many digits there are - in this case 5. Now, you can divide by 10^4 (10 to the power 4, 4 because it is one less than the number of digits) and you will get 7 as the answer. Now you know that 7 is the first digit, and because you used 4 as the power, you know that this is the ten thousands digit. So you can store "Seventy". Now subtract off 10^4 * 7 and you will have the leftover amount of 2061.

Now you simply repeat the process after decrementing the power. Divide by 10^3 and you get 2. Since we used the power 3, we know this is the thousands digit, so we can store "Two Thousand". Then, subtract off 10^3 * 2 leaving 61.

Repeat again, divide by 10^2 to find the hundreds digit. Here the answer will be 0, so we don't need to store anything. Note that in some cases you will still need to (if for example the thousands digit was 0 but the ten thousands digit was 7, you need to still store "Thousand").

Repeat again, divide by 10^1 and get the digit 6 which is the tens digit. Store "Sixty" and subtract 10^1 * 6 to get 1.

Repeat again, dividing by 10^0 (if u really want to, although this is just dividing by 1) and you will get the units digit 1. Store "One".

So you stored: "Seventy Two Thousand Sixty One".

I'm sure there are other ways to approach the problem, but this one will probably work well.

jhonnyboy, it will be better if you can try to write a program first then post your coding.

mahlerfive is right but another alternative way is to store the number the user input in string then you can get each digit in the string a lot more easier. After you get the single digit, use "switch" for each digit to display the number in word. :)

yep thats what i was thinking raul. But i never heard of this "switch" can you explain it a little further? do i need to include any libraries?

This is what i have so far guys...

#include <iostream>
#include <string>

using namespace std;

void ones(string);

int main(){


string number;


cout<<"ENter:"<<endl;
cin>>number;

switch(number.length())
{
case 1: ones(number);


}

system("pause");
return 0;


}

void ones(string numero)
{
int x;
x=atoi (numero);

switch(x)
{
case 1: cout<<"one"<<endl;
case 2: cout<<"two"<<endl;
case 3: cout<<"three"<<endl;
case 4: cout<<"four"<<endl;
case 5: cout<<"five"<<endl;
case 6: cout<<"six"<<endl;
case 7: cout<<"seven"<<endl;
case 8: cout<<"eight"<<endl;
case 9: cout<<"nine"<<endl;
case 0: cout<<"zero"<<endl;

}

}

It's giving me a hard time when i try to convert the string into an integer for the converting. Don't know what to do :(

string number;

It's giving me a hard time when i try to convert the string into an integer for the converting. Don't know what to do :(

Why did you declare 'number' as a string anyway? If you just declare it as an int, you won't have to convert it, and it won't give you any problems :)

because if i dont declare it as string i can't use the

switch(number.length())

How about making your own function to check how many digits a number has:

(you've shown some effort, so you'll get a freebie :) )

int HowManyDigits(int input)
{
    long div=1, digits=1;
    while (input/div > 1)
    {
        digits++;
        div*=10;
    }
    return digits;
}

int main()
{
    std::cout << HowManyDigits(12345678);
    return 0;
}

output : 8

This article has been dead for over six months. Start a new discussion instead.