+ 1
Can someone help me with this C++ code?
I'm trying to solve this code coach. https://www.sololearn.com/coach/64?ref=app One have to accept input and check for numbers. One have to change each numbers to their word eg. 1 will change to "one". The problem is each time the program encounter a space in the input, it stops. E.g user input : "ilove8 mama and 9 papa" My output: iloveeight Instead of: iloveeight mama and nine papa. My code: https://code.sololearn.com/cnYTxkn0BYAU/?ref=app I wish there is the "in" statement in c++ đŁ
8 Answers
+ 5
Timothy Adeleke
AJ #Infinity Love is correct 10 should be output as ten, while 11 and up should be numerical.
Also, you can't use cin to get an entire string to the newline when there are spaces. It will only grab the 1st word. Or up to the first whitespace character.
Try using getline()
+ 4
Take in the entire string and then break it up into an array or vector of strings, spliting on the spaces. You can actually loop with getline and a char delimiter to pushback the strings into a vector of string when geting the input.
Then loop over each word and check its value. output the word if it's not one to replace, otherwise output its replacement.
https://thispointer.com/how-to-split-a-string-in-c/
+ 2
Timothy Adeleke 10 is missing. You need to check number 10 first then 0 and 1 and here you need to replace number with String but you are just printing replaced number.
+ 2
Timothy Adeleke No 10 will not be taken as 1 and 0 separately. There is one test case with number 10.
Also you need to print original string with replaceed String but you are just printing replaced String.
+ 2
Thank you ChaoticDawg . I used getline and it worked!
Thank you AJ #Infinity Love I'll add 10
+ 2
Timothy Adeleke You should first check number 10 then 1 and 0 like
if(word[I] == 10) {
} else if (word[I] == 1 || word[I] == 0) {
}
+ 1
AJ #Infinity Love
That's another problem.
The input has been saved as array of char. 10 will be taken as 1 and 0 separately.
+ 1
But 10 is still taken separately what do I do?