I could use some help

I am trying to change a typed in number (324.54) to a written number (three hundred twenty four and fifty four cents). I jsut don't quite know what the best was is to do the conversion.
Last edited on
mayby you should first look if it has a . or a , in it then look at the lentgh to determine het biggest number witch is in you case 3 but it 3 positions to the left from the . so it has to be a hundred digit etc etc.
I'm not aware of anything that provides this capability directly, so I would suggest the following general solution: (I don't have the time to provide you with more than an outline, so this is just a functional description. If you have more specific questions as you're writing the code, post them and I'll try to help.) Start by assigning your number to a string, since strings have such nice "find" capabilities. Find the position of the decimal point. Using that and the overall "size" of the string (another useful function of strings) your program can determine the number of digits to the left and the right of the decimal. Now I'd suggest transferring the data into a couple arrays, one to hold the individual charachters from the string (See stringstream if you're not familiar with it.) and one generated to hold the position values relative to the decimal point. So for your example above the two arrays would look something like this:

individualchararray = [3,2,4,., 5, 4]
positionarray = [3,2,1,0,-1,-2]

(The zero representing the position of the decimal point, the positive numbers represent the ones, tens, hundreds, etc. and the negative numbers the tenths,hundredths, etc.)

Define name conversions for your program based on charachter and position. For example, one function (we'll call it charachterconvert) would accept an integer value and convert it to it's written name. There are a variety of ways you can do this, the easiest to understand is probably just a simple if statement.

if (some_int_from_individualchararray == 1)
{
cout<<"one"; //or store for later output if you desire
}
elseif (some_int_from_individualchararray == 2)
{
cout<<"two";
}

Another function (call it positionconvert) would take the charachters from the position array and convert it to names based on the position. For example

if (int_value_from_postionarray == 3)
{
cout<<"hundred";
}

(Three being the third value away from 0. Ones, tens, hundreds... and you wouldn't want to return anything but a blank space for the ones position. Also note that the tens postion will be a special case since its obviously its pronounced "twenty" instead of "two tens". I'll leave that for you to figure out Hint: Tie the execution of position and number together, and have the position function always check two postions ahead to see if it's a zero. You'll also need to have the position function aware of the value from the number function, or else you'll end up with some weird results. For example, 3030 should be "Three thousand thirty" but if the position function isn't aware that the hundred position is a zero, then it will output "Three thousand Zero hundred thirty" Also note that if you plan on this working properly for numbers beyond 9,999 special consideration will have to be taken for that in ties between the position and number functions. For instance you want 10000 to come out as "ten thousand" not "one thousand", so the functions will have to compare notes to see not just what the number is but WHERE it is. Just bear that in mind while you're writing the program.)

All that would be left is to loop through these two functions, so that they output in sequence:

for (i=0, i<=sizeof(individualchararray), i++)
{
charachterconvert(individualchararray[i]);
positionconvert(positionarray[i]);
}

Whew! That seems like a lot, but it's my best suggestion. The challenge of writing something like this has struck my interest, so I'll probably try it myself as I have the time. This isn't going to be a walk in the park to write, but I'm sure that if you stick with it, you'll persevere. Don't hesitate to post back with anything giving you difficulty.
Last edited on
Thank you, this will definitely help. I will post what I come up with if you want me to. All this program is basicly going to do it take the user's name the date they want and the amount of pay and put it into the form of a check.
Last edited on
I have a working function that will convert any entered number up to the trillions of dollars. There were were a whole lot of special cases to be considered! I thought I had it several times, and then I'd find one that I missed! I got them all though (I think.) A very interesting challenge. If you're still working on it and you have any questions, feel free to ask.
Topic archived. No new replies allowed.