Need some hints, trying to solve chapter 7 in the book 'Jumping Into C++'

Hello, i've been trying to solve this for a day now, i can't seem to figure it out.

I'm on chapter 7 in the book Jumping Into C++. I feel i'm always very close to the solution, and i'm pretty sure i am, but i always fail when i'm trying to think of the logic. Last time, i ended up deleting a lot of the code, because i felt i over complicated it.

I do know about arrays and i know how to use them, but because i haven't been introduced to arrays in the book yet, i don't want to use arrays because i know there's a solution without arrays.

Anyway, let me get to the point.

The problem is, i can't think of how i would break the number the user types in, into chunks of 3 digits or 2 etc. I really need some hints, i would appreciate it.

I'm following these steps, from the book:

1) Break the number up into chunks of 3 digits

2) For each three digit chunk, compute the text; append the magnitude of that chunk; append the chunks together

3) To compute the text of a three digit chunk, compute the number of hundreds, and convert that one digit number to text, and add “hundreds”, appending the text of the two digit chunk

4) To compute the text of a two digit chunk, if it’s less than 20, look it up; if it’s greater than 20, compute the number of tens, and look up the word, and append the text of the one digit number

I did succeed the 1st step one time, but then when i came to step 2 i didn't know how i would convert it to text like hundred, or thousand etc.

Here's the rewritten code. I don't have any syntax problems, it's just the logic :-)

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
#include <iostream>

void BreakUpIntoChunks(int input);
void ConvertNumbersToWords(int chunk1, int chunk2, int chunk3, int chunk4, int chunk5);

int main()
{
	int userInput = 0;
	bool close = false;

	while (!close)
	{
		std::cin >> userInput;

		BreakUpIntoChunks(userInput);
	}

	std::cin.ignore();
	std::cin.get();
}

void BreakUpIntoChunks(int input)
{
	//Problem is how would i know when to print, thirty etc. (Example: 1,942,412 | one million, nine hundred, fourty two, four hundred, twelve)
	int chunk1 = 0;
	int chunk2 = 0;
	int chunk3 = 0;
	int chunk4 = 0;
	int chunk5 = 0;

        /*Wasn't sure how the (input % x) / x stuff worked, but i'm beginning to understand how it all works. Works somewhat like i want it to do, it is just the number to text, the real problem is*/

	chunk1 = (input % 10) / 1; //Ones
	std::cout << "chunk1: " << chunk1 << "\n";

	chunk2 = (input % 100) / 10; //Tens
	std::cout << "chunk2: " << chunk2 << "\n";

	chunk3 = (input % 1000) / 100; //Hundreds
	std::cout << "chunk3: " << chunk3 << "\n";

	chunk4 = (input % 1000000) / 1000; //Thousands
	std::cout << "chunk4: " << chunk4 << "\n";

	chunk5 = (input % 1000000000) / 1000000; //Millions
	std::cout << "chunk5: " << chunk5 << "\n";

	if (input == 10)
	{
		std::cout << "ten ";
	}
	else if (input == 11)
	{
		std::cout << "eleven ";
	}
	else if (input == 12)
	{
		std::cout << "twelve ";
	}
	else if (input == 13)
	{
		std::cout << "thirteen ";
	}
	else if (input == 14)
	{
		std::cout << "fourteen ";
	}
	else if (input == 15)
	{
		std::cout << "fifteen ";
	}
	else if (input == 16)
	{
		std::cout << "sixteen ";
	}
	else if (input == 17)
	{
		std::cout << "seventeen ";
	}
	else if (input == 18)
	{
		std::cout << "eightteen ";
	}
	else if (input == 19)
	{
		std::cout << "nineteen ";
	}

	ConvertNumbersToWords(chunk1, chunk2, chunk3, chunk4, chunk5);
}

void ConvertNumbersToWords(int chunk1, int chunk2, int chunk3, int chunk4, int chunk5)
{
	/*if (chunk1 == "1")
	{
		chunk1 = "one ";
	}
	else if (chunk1 == "2")
	{
		chunk1 = "two ";
	}
	else if (chunk1 == "3")
	{
		chunk1 = "three ";
	}
	else if (chunk1 == "4")
	{
		chunk1 = "four ";
	}
	else if (chunk1 == "5")
	{
		chunk1 = "five ";
	}
	else if (chunk1 == "6")
	{
		chunk1 = "six ";
	}
	else if (chunk1 == "7")
	{
		chunk1 = "seven ";
	}
	else if (chunk1 == "8")
	{
		chunk1 = "eight ";
	}
	else if (chunk1 == "9")
	{
		chunk1 = "nine ";
	}
	else if (chunk1 == "10")
	{
		chunk1 = "ten ";
	}
	else if (chunk1 == "11")
	{
		chunk1 = "eleven ";
	}
	else if (chunk1 == "12")
	{
		chunk1 = "twelve ";
	}
	else if (chunk1 == "13")
	{
		chunk1 = "thirteen ";
	}
	else if (chunk1 == "14")
	{
		chunk1 = "fourteen ";
	}
	else if (chunk1 == "15")
	{
		chunk1 = "fifthteen ";
	}
	else if (chunk1 == "16")
	{
		chunk1 = "sixteen ";
	}
	else if (chunk1 == "17")
	{
		chunk1 = "seventeen ";
	}
	else if (chunk1 == "18")
	{
		chunk1 = "eightteen ";
	}
	else if (chunk1 == "19")
	{
		chunk1 = "nineteen ";
	}*/
	


	std::cout << chunk5 << "," << chunk4 << "," << chunk3 << "," << chunk2 << "," << chunk1;
}


I will feel pretty bad if i have to skip this one
Last edited on
To solve this you need the principle of 'divide and conquer'. I.e. put things that belongs together into functions.

Like so:
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
void PrintOnesWord(int value)
{
  switch(value)
  {
    case 1:
      std::cout << "one";
    case 2:
      std::cout << "two";
...
  }
}
void Print2DigitsWord(int value, const char *what) // what = "twenty", "thirty", ...
{
  if(value > 0)
  {
    std::cout << what << " ";
    PrintOnesWord(value);
  }
}
void PrintTensWord(int value)
{
  if(value < 1)
    std::cout << "0";
  else if(value < 10)
    PrintOnesWord(value);
  else if((value / 10) == 1)
  {
    switch(value)
    {
    case 10:
      std::cout << "ten";
    case 11:
      std::cout << "eleven";
...
    }
  }
  else switch(value / 10)
  {
    case 2:
      Print2DigitsWord(value % 10, "twenty");
      break;
    case 3:
      Print2DigitsWord(value % 10, "thirty");
      break;
...
  }
}
void PrintHighNumbersWord(int value, const char *what) // what = "hundred" or "thousands" or "milion"
{
  if(value > 0)
  {
    PrintOnesWord(value);
    std::cout << " " << what;
  }
}
This might give you some ideas
Last edited on
Thanks, i'll see if i succeed :-) Thank you again :-)
Topic archived. No new replies allowed.