# Thread: [Jumping into C++] Chapter 7 Problem 1 Question

1. ## [Jumping into C++] Chapter 7 Problem 1 Question

Hi everyone, I'm new to this forum. I've been reading Jumping Into C++ and I love the book so far. My problem is that is directly jumps into hard mode with these practice problems. I have no idea how to do "1. Implement the source code that turns numbers into English text. " because I haven't learned it yet. I was hoping the book would be straight forward that you learn everything you need to complete the problem but I guess it doesn't seem the way. I haven't tried solving it because I have no idea what to do. I'm stumped, any help would be greatly appreciated.

2. The world isn't full of problems where you know everything to solve the problem.

Sooner or later, you have to start to really think about problems, break them down and solve problems in steps.

For example, if the input number was restricted to say 1 to 10, could you make a go of it?

3. I don't believe anyone answering questions here was involved in writing that book. In fact, most of us don't have a copy and have no idea what's in it so it's best not to assume that we are familiar with it.

However, sneaking a cheeky peek at a copy it looks to me that he's given you more than enough information. In fact, he's pretty much told you how to do it.

From the text:
So our algorithm will look something like this:
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
Where are you stuck?

4. Originally Posted by Salem
The world isn't full of problems where you know everything to solve the problem.

Sooner or later, you have to start to really think about problems, break them down and solve problems in steps.

For example, if the input number was restricted to say 1 to 10, could you make a go of it?
Yeah because it's restricted. I'm thinking of if you type any number at all it will translate it to you in English. But you have a point I guess I should just try. Thank you for the response.

5. Originally Posted by algorism
I don't believe anyone answering questions here was involved in writing that book. In fact, most of us don't have a copy and have no idea what's in it so it's best not to assume that we are familiar with it.

However, sneaking a cheeky peek at a copy it looks to me that he's given you more than enough information. In fact, he's pretty much told you how to do it.

From the text:

Where are you stuck?
How to actually break down the 3 digit chunks because I haven't learned arrays yet (unless it doesn't require arrays).

6. Code:
```#include <iostream>
#include <string>

using namespace std;

string tens(int number);

int main()
{
cout << tens(3);
}

string tens(int number)
{
if (number == 0)
{
return "Zero";
}
else if (number == 1)
{
return "One";
}
else if (number == 2)
{
return "Two";
}
else if (number == 3)
{
return "Three";
}
else if (number == 4)
{
return "Four";
}
else if (number == 5)
{
return "Five";
}
else if (number == 6)
{
return "Six";
}
else if (number == 7)
{
return "Seven";
}
else if (number == 8)
{
return "Eight";
}
else if (number == 9)
{
return "Nine";
}
}```

7. How to actually break down the 3 digit chunks
That depends. Recursion would simplify the problem, but you may not know about that yet.

Here's another (untested!) possibility:
Code:
```if (n >= 1000000000) {
chunk = n / 1000000000;
n %= 1000000000;
} else if (n >= 1000000)
chunk = n / 1000000;
n %= 1000000;
} else if (n >= 1000)
chunk = n / 1000;
n %= 1000;
} else {
chunk = n;
n = 0;
}```
That gives you the highest 3 digit (at the most) chunk and reduces n so that the next time you will get the next highest chunk.
If you need to go beyond billions (as you would for a 64-bit integer), then you'll need to extend it in the obvious way.

8. Good.

Now study what this does
int a = 42;
cout << a / 10 << endl;
cout << a % 10 << endl;

9. Originally Posted by Salem
Good.

Now study what this does
int a = 42;
cout << a / 10 << endl;
cout << a % 10 << endl;
Code:
```#include <iostream>
#include <string>
using namespace std;
string tens(int number);
int main()
{
int number;
cin >> number;
if (number < 10)
{
cout << tens(number) << endl;
}
else if (number >= 10 && number < 100)
{
int first_digit, second_digit;
first_digit = number / 10;
second_digit = number % 10;
cout << tens(first_digit) << " " << tens(second_digit) << endl;
}
return 0;
}
string tens(int number)
{
if (number == 0)
{
return "Zero";
}
else if (number == 1)
{
return "One";
}
else if (number == 2)
{
return "Two";
}
else if (number == 3)
{
return "Three";
}
else if (number == 4)
{
return "Four";
}
else if (number == 5)
{
return "Five";
}
else if (number == 6)
{
return "Six";
}
else if (number == 7)
{
return "Seven";
}
else if (number == 8)
{
return "Eight";
}
else if (number == 9)
{
return "Nine";
}
}```
I'm getting there but now I'm stuck on the "teen" part. I can see a lot of "if" statements incoming.

10. Another small clue that might simplify things.
Code:
```string tens(int number) {
char *words[] = {
"Zero","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine"
};
return words[number%10];
}```

11. Originally Posted by Salem
Another small clue that might simplify things.
Code:
```string tens(int number) {
char *words[] = {
"Zero","One","Two","Three","Four",
"Five","Six","Seven","Eight","Nine"
};
return words[number%10];
}```

stray '/240' in program error

12. Originally Posted by LordAbolition
stray '/240' in program error
The stray '\240' has nothing to do with Salem's code. You are obviously using the despicable windows OS. It has somehow converted at least some of the spaces to "non-breaking" spaces, presumably when you copied and pasted it. All I can say is to try again. You could also delete and retype all the spaces in that chunk of code.

13. Originally Posted by algorism
The stray '\240' has nothing to do with Salem's code. You are obviously using the despicable windows OS. It has somehow converted at least some of the spaces to "non-breaking" spaces, presumably when you copied and pasted it. All I can say is to try again. You could also delete and retype all the spaces in that chunk of code.
You were right it was the spaces, I just had to reposition it more. It's odd that it does that but anyway back to work.

14. Alright this is honestly the best I can do. It keeps crashing at the end and I don't know why.

Code:
```#include <iostream>
#include <string>
using namespace std;
string tens(int number);
string singles(int number);
string bigger_tens(int number);
int main()
{
int number;
cin >> number; */
for (int i=10; i < 30; i++)
{
if (i < 10)
{
cout << singles(i) << endl;
}
else if (i >= 10 && i < 20)
{
cout << tens(i) << endl;
}
else if (i >= 20 && i < 100)
{
cout << bigger_tens(i) << endl;
}
}
return 0;
}
string singles(int number)
{
char *words[] =
{
"Zero","One","Two","Three","Four","Five","Six","Seven","Eight","Nine"
};
return words[number%10];
}
string tens(int number)
{
char *words[] =
{
"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"
};
return words[number%10];
}
string bigger_tens(int number)
{
char *words[] =
{
"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
};
string first_word = words[number%10];
string second_word = singles(number/10);
string whole_word = first_word + " " + second_word;
return whole_word;
}```

15. The program you posted doesn't even compile with my compiler so I can't duplicate your reported problem.
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
main.cpp|31|error: ISO C++ forbids converting a string constant to ‘char*’ [-Wpedantic]|
Plus the other two places where you're doing similar operations.

Why are you using C-strings, you seem to know about std::string so why not use the std::string instead. I would also recommend you consider using std::vector instead of the array and use the .at() function instead of the [] to access the vector.

Something like:
Code:
```string bigger_tens(int number)
{
std::vector<std::string> words{
"Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"
};
string first_word = words.at(number%10);
string second_word = singles(number/10);
string whole_word = first_word + " " + second_word;
return whole_word;
}```