Help Fixing Simulated Check Program

I have just started with c++ programming a month ago. I have an assignment where I need to have an ouput that shows a simulated check.

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
131
132
133
134
135
#include <iostream>
#include <cstdlib>
#include <string>
#include <iomanip>
using namespace std;

//****************************************************************************
//			Function Prototypes follow
//****************************************************************************

void printArrayContents(string, int SIZE);

//****************************************************************************
//			Main Line
//****************************************************************************

int main()
{
	char *ones[]={				"	",
								"one",
								"two",
								"three",
								"four",
								"five",
								"six",
								"seven",
								"eight",
								"nine"};
	char *teens[]={				"ten",
								"eleven",
								"twelve",
								"thirteen",
								"fourteen",
								"fifteen",
								"sixteen",
								"seventeen",
								"eighteen",
								"nineteen"};
	char *tens[]={				"	",
								"	",
								"twenty",
								"thirty",
								"forty",
								"fifty",
								"sixty",
								"seventy",
								"eighty",
								"ninety"};
	char hundred[]={			" hundred "};
	string date;
	string name;
	double amount;
	char and[] = " and ";
	char value_str[50] = "";
	int value = 0;                       
	int digits[] = {0,0,0};        
	int i = 0;

	// Get The Date
	cout << "Enter the date: ";
	cin >> date;
	cin.ignore();

	// Get name
	cout << "Enter your name: ";
	getline(cin,name);

	// Get amount
	cout << "Enter the amount: ";
	cin >> amount;

	// Validate amount
	if (amount < 0)
	{
		cout << "Invalid amount: amount cannot be negative or over 1000. Please re-enter: ";
		cin >> amount;
	}
	else if (amount >= 1000)
	{
		cout << "Invalid amount: amount cannot be negative or over 1000. Please re-enter: ";
		cin >> amount;
	}

	printf("Enter the amount: ");
	scanf("%d",&value);
	if(value>=1000)
	{
		cout << "Invalid amount: amount cannot be negative or over 1000. Please re-enter: ";
		cin >> value;
	}
	else if(value<1)
	{
		cout << "Invalid amount: amount cannot be negative or over 1000. Please re-enter: ";
		cin >> value;
	}

	while(value>0)
	{
    	digits[i++] = value%10;
    	value /= 10;
	}

	if(digits[2] > 0)
	{
    	strcat(strcat(value_str,ones[digits[2]]), hundred);
    	if(digits[1] >0 || digits[0] > 0)
			strcat(value_str, and);
	}
	if(digits[1] > 0)
	{
   		if(digits[1] == 1)
     		strcat(value_str,teens[digits[0]]);
    	else
    	{
      		strcat(value_str,tens[digits[1]]);
      		if(digits[0] > 0)
        		strcat(strcat(value_str, " "), ones[digits[0]]);
    	}
	}
  	else
    	if(digits[0] > 0)
      		strcat(value_str, ones[digits[0]]);

	// Show Final Result
	{
		cout << setprecision(2) << fixed << showpoint;
		cout << "========================================" <<endl <<endl;
		cout << "\n																	Date:	" << date;
		cout << "\nPay to the order of: " << name; cout << "			$" << amount;
		cout << "\n		" << cout << printf("\n%s\n", value_str) << "cents";
	}

	return 0;

}


I have two major issues. The first is that for some reason I have the program asking me for the amount twice, yet both outputs are to different places. The first shows the output in its monetary form ($10.00), while the 2nd time it asks, it displays it in word form (ten). I need to get both those result, but with one entry instead of 2.

Also, I cannot get it to display the cents. When I enter $10.00, it shows up as 'ten' and never adds the cents. I just don't know how to do it with what I have. The program is messy because I have added on over my starting point so there is useless stuff there at the moment, but any help is appreciated.
You have two inputs, because you wrote two inputs. Delete lines 84-95 and replace them with value = amount.

The reason why you don't get cents is (apart from that you didn't write any code to do it) is that since value is an integer, it can't represent fractions. However you can't make it a double, as then you couldn't use %. The key is to notice that while you have a fraction of dollars, you have an integer of cents. That is, have two variables
1
2
int dollar_value = amount;
int cent_value = (ammount-dollar_value)*100;

And make words out of them separately.

Another thing, don't use strcat (and other c string functions). They are hard to read and hard to write. You can instead use std::string and replace strcats with operator + or +=.
Last edited on
Topic archived. No new replies allowed.