Check Writer C++ Issue!

// Chapter 12-- Assignment 14: Check Writer
// This program can convert a dollar and cents amount given in
// numerical form to a word description of the amount.
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
using namespace std;

class text
{
const int SIZE;
private:
double amount;
string amount_string;

public:
string convertNumberToText();
void setAmount(double);
};
// Assume a maximum amount of $10,000

int main()
{

double value;
string date, payee;
TextVersionOfNumber text;

cout << "Enter the date in mm/dd/yyyy format: ";
cin >> date;
cin.ignore(80, '\n');

cout << "\nEnter the payee name (First name, blank, last name): ";
getline(cin, payee);

cout << "\nEnter the amount: ";
cin >> value;
while (value < 0 || value >= 10000)
{
cout << "Invalid amount: amount cannot be negative or over 10000.\n\n";
cout << "\nEnter the amount: ";
cin >> value;
}

text.setAmount(value);

// Display date

cout << "---------------------------------------------------\n";
cout << setw(60) << right;
cout << "Date: " << date << endl;

cout << "Pay to the order of: " << payee << "\t\t\t";
cout << "$" << value << endl<<endl;;

cout << text.convertNumberToText() << endl << endl<<endl;;
cout << "---------------------------------------------------\n\n\n";
system("pause");
return 0;
}

This is as far as i got and do not know where to start. I need to put this into 3 separate files: an .h file and 2 cpp files as the main is a cpp tester file. If anyone can help me asap i would really be appreciated!!
Hi SimonXM,

I will suppose you work on Linux and here what you must do:

Write a file text.h in which you will put your text class interface :

1
2
3
4
5
6
7
8
9
10
11
class text
{
const int SIZE;
private:
double amount;
string amount_string;

public:
string convertNumberToText();
void setAmount(double);
};


Write a file text.cpp in which you will define the methods of your class. For example you will have:
1
2
3
string text::convertNumberToTex() {
     // write your code here for the conversion
}


And then create a third file, main.cpp in which you will write your main function.

Don't forget the deal with the header files to include.

To compile to project, you must then do this:

g++ main.cpp text.cpp -o myExecutable

And to execute:

./myExecutable
Thanks Aleonard for the tip. I am working with visual basic 2012 to code my programs. I figured out how to separate them into the 3 programs. I just don't know what to put in under the public classes to make my conversions.
Ok, I see. The easiest way to convert numbers to string is using a stringstream. You can find some doc about that here:

http://www.cplusplus.com/reference/sstream/stringstream/stringstream/

I let you discover that if you don't know stringstream :)
Is there a difference between a stringstream compared to a ostreamstring? My prof. recommends us using the ostreamstring but didn't teach us the concept.
I'm not sure about the details but basically, an ostringstream is a stringstream specialized for output stuff, while an istringstream is a stringstream for input stuff.

It is a good exercise to try to decrypt the doc:
http://www.cplusplus.com/reference/sstream/

I'm sure it's not important at this level. It would be a good question for your teacher to ask him the real difference between istringstream, ostringstream and stringstream. One more time, the answer is in the doc :)
Okay awesome Aleonard. Thanks for helping out!
I don't know how to transfer the user input "value" data from the Main into the setAmount and am very clueless on the output.

so if the user inputs 123.45.

Im looking for output:
One hundred and twenty three and 45 cents.

any suggestions into putting it into a "while" function?
Last edited on
So this is what i have..

//TextVersionOfNumber.h file:
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;

class TextVersionOfNumber
{
const int SIZE;
private:
double amount;
string amount_string;

public:
string convertNumberToText();
void setAmount(double);
};

//TestVersionOfNumber.cpp file:
#include "TextVersionOfNumber.h"

string TextVersionOfNumber::convertNumberToText()
{
string rv = "";
string one_19[] = { "", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten",
"eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
"eightteen", "nineteen"};
string twenty_90[] = {"", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety"};
string hundred[] = {"hundred"};
string thousand[] = {"thousand"};
return rv;
}

void TextVersionOfNumber::setAmount(double)
{


}

//And then my test file which consist of the main function.
//Tester.cpp:
// Chapter 12-- Assignment 14: Check Writer
// This program can convert a dollar and cents amount given in
// numerical form to a word description of the amount.
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
#include <sstream>
using namespace std;

#include "TextVersionOfNumber.h"
// Assume a maximum amount of $10,000

int main()
{

double value;
string date, payee;
TextVersionOfNumber text;

cout << "Enter the date in mm/dd/yyyy format: ";
cin >> date;
cin.ignore(80, '\n');

cout << "\nEnter the payee name (First name, blank, last name): ";
getline(cin, payee);

cout << "\nEnter the amount: ";
cin >> value;
while (value < 0 || value >= 10000)
{
cout << "Invalid amount: amount cannot be negative or over 10000.\n\n";
cout << "\nEnter the amount: ";
cin >> value;
}

text.setAmount(value);

// Display date

cout << "---------------------------------------------------\n";
cout << setw(60) << right;
cout << "Date: " << date << endl;

cout << "Pay to the order of: " << payee << "\t\t\t";
cout << "$" << value << endl<<endl;;

cout << text.convertNumberToText() << endl << endl<<endl;;
cout << "---------------------------------------------------\n\n\n";
system("pause");
return 0;
}
Last edited on
Hi, you wrote:
Im looking for output:
One hundred and twenty three and 45 cents.

And I must say, this is not what I understood at first ! This is a bit less straightforward...

Forget about stringstream then ... sorry for that. what you should do is the following:

extract the decimal and integer parts of your number. For decimal, you multiply by 100 and then %100 should give you the cents.

(123.45 * 100) % 100 = 45 (if I'm not wrong)

To get the integer part, simply do a type cast to int:

(int)123.45 = 123;

So for the cents part, you're done. For the integer part, you must create a table of strings for "one", "two", "three", ... "nine", "ten", "elevene", nineteen". Another one for "tweenty", "thirty", "fourty", ... "ninety".

Then you extract the units usin modulo 10 operation:

123 % 10 = 3, and you call unitArray[3] = "three"

Then you extract the next digit:

(123/10) % 10 = 2, and you call decadeArray[2] = "tweenty"

Last number:

(123/100)%10 = 1 and you call unitArray[1] = "one" + "hundred"

Do you see what I mean ? You'll have to make a lot of checks if statements...
i'm doing the same program, the part i don't get is how to put the decision together.
should i use a for look to search through the array and pick what subscript is should choose to match the number

Topic archived. No new replies allowed.