![]() |
|
#1
|
|||
|
|||
Converting a number amount to textI am doing a checkwriting program using C strings for my intermediate programming class. We have covered loops, streams, if statements, arrays, pointers, and strings.
The person is supposed to enter a number like $1000. 50, and it is supposed to display it like "one thousand dollars and 50 cents" as a string. The highest value that can be entered is $10,000. My book doesn't show any good examples that would help on this assignment and I am stumped. Help would be appreciated. Thanks Note: I am using Microsoft Visual Studio. |
|
#2
|
|||
|
|||
Re: Converting a number amount to textHi Godzilla,
This is the only way I can think of. It's nothing fancy or anything. It should work though. CPP / C++ / C Code:
I hope I've been of some help. If I can think of a better way, I'll post it here. Last edited by cable_guy_67 : 30-Mar-2006 at 06:38.
Reason: Please enclose c code in [c] ... [/c] tags
|
|
#3
|
|||
|
|||
Re: Converting a number amount to textQuote:
Here is a slight improvement on your code, but it still doesn't meet the requirements as written. CPP / C++ / C Code:
The requirements were: Quote:
Notice the "50" cents and not "fifty" cents. Also, your code doesn't handle "no" cents properly. You've got the right idea, but you're going about it rather bluntly. Try creating an array of strings for your textual numbers and an array of integers for your "value amounts." EG: CPP / C++ / C Code:
Output: Code:
Also, you'll want to convert the incoming dollar.cents value into a pair of integers, one for the dollar amount and another for the cents amount since you need to treat dollars in text and cents as the numerical value in order to properly meet the requirements. Of course, there is a much more elegant solution using units, but that is for someone with more time to spend on it.... ...the same basic thing is possible using char* (C strings) and maintaining a buffer and strcat'ing the char*s into the buffer, but I was too lazy and used "C++ strings." :davis: |
|
#4
|
|||
|
|||
Re: Converting a number amount to textI had to do the same project. This is what I came up with. It is a lengthy program, but it works.
CPP / C++ / C Code:
|
|
#5
|
|||
|
|||
Re: Converting a number amount to textQuote:
Code:
Your output should probably be: Twenty Nine and 01/100 Dollars. Look at what happens in this case: Code:
...there are no "units" being disclosed. We've got 1/100 of something unspecified. :davis: |
|
#6
|
|||
|
|||
Re: Converting a number amount to textnice solution david
|
Recent GIDBlog
First week of IA training by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| My program can run,but warning were display on Vc++ | fwongmc | C Programming Language | 5 | 08-Dec-2004 10:15 |
| Revised--C/Pointers problem | fwongmc | C Programming Language | 9 | 06-Dec-2004 11:25 |
| C Pointer problem | fwongmc | C Programming Language | 8 | 04-Dec-2004 12:34 |
| Anyone can write a program code for this??? | chriskan76 | C Programming Language | 1 | 19-Oct-2004 20:25 |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The