What's the issue?

I'm writing a program that writes a check by converting numbers into words. (10th Grade Computer Science, should be pretty simple except for the fact that I'm me) however, the program automatically puts prints out that there is a one at the end. 6666 becomes six thousand six hundred sixty one no matter what. And it also prints every case 6 of the switch statements. (which are nested in if statements) Here's the code that seems to be causing the trouble.

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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
if (ones = 1)
		{
			switch (tens)
			{
			case 1: cout << "eleven dollars" << endl; break;
			case 2: cout << "twenty one dollars" << endl; break;
			case 3: cout << "thirty one dollars" << endl; break;
			case 4: cout << "fourty one dollars" << endl; break;
			case 5: cout << "fifty one dollars" << endl; break;
			case 6: cout << "sixty one dollars" << endl; break;
			case 7: cout << "seventy one dollars" << endl; break;
			case 8: cout << "eighty one dollars" << endl; break;
			case 9: cout << "ninety one dollars" << endl; break;
			default: cout << "one dollars" << endl; break;


			}//switch
		}//if


		if (ones = 2)
		{
			switch (tens)
			{
			case 1: cout << "twelve dollars" << endl; break;
			case 2: cout << "twenty two dollars" << endl; break;
			case 3: cout << "thirty two dollars" << endl; break;
			case 4: cout << "fourty two dollars" << endl; break;
			case 5: cout << "fifty two dollars" << endl; break;
			case 6: cout << "sixty two dollars" << endl; break;
			case 7: cout << "seventy two dollars" << endl; break;
			case 8: cout << "eighty two dollars" << endl; break;
			case 9: cout << "ninety two dollars" << endl; break;
			default: cout << "two dollars" << endl; break;


			}//switch
		}//if

		if (ones = 3)
		{
			switch (tens)
			{
			case 1: cout << "thirteen dollars" << endl; break;
			case 2: cout << "twenty three dollars" << endl; break;
			case 3: cout << "thirty three dollars" << endl; break;
			case 4: cout << "fourty three dollars" << endl; break;
			case 5: cout << "fifty three dollars" << endl; break;
			case 6: cout << "sixty three dollars" << endl; break;
			case 7: cout << "seventy three dollars" << endl; break;
			case 8: cout << "eighty three dollars" << endl; break;
			case 9: cout << "ninety three dollars" << endl; break;
			default: cout << "three dollars" << endl; break;


			}//switch
		}//if

		if (ones = 4)
		{
			switch (tens)
			{
			case 1: cout << "fourteen dollars" << endl; break;
			case 2: cout << "twenty four dollars" << endl; break;
			case 3: cout << "thirty four dollars" << endl; break;
			case 4: cout << "fourty four dollars" << endl; break;
			case 5: cout << "fifty four dollars" << endl; break;
			case 6: cout << "sixty four dollars" << endl; break;
			case 7: cout << "seventy four dollars" << endl; break;
			case 8: cout << "eighty four dollars" << endl; break;
			case 9: cout << "ninety four dollars" << endl; break;
			default: cout << "four dollars" << endl; break;


			}//switch
		}//if

		if (ones = 5)
		{
			switch (tens)
			{
			case 1: cout << "fifteen dollars" << endl; break;
			case 2: cout << "twenty five dollars" << endl; break;
			case 3: cout << "thirty five dollars" << endl; break;
			case 4: cout << "fourty five dollars" << endl; break;
			case 5: cout << "fifty five dollars" << endl; break;
			case 6: cout << "sixty five dollars" << endl; break;
			case 7: cout << "seventy five dollars" << endl; break;
			case 8: cout << "eighty five dollars" << endl; break;
			case 9: cout << "ninety five dollars" << endl; break;
			default: cout << "five dollars" << endl; break;


			}//switch
		}//if

		if (ones = 6)
		{
			switch (tens)
			{
			case 1: cout << "sixteen dollars" << endl; break;
			case 2: cout << "twenty six dollars" << endl; break;
			case 3: cout << "thirty six dollars" << endl; break;
			case 4: cout << "fourty six dollars" << endl; break;
			case 5: cout << "fifty six dollars" << endl; break;
			case 6: cout << "sixty six dollars" << endl; break;
			case 7: cout << "seventy six dollars" << endl; break;
			case 8: cout << "eighty six dollars" << endl; break;
			case 9: cout << "ninety six dollars" << endl; break;
			default: cout << "six dollars" << endl; break;


			}//switch
		}//if

		if (ones = 7)
		{
			switch (tens)
			{
			case 1: cout << "seventeen dollars" << endl; break;
			case 2: cout << "twenty seven dollars" << endl; break;
			case 3: cout << "thirty seven dollars" << endl; break;
			case 4: cout << "fourty seven dollars" << endl; break;
			case 5: cout << "fifty seven dollars" << endl; break;
			case 6: cout << "sixty seven dollars" << endl; break;
			case 7: cout << "seventy seven dollars" << endl; break;
			case 8: cout << "eighty seven dollars" << endl; break;
			case 9: cout << "ninety seven dollars" << endl; break;
			default: cout << "seven dollars" << endl; break;

			}//switch
		}//if

		if (ones = 8)
		{
			switch (tens)
			{
			case 1: cout << "eighteen dollars" << endl; break;
			case 2: cout << "twenty eight dollars" << endl; break;
			case 3: cout << "thirty eight dollars" << endl; break;
			case 4: cout << "fourty eight dollars" << endl; break;
			case 5: cout << "fifty eight dollars" << endl; break;
			case 6: cout << "sixty eight dollars" << endl; break;
			case 7: cout << "seventy eight dollars" << endl; break;
			case 8: cout << "eighty eight dollars" << endl; break;
			case 9: cout << "ninety eight dollars" << endl; break;
			default: cout << "eight dollars" << endl; break;

			}//switch
		}//if

		if (ones = 9)
		{
			switch (tens)
			{
			case 1: cout << "nineteen dollars" << endl; break;
			case 2: cout << "twenty nine dollars" << endl; break;
			case 3: cout << "thirty nine dollars" << endl; break;
			case 4: cout << "fourty nine dollars" << endl; break;
			case 5: cout << "fifty nine dollars" << endl; break;
			case 6: cout << "sixty nine dollars" << endl; break;
			case 7: cout << "seventy nine dollars" << endl; break;
			case 8: cout << "eighty nine dollars" << endl; break;
			case 9: cout << "ninety nine dollars" << endl; break;
			default: cout << "nine dollars" << endl; break;


			}//switch
		}//if
	
} 
First thing I see is your if statements - if you're comparing two items to see if they're equal to each other, you need to use == , not the assignment operator =.


Edit: How are you getting the value for tens?
Last edited on
Hmm without more to look at I'm not quite sure what the exact problem might be. But I suspect it may have something to do with your ones and tens variables not grabbing the right numbers. Try sticking a cout before you use the switch statements, just to see what each variable holds.

Because of the way switch statements work, case 1 is going to see if the variable is equal to 1.

So for the tens spot case 1 looks to see if tens = 1. If you were to change it to case 10, it would look to see if tens = 10.

wildblue is also right! I didn't even think of that! Use ones == 1.
Last edited on
wildblue, you're a life saver. Such a simple error too. That's just embarrassing
Topic archived. No new replies allowed.