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