+ 1

Help, please :)

https://www.sololearn.com/coach/51?ref=app #include <iostream> #include <cstring> using namespace std; int main() { string word; string trans; //cout<<"Enter a word to translate - "; cin>>word; //cout<<endl<<"Translation - "; int l=word.length(); //gets string length do { // writes the word backward l--; trans+=word[l]; } while (word[l]); cout<<trans; return 0; } Frustrated:( Why does it think that the code is incorrect?

21st Dec 2019, 12:28 PM
Dmitriy Sychov
Dmitriy Sychov - avatar
6 Respostas
+ 3
Lets say the word is "h" ( because I'm too lazy to come up with a word ) I = 1; ( length = 1 ) Begin: I--; ( I = 0 ) trans += 'h' If h go back to Begin: Since h will result in true, we go back to Begin: I-- ( I = -1 ) trans += "???" Sometimes you should go for the rubber duck debugging approach
21st Dec 2019, 12:52 PM
Dennis
Dennis - avatar
+ 2
The rubber duck debugging approach is just a method where you go line by line and explain yourself what is happening and compare it with what is supposed to happen and in that way, find the bug(s). while( l ) might work with the test cases, but it still doesn't handle the case when the word length is 0, so watch out for that. But I can understand it if you choose to ignore that, since for this particular problem, that won't happen anyway.
21st Dec 2019, 7:36 PM
Dennis
Dennis - avatar
+ 1
I've just got the problem. There is some base code in the task which was deleted by me and replaced fully by ready code. I will try again to resolve by adding needed part to the base.
21st Dec 2019, 12:54 PM
Dmitriy Sychov
Dmitriy Sychov - avatar
+ 1
Dennis, thanks for the comment. By the way, I didn't understand it :D Will try to Google your last sentence. What about the code. I replaced 'while (word[l])' with 'while (l)' and it works now.
21st Dec 2019, 7:28 PM
Dmitriy Sychov
Dmitriy Sychov - avatar
+ 1
Dennis, thanks. I understood.
21st Dec 2019, 8:40 PM
Dmitriy Sychov
Dmitriy Sychov - avatar
0
Selam meraba
23rd Dec 2019, 9:19 AM
Arzu Arzu