Numbers to words (0 to 1million) using only iostream and basic functions

I've found numerous threads on here with this question, but none that use only simple operators (if else, while, for, switch and functions).

I am taking C++ in college, first time, and we have a limited amount of tools at our disposal, so that I can't use more complicated functions to complete my task.

I got the code almost working, but I think I am missing something, not sure what.

Please help improve my code, with simple operators only.

Thank you in advance.

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
 #include <iostream>
using namespace std;
void printDigit(int);
void printTensDigit(int);

int main()
{
		int number, currDigit, digitsCount = 1;
	int x = 3;
	
	while (true)
	{
		do
		{
			cout << "Please input a positive integer between 0 and 999,999 or -1 to exit: ";
			cin >> number;
			if (number == -1)
			{
				return 0;
			}

		} while (number < 0);

		//count the number of digits the integer has
		int temp = number;
		while ((temp /= 10) > 0)
		{
			digitsCount++;
		}
		cout << "The number has " << digitsCount << " digits." << endl;

		while (digitsCount > 0)
		{
			digitsCount--;

			//1st method
			//currDigit = number / pow(10, digitsCount);
			//number = number - currDigit * pow(10, digitsCount);

			//2nd method
			currDigit = (number % (int)pow(10, digitsCount + 1)) / pow(10, digitsCount);

			int currentMultiplier = pow(10, digitsCount);

			// cout << "The current digit is " << currDigit << " * " << pow(10, digitsCount) << " ." << endl;

			if ((currentMultiplier >= 10 && currentMultiplier < 99) ||
				(currentMultiplier >= 10000 && currentMultiplier < 99999))
			{
				printTensDigit(currDigit);
			}
			else if ((currentMultiplier >= 100 && currentMultiplier < 999) ||
				(currentMultiplier >= 100000 && currentMultiplier < 999999))
			{
				if (currDigit != 0){
					printDigit(currDigit);
					cout << "Hundred ";
				}
			}
			else
			{
				printDigit(currDigit);
			}

			if (pow(10, digitsCount) == 1000)
			{
				cout << "Thousand ";
			}

		}
		cout << endl << endl;
		system("pause");
	}
	return 0;
}

void printDigit(int x)
{
	switch (x)
	{
	case 0:
		cout << "  ";
		break;
	case 1:
		cout << "One ";
		break;
	case 2:
		cout << "Two ";
		break;
	case 3:
		cout << "Three ";
		break;
	case 4:
		cout << "Four ";
		break;
	case 5:
		cout << "Five ";
		break;
	case 6:
		cout << "Six ";
		break;
	case 7:
		cout << "Seven ";
		break;
	case 8:
		cout << "Eight ";
		break;
	case 9:
		cout << "Nine ";
		break;
	default:
		cout << "Invalid input" << endl;
	}
}

void printTensDigit(int x)
{
	switch (x)
	{
	case 0:
		cout << "  ";
		break;
	case 1:
		cout << "Ten ";
		break;
	case 2:
		cout << "Twenty ";
		break;
	case 3:
		cout << "Thirty ";
		break;
	case 4:
		cout << "Fourty ";
		break;
	case 5:
		cout << "Fifty ";
		break;
	case 6:
		cout << "Sixty ";
		break;
	case 7:
		cout << "Seventy ";
		break;
	case 8:
		cout << "Eighty ";
		break;
	case 9:
		cout << "Ninety ";
		break;
	default:
		cout << "Invalid input" << endl;
	}
}
Last edited on
Hi,

Thanks for using code tags, but if you could get rid of those embedded line numbers, so we can compile it with the cpp.sh that comes with properly formatted code.

I am sure you will get more replies if you do this :+)

Good Luck!!
Thanks for the idea, appropriately named user.
You need to reset digitCount to 1 whenever you start with a number.

The program doesn't work for anything >= 1 million, so you should check for that input.

The user has no way of knowing without looking at your code that -1 is the exit flag.

I was getting some weird spacing with the output that you should check into.
Where is it that I have to reset digitCount?

The program doesn't need to work after 1 million.

Fixed -1 prompt.

You need to reset at the beginning of you loop, before you check the number of digits in the new number.

I know the program doesn't need to work after 1 million, but if you enter 1 million of more it provides false output, instead of an error message of some sort.
Topic archived. No new replies allowed.