help with converting numbers to Charater strings!

M

MeganTSU

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;
}
 
N

nacci

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
}
}
 
J

Jay Nabonne

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
 
D

David Hilsee

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.
 
M

MeganTSU

Thank for the help.... I will try it and get back about the outcome!
Thanks again for your time!
 
M

MeganTSU

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!!!!!
 
N

Neil Cerutti

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top