Conversion issues!!

This is what i have thus far.

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


1. I do not know how to transfer the input "value" data from the main function into the setAmount before the main function.

2. Clueless about how to do the conversion.
for example: If 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
You have an unused and unneeded const int SIZE; in the TextVersionOfNumber class.

I'm not really sure why it has to be in a class like this. Why not just a function that does it?


1.
In any case, you are already using it correctly. Here is your class declaration, cleaned up:

TextVersionOfNumber.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef TEXTVERSIONOFNUMBER_H
#define TEXTVERSIONOFNUMBER_H

class TextVersionOfNumber
{
private:
  double amount;

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

#endif 

You already have a class variable named amount. And you have a method named setAmount(). What should the method do with the argument value?

TextVersionOfNumber.cpp
1
2
3
4
5
6
#include "TextVersionOfNumber.h"

void TextVersionOfNumber::setAmount(double value)
{
  // Do something with  this->amount  and  value  here.
}

Notice also that all the #include <stuff> and the using namespace std was removed from the header. Never use namespace anything in headers, and only #include stuff necessary in the header itself. For example, if your class had a std::string variable, then you would need to #include <string>. Otherwise you don't.

2.
The conversion itself requires you to think about how people think about numbers. And, if you are going to handle numbers over 999, you will also want to have another function to help handle numbers just in the range 0..999.

You have already (I hope) thought out the way numbers work in English. Anything less than twenty is special. Everything else follows a very simple pattern.

To split an integer into its digits, use the remainder operator:

1
2
3
4
  int number = 1234;
  int ones     =  number        % 10;  // ones == 4
  int tens     = (number /  10) % 10;  // tens == 3
  int hundreds = (number / 100) % 10;  // hundreds == 2 

If 'tens' is less than 2, then you need to use the special rule to get the correct text in one_19[]. Otherwise things work pretty straight-forwardly. Think about it.

This only works on integers. So you must convert the double value to an integer before you can do it. That is easy though:

1
2
double x = 12.34;
int y = x;  // x == 12 -- the .34 was lost 


Let's consider the lost part.

Remember that something like 12345.67 has two parts -- the whole part and the fractional part. You will have to separate them. There is a convenient function in <cmath> that will help there:
http://www.cplusplus.com/reference/cmath/modf/

1
2
  double int_part, frac_part;
  frac_part = std::modf( this->amount, &int_part );

To convert the fractional part into a whole number, use your basic math and multiply by 100:

1
2
  int dollars = int_part;
  int cents = frac_part * 100;

This should get you started.

You might, at some point, also want to make sure that the user did not enter a negative number.

Hope this helps.

(Gotta run. I think the cat knocked something over.)
Thanks a lot, especially for a fast reply too. I will look over this and take this into consideration. I am still very new at c++ and struggles understanding the concepts right away.

Again, thank you for taking your time and helping me out. So kind, so kind!
So another question..

1. how does the value that the user inputs (in the main function) gets transferred to "void TextVersionOfNumber::setAmount(double)"??

Sorry if this is a dumb question....
Topic archived. No new replies allowed.