
July 25th, 2012, 04:24 PM
#1
Converting numbers into text
This is my first post so if I did something wrong let me know. Ok, so I have been reading the book practical C++ programming. One of the practice problems asks you take a number that is entered by a user and turn that into printed text. Ex. 85 would be eight five. The problem I am having is I'm not quite sure how to go about this. I don't know if I should use an array, string, or something else. If you could point me in the right direction I would appreciate it.
P.S. I don't want anyone to write the code I just need to know how to go about this.
Thanks,
Rafen

July 25th, 2012, 06:02 PM
#2
Re: Converting numbers into text
Originally Posted by Rafen
This is my first post so if I did something wrong let me know. Ok, so I have been reading the book practical C++ programming. One of the practice problems asks you take a number that is entered by a user and turn that into printed text. Ex. 85 would be eight five.
Shouldn't that be "eighty five"?
The problem I am having is I'm not quite sure how to go about this.
Believe it or not, that is the assignment  whether you have the skills to come up with a method to solve the problem.
This assignment is one of the classical ones handed out to programming students. The "change a number into words" assignment has it as a goal to see if you can devise a scheme to solve the problem using whatever means you have.
I don't know if I should use an array, string, or something else.
That's the point  you can use an array, string, or something else. There are literally scores of ways to solve this problem, but again, the goal of the assignment is to see if you can figure it out (and of course, implement your idea).
Personally, I think that an assignment like this tests the programmer's design skills and if they have the knowledge to take the tools that they have and create a program without a teacher's (or our) help.
If you could point me in the right direction I would appreciate it.
You know exactly what the program requires  it is very simple. Your goal is to break this down into discrete steps. The first steps is to match each sub number into a word:
Code:
1 > one
2 > two
3 > three
4 > four
...
9 > nine
Now you do the "twodigit" names
Code:
10 > ten
11 > eleven
12 > twelve
13 > thirteen
...
19 > nineteen
Then you do the "tens"
Code:
20 > twenty
30 > thirty
40 > forty
50 > fifty
...
90 > ninety
Then you do the multiples of 10 starting with 100
Now you have the "comma" names
Code:
one comma > thousand
two commas > million
three commas > billion
All worded numbers consist of a combination of the above. There are no further names to consider. So now you have a number:
How would you set up the above to print "four". Easy. Now you have a two digit number:
How would you print "twenty four"? You have to somehow parse the number, getting the "2" and doing some sort of a look up to find the word "twenty". Then you need to parse the "4" to get the "four".
Then you try 3 digit numbers, 4 digit numbers, etc.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 26th, 2012 at 02:24 AM.

July 25th, 2012, 06:58 PM
#3
Re: Converting numbers into text
Alright thanks a lot for the help. I think I figured a way out so I will go try it and see if it works.

July 25th, 2012, 07:09 PM
#4
Re: Converting numbers into text
Originally Posted by Rafen
Alright thanks a lot for the help. I think I figured a way out so I will go try it and see if it works.
Well, to give you an idea, you lay out the digits in groups of 3.
For example:
Code:
187932 > 187 932
4 > 004
123 > 123
19371387 > 019 371 387
The pattern is that a group of three numbers will never be greater than 999. So if you can write the words for all numbers up to 999, then you have 80% of the assignment done. The rest of the assignment is to figure out which "comma" word to use (or if you need to use one) for each group of 3.
Regards,
Paul McKenzie

July 25th, 2012, 07:34 PM
#5
Re: Converting numbers into text
Thanks a lot. I have one other question...Is this suppose to take a while or is it like a test to see how well of a programmer you are?

July 25th, 2012, 10:49 PM
#6
Re: Converting numbers into text
Originally Posted by Rafen
Thanks a lot. I have one other question...Is this suppose to take a while or is it like a test to see how well of a programmer you are?
Both.
Just to let you know, a professional programmer would be able to write this up (or get very close to finishing) in less than an hour, and more than likely, less than a halfhour. However, for a good student  maybe a day or two at most.
Regards,
Paul McKenzie

July 26th, 2012, 05:26 AM
#7
Re: Converting numbers into text
Yeah I thought it might take awhile for me to complete this. Thanks a lot for all your help.

July 26th, 2012, 04:44 PM
#8
Re: Converting numbers into text
I dont think having C++ for a single semester has given me enough background to write the code for this program.
I am stuck and have no idea what I'm doing wrong. I haven't typed all the code just enough so I could get everything working then I would finish the rest of the code. I have been trying to change "125" into words. The problem is when I enter this number the only thing that prints to the screen is "Hundred". Any help would be appreciated. Thanks.
Code:
#include <iostream>
using namespace std;
int main()
{
int num,ones,tens,hundreds;
cout << "Please enter a number to be converted to words: ";
cin >> num;
tens = num/10;
ones = num%10;
hundreds = num/100;
// this is for the ones place.
switch(num)
{
case 0 :
cout << "zero";
break;
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
case 3:
cout << "three";
break;
case 4:
cout << "four";
break;
case 5:
cout << "five";
break;
case 6:
cout << "six";
break;
case 7:
cout << "seven";
break;
case 8:
cout << "eight";
break;
case 9:
cout << "nine";
break;
case 10:
cout << "ten";
break;
}
// this is for the tens place.
switch(tens)
{
case 2:
cout << "twenty ";
break;
}
// this is for the hundreds place
switch(hundreds)
{
case 1:
cout << "Hundred";
break;
}
}
Please let me know anything that I am doing wrong.
Last edited by Rafen; July 26th, 2012 at 04:47 PM.

