473,396 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

help with converting numbers to Charater strings!

Hey yall! I am trying to get this program finished for class.... It
says that you are suppposed to write a program that will display a
check formatted out put (the output looks like a check). I got
everything to work and it will compile and run......

BUT.... I dont know how to turn the 'Amount' into a charater string
that puts the number into words.... It is supposed to show the name,
then the amount, and then on the next line write out the amount, just
like you would on a real check....

I have attached a copy of the program that I have so far.... PLEASE
help if you can.... this has got me stuck.....

note: the amount of the check can be up to $10,000.00

Thanks for your time!

Here is my code:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string name;
string lname;
string date;
float amount;

cout << "Please enter the date of the paycheck. (mm/dd/yy)" <<
endl;
cin >> date;

cout << "Please enter the first name of the Payee." << endl;
cin >> name;

cout << "Please enter the last name of the Payee." << endl;
cin >> lname;

cout << "Please enter the amount of the paycheck." << endl;
cin >> amount;
if (amount > 10000)
{ cout << " Amount cannot be over $10,000.00. Please
reenter the amount." << endl;
cin >> amount;
}


cout << "Here is your check printed out: " << endl;
cout << setw(55)<< date << endl;
cout << endl;
cout << left << name << " " << lname << setw(15);
cout << setw(35) << right << "$ " << showpoint << amount << endl;

// need to figure out how to change the float 'amount' into charaters.

return 0;
}

Nov 29 '05 #1
7 3579
OK. I think the boost::lexical_cast may help you solve the problem. You
can use boost::lexical_cast to cast float to std::string.

Example:

#include <iostream>
#include <string>
#include "boost/lexical_cast.hpp"

int main() {
// string to int
std::string s="42";
int i=boost::lexical_cast<int>(s);

// float to string
float f=3.14151;
s=boost::lexical_cast<std::string>(f);

// literal to double
double d=boost::lexical_cast<double>("2.52");

// Failed conversion
s="Not an int";
try {
i=boost::lexical_cast<int>(s);
}
catch(boost::bad_lexical_cast& e) {
// The lexical_cast above will fail,
// and we'll end up here
}
}

Nov 30 '05 #2
On Tue, 29 Nov 2005 15:21:53 -0800, MeganTSU wrote:
Hey yall! I am trying to get this program finished for class.... It
says that you are suppposed to write a program that will display a
check formatted out put (the output looks like a check). I got
everything to work and it will compile and run......

BUT.... I dont know how to turn the 'Amount' into a charater string
that puts the number into words.... It is supposed to show the name,
then the amount, and then on the next line write out the amount, just
like you would on a real check....

I have attached a copy of the program that I have so far.... PLEASE
help if you can.... this has got me stuck.....

note: the amount of the check can be up to $10,000.00


This is not a trivial task, depending on how true-to-life you want to be.

9345.12 = Nine thousand, three hundred, forty-five and 12/100
9000 = Nine thousand and xx/100
9500.12 = Nine thousand, five hundred and xx/100

