Issue with converting doubles to strings

For my code we are looking at making a program that can convert a dollar and cents amount given in numerical form to a word description of the amount. I can get it to cycle through all the numerical values I need but there is something wrong in my algorithm that is causing the strings to not display properly.

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
  //Header file
#include <iostream>
#include <iomanip>
#include <string>
#include <string.h>
using namespace std;

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

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

string TextVersionOfNumber::getTextVersionOfNumber()
{
	string upto20[20] = { "", "one", "two", "three", "four",
		"five", "six", "seven", "eight", "nine", "ten",
		"eleven", "twelve",
		"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
		"eighteen", "nineteen" };

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

	double _amount = (double)amount;
	string ret_val = "";

	if (amount < 0)

		cout << "Given amount is negative.";
	amount = abs(amount);
	_amount = amount / 1000;
	if (_amount>0)
		ret_val += upto20[(int)_amount], " thousand ";
	amount /= 1000;
	_amount = amount / 100;
	if (_amount > 0)
		ret_val += upto20[(int)_amount], " hundred ";
	amount /= 100;
	if (amount >= 20)
	{
		_amount = amount / 10;
		if (_amount > 0)
			ret_val += tens[(int)_amount], " ";

	}
	else if (amount >= 10)
	{
		ret_val += upto20[(int)amount], " ";
	}
	amount /= 10;
	if (amount > 0)
		ret_val += upto20[(int)amount];


	system("pause"); 
	return ret_val;
};

void TextVersionOfNumber::setAmount(double _amount)
{
	amount = _amount;

};

//tester file
//  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;

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

int main()
{
	string date = "03/05/2016", payee = "Michael Wille";
	TextVersionOfNumber checkAmount;
	double testAmounts[] = { 0, .01, .25, 12, 12.45, 19, 19.02,
		19.45, 20, 20.45,
		34, 56.78, 100, 109, 119.78,
		450, 678.90, 1000, 1009.45, 1056,
		1234, 1567.98, 9999, 9999.99 };

	for (int i = 0; i < sizeof(testAmounts) / sizeof(double); i++)
	{
		double an_amount = testAmounts[i];
		checkAmount.setAmount(an_amount);
		cout << setprecision(2) << fixed;
		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;
}
What did it display? What did you want it to display?


As an aside, do not use a double for this. A double cannot perfectly represent an infinite number of simple decimal values and many of your answers will appear to be off by a cent. Use int values.
http://floating-point-gui.de/
Last edited on
It doesn't display the numerical values in the form of strings. And my teacher set up the tester file and requested we use double. I don't know why she'd do that but I don't tend to deviate from the provided code as points have been taken off before if we change hers.
Last edited on
What are you trying to accomplish the the following:
ret_val += upto20[(int)_amount], " thousand ";
I've modified my algorithm. Here is the new one:
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
string TextVersionOfNumber::getTextVersionOfNumber()
{
	string upto20[] = { "zero", "one", "two", "three", "four",
		"five", "six", "seven", "eight", "nine", "ten",
		"eleven", "twelve",
		"thirteen", "fourteen", "fifteen", "sixteen", "seventeen",
		"eighteen", "nineteen" };

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

	double _amount1 = (int)amount;
	double _amount2 = (amount - _amount1) * 100;

	string str_one = "";

	if (_amount1 <= 19)
	{
		str_one = upto20[(int)_amount1];
	}
	else
	{
		int tens = _amount1 / 10;
		int ones = _amount1 - (tens * 10);
      	str_one = ten_ninety[tens] + upto20[ones];
	}

	string str_two = "";

	if (_amount2 <= 19)
	{
		str_two = upto20[(int)_amount2];
	}
	else
	{
		int tens = _amount2 / 10;
		int ones = _amount2 - (tens * 10);
		str_two = ten_ninety[tens] + upto20[ones];
	}

	string ret_val = "$ " +str_one + " and " + str_two + " cents";
	system("pause");
	return ret_val;
};


What I'm trying to accomplish with this is getting the algorithm to convert numerical values to strings. For the most part this one works until it hits 100 and then it breaks.
Anybody got any tips? I'm going with the second algorithm but the issues are really setting me back.
Topic archived. No new replies allowed.