numbers to words converter problems

Am trying to make a program that convert numbers in money format like 12.45 in words for example twelve dollars and 45 cents (cents do not need to be converted)
and when I test with the code i got, my output did not show any of the dollars value and the cents output was a big negative number.

so does anybody have any tips on what I should do to fix my problem???

class_amount.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#pragma once
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring>
#include <cctype>
#include <sstream>
using namespace std;

class class_amount
{
private:
	double amount;
public:
	
	string getTextVersionOfNumber(); 
	void setAmount(double);
	

};

amount.ccp

#include "class_amount.h"



string class_amount::getTextVersionOfNumber() 
{
	double * amount_arr = new double[amount];
	string one_19[] = { "", "one", "two", "three", "four",
		"five", "six", "seven", "eight", "nine", "ten",
		"eleven", "twelve",
		"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
		"eighteen", "nineteen" };

	string twenty_90[] = { "","","twenty","thirty","forty",
		"fifty", "sixty", "seventy", "eighty", "ninety" };

	for (int i = 0; i < sizeof(amount); i++)
	{
		string doller = " dollers ";
		string hundreds = " hundred ";
		string thousand = " thousand ";
		string cents = " cents ";
		string and = " and ";

		if (amount_arr[i] > 1)
		{

			int doller_val = amount_arr[i]; //0.01
			int cents_val = amount_arr[i] * 100; //1
			string doller_name = "Zero";
			string cents_name;

			ostringstream ostr;
			ostr << cents_val;
			cents_name = ostr.str();

			string total_string_0 = doller_name + doller + and +cents_name + cents;


			return total_string_0;
		}

		if (amount_arr[i] <= 1 || amount_arr[i] >= 19)
		{
			double doller_total_v1 = amount_arr[i];
			int doller_value_v1 = doller_total_v1; // no cents
			int cents_val_v1 = doller_total_v1 - doller_value_v1;
			string doller_name_v1 = one_19[doller_value_v1];
			string cents_name_v1;
			ostringstream ostr;
			ostr << cents_val_v1;
			cents_name_v1 = ostr.str();
			ostr << setprecision(1) << fixed << (cents_val_v1);

			string total_string_1 = doller_name_v1 + doller + and +cents_name_v1 + cents;

			return total_string_1;
		}
	}

}
void class_amount::setAmount(double _double )
{
	amount = _double;
}


class_tester.cpp

#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
using namespace std;

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

int main()
{
	string date = "03/05/2016", payee = "Maya Tolappa";
	class_amount checkAmount;
	double arr_num[] = { 0, .01, .25, 12.12, 12.45, 19, 19.02,
		19.45, 20, 20.65,
		34, 56.78, 100.21, 109.05, 119.78,
		450, 678.90, 1000, 1009.45, 1056,
		1234.15, 1567.98,9999, 9999.99 };

	cout << setprecision(2) << fixed;

	for (auto an_amount : arr_num)
	{

		checkAmount.setAmount(an_amount);

		cout << setw(60) << right;
		cout << "Date: " << date << endl;
		cout << "Pay to the order of:  " << payee << "\t\t\t";
		cout << "$" << an_amount << endl;
		cout << checkAmount.getTextVersionOfNumber() << endl;
		cout << "--------------------------------------------\n";
	}
	system("pause");
	return 0;
}

Last edited on
got the code tags in.
Last edited on
Topic archived. No new replies allowed.