July 26th, 2012, 07:06 PM
#9
Re: Converting numbers into text
Originally Posted by Rafen
I dont think having C++ for a single semester has given me enough background to write the code for this program.
Before you write one line of code, you should have written out the steps required to convert the number. You didn't do that and you're trying to figure out what to do while writing the program. That is the wrong way to go about doing this.
Write out the steps in English first:
Code:
1) Determine the number of digits in the input.
2) If the number of digits is equal to 1, then the number is a straight lookup.
3) If the number of digits is equal to 2 then
a) extract the tens digit.
a2) if the tens digit is greater than 1, then map the digit to twenty, thirty, forty, etc. Add this to your sentence and go to step b)
a3) if the tens digit is a 1, then you map it to ten, eleven twelve, thirteen, etc. This is your sentence  done.
b) extract the ones digit
b2) add the digit word to your sentence. Done.
etc...
It doesn't take C++ to write these out  that's why the assignment is about design, and not necessarily about C++. With these steps written out, it should be able to be coded in any language.
Regards,
Paul McKenzie

July 26th, 2012, 07:15 PM
#10
Re: Converting numbers into text
Thanks for the help I will try to write out the steps from now on before I even write a line of code. We never programmed like that in class we just jumped right into the program and started coding. I will have to take a different approach to programming after see how easy it was to figure out what to code once you wrote the program out in english.
Thanks Again,
Richard

July 27th, 2012, 08:42 PM
#11
Re: Converting numbers into text
I have been working on this for a couple days here and there. I can't figure out what to do on this program. I didn't get any further than I was before I just took a different way of writing the program. Do you think I should just put this program to the side and read over the book until I understand how to problem solve better? I know what I need to do in the steps that you listed out above I just can't seem to figure out how exactly to do it. I am just guessing and can't figure out how exactly to do it.
Code:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int integer;
int places;
int ones,tens;
string num;
cout << "Please enter a number to be converted to words: ";
getline(cin, num);
places = num.length();
if(!(stringstream(num)>> integer)) integer = 0;
ones = integer%10;
tens = integer/10;
switch(ones)
{
case 1:
cout << "one";
break;
case 2:
cout << "two";
break;
}
switch(tens)
{
case 2:
cout << "twenty ";
break;
}
}
I was using "22" as a tester and I get "twotwenty " to print to the screen.
Last edited by Rafen; July 27th, 2012 at 08:49 PM.

July 27th, 2012, 09:02 PM
#12
Re: Converting numbers into text
1) Concentrate on converting numbers between 1 and 999. Write a function just to do that.
2) To do 1), you should write a function that extracts the hundreds, tens, and units digit first.
For example:
Code:
#include <iostream>
using namespace std;
int main()
{
int nNumber;
cout << "Enter a number: ";
cin >> nNumber;
int nHundreds, nTens, nUnits;
nHundreds = ???;
nTens = ???;
nUnits = ???;
cout << "Hundreds: " << nHundreds << endl << "Tens: " << nTens << endl << "Units: " << nUnits;
}
Once you know how to extract the digits, you then can use the digits to help you figure out how to print the number. For example, you need to have "one", "two", "three", up until "nine" in one array, "ten", "eleven", up until "nineteen" in another array, "twenty", "thirty", etc. in another array. Then you use the extracted digits, logic, and the tables to determine how to print the full number (the logic is required to know if you need to have the string "hundred" in the final output).
I won't help with the extraction of the digits  simple computer math using divisions and modulus is what you need to know to use. But assume you did extract the digits correctly. Given the extracted digits, how do you use the tables that were set up to print the correct string? You can write the steps out, assuming you know to get the individual digits from the number.
Basically this is how you approach a problem like this. You do each piece, making sure it works before proceeding.
Once you have that, create a function that returns the 1999 string. Then you concentrate on how to write the comma strings such as million, thousand, billion (if you get that high a number), etc. but using the function that you wrote above to generate the 1999 string for each group of three digits.
On a lark, I got then entire assignment working in a few minutes (if you subtract the time it took to type in the number strings).
Regards,
Paul McKenzie
Last edited by Paul McKenzie; July 27th, 2012 at 11:45 PM.

July 27th, 2012, 11:55 PM
#13
Re: Converting numbers into text
Originally Posted by Rafen
I just can't seem to figure out how exactly to do it. I am just guessing and can't figure out how exactly to do it.
Regardless of the number, you should extract all the digits (this is assuming the number is between 1 and 999, and it's this range you should put the most time in).
As to your code:
1) Even if the hundreds, tens, or units place is 0, you extract that value.
2) Your code does not extract the hundreds digit.
3) You don't need to make the input a string. Make it an integer and use cin, not getline().
4) Please create your arrays of names I stated above.
Code:
const char *unitName[] = {"zero", "one", "two", /* ... */, "nine"};
const char* tensNames[] = {"ten", "twenty", "thirty", /* ... */, "ninety"};
const char* teensNames[] = {"eleven", "twelve", /* ... */, "nineteen"};
Once you have 1) and 4) staring at you, it shouldn't take a long time to write the logic to print the correct number. For example, If the hundreds digit is > 0, then you know you need to do a lookup in the units array for the hundred's digit name, and then print the word "hundred" after it. That's as far as I'm going to help you
Tags for this Thread
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
OnDemand Webinars (sponsored)
