+ 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!
4 Réponses
+ 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
+ 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
+ 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.
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?