Note that you can always write the cents (even if it's zero), so you will
always have "and CC/100" at the end, where "CC" is the number of cents
(perhaps using "XX" if it's 0).

You're going to need to break the number down into pieces:
- thousands
- hundreds
- tens
- ones
- cents

Then construct the string using these pieces adding the appropriate
"units".

Construct the string. You will want a table mapping numbers to strings for
values less than 20 (the rest can be assembled in pieces):

static const char* lownumberstrings[] =
{
"zero", "one", "two", "three", "four"
"five", "six", "seven", "eight", "nine",
"ten", "eleven", "twelve", "thirteen", "fourteen",
"fifteen", "sixteen", "seventeen", "eighteen", "nineteen"
};

You'll want a table mapping number of tens to the ten string:

static const char* tenstrings[] =
{
"", "ten", "twenty", "thirty", "forty", "fifty",
"sixty", "seventy", "eighty", "ninety"
};
You will have start with the thousands (and an empty string stream). If
you have some thousands (not zero), then add that part first (this is not
real C++ code):

if (thousands != 0)
string = lownumberstrings[thousands] + " thousand";

If there are hundreds, add the hundreds:

if (hundreds != 0)
if (!string.empty())
string += ", ";
string += lownumberstrings[hundreds] + " hundred"

If there are tens or ones, add them:

if (tens != 0 || ones != 0)
if (!string.empty())
string += ", ";
if (tens < 2)
string += lownumberstrings[tens*10 + ones];
else
string += tenstrings[tens] + " " + lownumberstrings[ones];

Then add the cents:
if (!string.empty())
string += " and ";

string += tostring(cents) + "/100";

You might want to special case the case where you have only cents (is it
"0 and CC/100?). You might also want to show 0 cents as "XX".

There's lots left to do here. Have fun!

- Jay
Nov 30 '05 #3
<Me******@gmail.com> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Hey yall! I am trying to get this program finished for class.... It
says that you are suppposed to write a program that will display a
check formatted out put (the output looks like a check). I got
everything to work and it will compile and run......

BUT.... I dont know how to turn the 'Amount' into a charater string
that puts the number into words.... It is supposed to show the name,
then the amount, and then on the next line write out the amount, just
like you would on a real check....

I have attached a copy of the program that I have so far.... PLEASE
help if you can.... this has got me stuck.....

note: the amount of the check can be up to $10,000.00

Thanks for your time!

Here is my code:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string name;
string lname;
string date;
float amount;

cout << "Please enter the date of the paycheck. (mm/dd/yy)" <<
endl;
cin >> date;

cout << "Please enter the first name of the Payee." << endl;
cin >> name;

cout << "Please enter the last name of the Payee." << endl;
cin >> lname;

cout << "Please enter the amount of the paycheck." << endl;
cin >> amount;
if (amount > 10000)
{ cout << " Amount cannot be over $10,000.00. Please
reenter the amount." << endl;
cin >> amount;
}


cout << "Here is your check printed out: " << endl;
cout << setw(55)<< date << endl;
cout << endl;
cout << left << name << " " << lname << setw(15);
cout << setw(35) << right << "$ " << showpoint << amount << endl;

// need to figure out how to change the float 'amount' into charaters.

return 0;
}


You have reached what is commonly known as "the hard part." I think that
the instructor assigned this task so that you might show your understanding
of mathematical operations like modulus (%), as well as if and looping
constructs. There is no simple, built-in way to do this in C++, so you'll
have to write your own implementation.

It seems to me that you will have to process each decimal digit
independently, and display text based on the value of the digit. So, for
example, if the amount is greater than or equal to 1000, you display an
english representation of the first digit (1-9) followed by the word
"thousand" ("one thousand," "two thousand," etc). Then, you eliminate that
digit (possibly using subtraction or modulus), and work on the hundreds
place ("one hundred," "two hundred," etc). Then, after eliminating that
digit, you process the tens, and then the ones, and then the cents (two
digits). If the maximum value includes 10000, then you'll have to add an
extra step at the beginning to account for that.

I'm just trying to point you in the right direction. HTH.

--
David Hilsee
Nov 30 '05 #4
I posted code for this type of conversion to comp.lang.c a few years
ago:
http://groups.google.com/group/comp....c367c8bcf64fc3

It's not very good C++, but it's a fair starting point.

Nov 30 '05 #5
Thank for the help.... I will try it and get back about the outcome!
Thanks again for your time!

Nov 30 '05 #6
Thank you soooooo much for all of your help! I have to get this thing
in by the end of the semester (IM GRADUATING! as you can tell my major
is not programming!) So I will try all of these things and let you know
my outcome!

THanks again!!!!!

Nov 30 '05 #7
On 2005-11-29, Me******@gmail.com <Me******@gmail.com> wrote:
Hey yall! I am trying to get this program finished for
class.... It says that you are suppposed to write a program
that will display a check formatted out put (the output looks
like a check). I got everything to work and it will compile and
run......

BUT.... I dont know how to turn the 'Amount' into a charater
string that puts the number into words....
First, figure out an algorithm to do it.

When designing your algorithm, you need to think as
simplistically as possible, as if you're explaining how to
convert 9,842.36 into nine thousand eight hundred forty-two and
36/100 to an extremely dull person. You need a list of discrete
steps containing only simple rules.

Next, write your algorithm in code. If you've designed it
completely and discretely enough, this should be the
easy part.

I have some comments on your code. Read on.
It is supposed to show the name, then the amount, and then on
the next line write out the amount, just like you would on a
real check....

I have attached a copy of the program that I have so far....
PLEASE help if you can.... this has got me stuck.....

note: the amount of the check can be up to $10,000.00

Thanks for your time!

Here is my code:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
using namespace std;
int main()
{
string name;
string lname;
string date;
float amount;
Use double, not float. Never use float. The float type is a lower
precision version of double. The higher precision your floating
point math, the less surprised you'll be by the results.
cout << "Please enter the date of the paycheck. (mm/dd/yy)" <<
endl;
cin >> date;

cout << "Please enter the first name of the Payee." << endl;
cin >> name;

cout << "Please enter the last name of the Payee." << endl;
cin >> lname;

cout << "Please enter the amount of the paycheck." << endl;
cin >> amount;
if (amount > 10000)
{ cout << " Amount cannot be over $10,000.00. Please
reenter the amount." << endl;
cin >> amount;
}
I would not bother validating your input at all. Validating the
input opens up a can of worms that your instructor most likely
didn't want to open just yet. Just make sure that your program
will work for valid inputs, and call it a day.

cout << "Please enter the last name of the Payee." << endl;
cin >> lname;

cout << "Please enter the amount of the paycheck." << endl;
cin >> amount;
if (amount > 10000)
{ cout << " Amount cannot be over $10,000.00. Please
reenter the amount." << endl;
cin >> amount;
}

Same here.
cout << "Here is your check printed out: " << endl;
cout << setw(55)<< date << endl;
cout << endl;
cout << left << name << " " << lname << setw(15);
cout << setw(35) << right << "$ " << showpoint << amount << endl;
You will want to investigate the setprecision manipulator for
amount, to prevent the check from showing something silly like

9842.100000

setprecision will tell cout how many digits to show after the
decimal place.
// need to figure out how to change the float 'amount' into
// characters.


Yep.

--
Neil Cerutti
Nov 30 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: fo | last post by:
Does anyone know if there is a free code available to convert numbers to text and/or text to numbers? Thanks
8
by: baustin75 | last post by:
Posted: Mon Oct 03, 2005 1:41 pm Post subject: cannot mail() in ie only when debugging in php designer 2005 -------------------------------------------------------------------------------- ...
17
by: Phil McKraken | last post by:
I am having a problem putting together a shopping cart with the below script. Everything displays fine, adds totals fine, and works perfect EXCEPT if you choose the 9.95 item #5 BY ITSELF the total...
8
by: Rick | last post by:
Hi, Does C have some handy functions to convert chars, ints and floats to bit arrays? I need to store that stuff binary so a few functions would be great. Converting chars and ints isn't...
3
by: Max Gattringer | last post by:
I have written a little programm, which converts normal Text into Unicode Bytes - nothing special, but i tried to creat 2nd Encoder which converts strings(numbers) in a textBox (strings which i...
30
by: zexpe | last post by:
I have an extremely cpu/data intensive piece of code that makes heavy use of the following function: void convertToDouble(const std::string& in, double& out) { out = atof(in.c_str()); } I...
38
by: Jordi | last post by:
I have made a little C program that generates the following output: 227.000000 / 5 = 45.400002 Here's the code: int main (int argc, char* argv) { float var = 227; printf("%f / 5 = %f\n", var,...
2
by: CoreyWhite | last post by:
Problem: You have numbers in string format, but you need to convert them to a numeric type, such as an int or float. Solution: You can do this with the standard library functions. The...
5
by: Kelth.Raptor | last post by:
Im having some difficulty with strings here, I hope someone is kind enough to help, I do appreciate it. Im working on a grade point average calculator for my intro to programming class and I...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.