Numbers to Strings

Working on a project for school that converts dollar amounts to written check amounts. I have most of the code done already but I obviously messed up somewhere in the string conversion I'm just not sure where and how. Just asking for some help and the modf function must be used as well as the two functions and arrays.


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
#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <math.h>


using namespace std;

const string digitStr[] = {"zero","one","two","three","four","five","six","seven","eight","nine"};
const string teenStr[] = {"eleven","twelve","thirteen","fourteen","fifteen","sixteen","seventeen","eighteen","nineteen"};
const string tenStr[] = {"ten","twenty","thirty","forty","fifty","sixty","seventy","eighty","ninety"};
const string suffix[] = {" ", "thousand", "million" , "billion"};

//---------------------------------------------------------------------
string writtenThreeDigits (int threeDig )
{
	//	(ndx%10);
	//	(ndx/10);

	//wholeNumber = 123


	unsigned int ones, tens, hundreds;

	ones = threeDig % 10;
	
	tens = (threeDig / 10) % 10;
	
	hundreds = (threeDig / 10) %10;

	string hundred;
	
	if ( hundreds != 0 ) 
	{
		hundred = hundred[hundreds] + " hundred ";
	}

	 if ( tens == 1 )  
	 {
	    hundred = hundred + tenStr[tens];
	 } 
	 else 
	 { 
		hundred = hundred + tenStr[tens] + digitStr[ones];		
	 }
	
	 return hundred;
	

	
	//return "error written three digits not implemented";
}

//---------------------------------------------------------------------
string writtenNumber (int wholeNumber)
{
	string totalAnswer = "";
	int groupNum = 0;

	do{
		int threeDig = wholeNumber % 1000;
		wholeNumber = wholeNumber / 1000;

		string threeDigit = writtenThreeDigits( threeDig );
			
		totalAnswer = threeDigit + suffix[groupNum]+ " " + totalAnswer;

		groupNum++;

	}while( wholeNumber != 0 );

	return totalAnswer;

	//return "error written number not implemented";


}

int main()
{
	double amount, wholeDollars, wholeCents;
	//string writtenNumber();
			
	while ( !cin.eof() )
	{	
		
		amount = -1.0 ;
		while ( amount < 0 )
		{
			cout << "Enter a dollar amount: ";
			cin >> amount;
			
			// break in middle if done
			if ( cin.eof() )
				break;
			

			if ( cin.fail() )
			{
				cout << "You entered an invalid dollar amount!\n" ;
				cin.clear();
				cin.ignore(1024, '\n');
				amount = -1.0;
			}
		
				
			wholeCents =  modf( amount, &wholeDollars );
			cout<< wholeDollars << " dollars " << "and " << wholeCents << " cents" << endl;

		}

		string writtenDollars = writtenNumber ( wholeDollars );
		string writtenCents = writtenNumber ( wholeCents );

		cout << fixed << setprecision(2)<< endl;
		cout << "You entered: " << amount << endl;
		cout << "$" << setw(14) << setfill('*') << amount << endl ;
		cout << wholeDollars << " " << wholeCents << endl;
	
		 
	}

}





Last edited on
please use code tags, they are the <> symbol under format. also, so you want to convert strings to numbers?
sorry about that, fixed it. I'm trying to get the output of the string to display correctly, somewhere its buggy and returns the wrong output.
Topic archived. No new replies allowed.