+ 1

C++ Question logic doubt : I need to write a program to read and translate integers into numbers. e.g 856 = Eight five six

Hi guys, I'm trying to solve it and the first number does get printed but idk how to integrate the logic of printing the rest of them. I'm a bit confused. This is my code. https://ide.codingblocks.com/s/409147 Can someone help please? Thanks!

19th Jan 2021, 7:50 PM
Shay J
Shay J - avatar
4 Answers
+ 3
I have edited the code a little bit, it should work now. I hope you will understand it... https://code.sololearn.com/cIoEbu0GBJPj/?ref=app e. g.: You enter 576 576 / 100 is 5, so you write"five" 576 - 100*5 = 76 76 / 10 is 7, so you write "seven" 76 - 10*7 = 6 6 / 1 is 6, so you write "six" 6 - 1*6 = 0
19th Jan 2021, 10:12 PM
Lukáơ Vladaƙ
+ 2
You are restricting the number to 3 digits in your code lines 20-23. Just delete them - and the else around the next block of course
19th Jan 2021, 8:15 PM
Benjamin JĂŒrgens
Benjamin JĂŒrgens - avatar
+ 1
Lukáơ Vladaƙ is right, I didn't spot that. The code in the loop can be simplified: cout << arr[n / i] << " "; n = n % i; As a side note: Reading input as string and mapping each character (=digit) to its word would be easier imo.
20th Jan 2021, 12:03 AM
Benjamin JĂŒrgens
Benjamin JĂŒrgens - avatar
0
Benjamin JĂŒrgens Thanks for responding! and actually thats also a requirement of the question, if the input is greater than 3 digits, it should print error. And I also did try what you said, but its still not working. Also it keeps printing zero after the first digit print. Any idea why?
19th Jan 2021, 9:38 PM
Shay J
Shay J - avatar