Really need help (new to c++)

EDIT: SOLVED TNX
Last edited on
Is it allowed to use arrays?

This should help
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <conio.h>
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{
	int num, hund, tens, ones, temp;
// 	system("cls");
	printf("Enter a number from -999-999: ");
	scanf("%d", &num);

	// ex. : 123
	hund = num / 100;	// hund = 1
	temp = num % 100;	// temp = 23
	tens = temp / 10;	// tens = 2
	ones = temp % 10;	// ones = 3
	if(num >= -999 && num < 999) // Check to see if num is valid
	{
		
	//hundreds
	if(num >= 100 && num < 999)
	{
		if(hund == 1)
		{
			printf("one hundred");
		}
		else if(hund == 2)
		{
			printf("two hundred");
		}
		else if(hund == 3)
		{
			printf("three hundred");
		}
		else if(hund == 4)
		{
			printf("four hundred");
		}
		else if(hund == 5)
		{
			printf("five hundred");
		}
		else if(hund == 6)
		{
			printf("six hundred");
		}
		else if(hund == 7)
		{
			printf("seven hundred");
		}
		else if(hund == 8)
		{
			printf("eight hundred");
		}	
		else if(hund == 9)
		{
			printf("nine hundred");
		}

num=num-(hund*100);		// New NUM
temp = num % 100;	// temp = 23
tens = temp / 10;	// tens = 2
	
if (temp > 19)
{
			
		if(tens == 1)	
		{
			printf(" ten");
		}
		else if(tens == 2)
		{
			printf(" twenty");
		}
		else if(tens == 3)
		{
			printf(" thirty");
		}
		else if(tens == 4)
		{
			printf(" fourty");
		}
		else if(tens == 5)
		{
			printf(" fifty");
		}
		else if(tens == 6)
		{
			printf(" sixty");
		}
		else if(tens == 7)
		{
			printf(" seventy");
		}
		else if(tens == 8)
		{
			printf(" eighty");
		}
		else if(tens == 9)
		{
			printf(" ninety");
		}
		else if(tens == 0)
		{
			printf("");
		}

		if(ones == 0)
		{
			printf("");
		}
		else if(ones == 1)
		{
			printf(" one");
		}
		else if(ones == 2)
		{
			printf(" two");
		}
		else if(ones == 3)
		{
			printf(" three");
		}
		else if(ones == 4)
		{
			printf(" four");
		}
		else if(ones == 5)
		{
			printf(" five");
		}
		else if(ones == 6)
		{
			printf(" six");
		}
		else if(ones == 7)
		{
			printf(" seven");
		}
		else if(ones == 8)
		{
			printf(" eight");
		}
		else if(ones == 9)
		{
			printf(" nine");
		}
}
	else
	{

		if(temp == 10)
		{
			printf(" ten");
		}
		else if(temp == 11)
		{
			printf(" eleven");
		}
		else if(temp == 12)
		{
			printf(" twelve");
		}
		else if(temp == 13)
		{
			printf(" thirteen");
		}
		else if(temp == 14)
		{
			printf(" fourteen");
		}
		else if(temp == 15)
		{
			printf(" fifteen");
		}
		else if(temp == 16)
		{
			printf(" sixteen");
		}
		else if(temp == 17)
		{
			printf(" seventeen");
		}
		else if(temp == 18)
		{
			printf(" eighteen");
		}
		else if(temp == 19)
		{
			printf(" nineteen");
		}		
	}

	printf("\n", hund, temp, tens, ones);
	}


	}
	else
	{
		puts("Your number is out of range.");
	}
	

// 	system("pause");
	return 0;
}
Last edited on
This is what I came up with. I wasn't sure if arrays are allowed, so I used them anyway. It's much much shorter and quite impossible to make a mistake if you're careful.
Didn't do too much error checking...

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
#include<stdio.h>
#include<conio.h>

int main()
{
	int num;
	system("cls");
	printf("Enter a number from -999 to 999: ");
	scanf("%d", &num);

	int ones = num / 1 % 10;
	int tens = num / 10 % 10;
	int hund = num / 100 % 10;

	const char *one[] = {" ", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"};
	const char *ten[] = {" ", "ten", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninety", "hundred"};
	const char *ten2[] = {"ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};


	if (num < 0)
	{
		printf("Negative ");
		//makes all the digits positive since the number entered is negative
		hund = hund - hund * 2;
		tens = tens - tens * 2;
		ones = ones - ones * 2;
	}

	if (num >= -999 && num <= 999)
	{	
		if (tens == 1)
		{
			printf("%s hundred %s", one[hund], ten2[ones]);
		}
		else
		{
			printf("%s hundred %s %s", one[hund], ten[tens], one[ones]);
		}
	}
	else
	{
		printf("Your number is out of range!");
	}

	getch();
	return 0;
}
Last edited on
Thanks samueladams,

the code was definitely a big help

though the only values that works are from positive 100 to 999 :(
thanks too sasauke,

but we're not allowed to use arrays :\
had something similar to sasauke before I noticed there were replies below xD

If you can't use arrays use a big string, all words seperated by a space, and find the correct item depending on the number
 
char *ones_str="one two three four etc";

and use the same sort of code to print out the goods
Topic archived. No new replies allowed.