Numbers into Letters
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? :)
jhonnyboy
Junior Poster in Training
97 posts since Jul 2008
Reputation Points: 11
Solved Threads: 0
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.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
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.
mahlerfive
Junior Poster in Training
77 posts since Aug 2008
Reputation Points: 33
Solved Threads: 18
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?
jhonnyboy
Junior Poster in Training
97 posts since Jul 2008
Reputation Points: 11
Solved Threads: 0
Here's a small tutorial on the switch statement
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
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 :(
jhonnyboy
Junior Poster in Training
97 posts since Jul 2008
Reputation Points: 11
Solved Threads: 0
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 :)
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403
because if i dont declare it as string i can't use the
switch(number.length())
jhonnyboy
Junior Poster in Training
97 posts since Jul 2008
Reputation Points: 11
Solved Threads: 0
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
Nick Evan
Not a Llama
10,112 posts since Oct 2006
Reputation Points: 4,142
Solved Threads: 403