+ 1

Simple Pig Latin for c++, output exactly as expected in Code Coach yet fails all tests. Why? [Solved]

As marked "medium" in the code coach, uses only concepts taught in early lessons. Produces exact output shown as expected yet fails all Code Coach tests... would like to understand why. https://code.sololearn.com/c1wsc01UcEFH/?ref=app

15th Nov 2022, 2:38 AM
Scott D
Scott D - avatar
2 Antworten
+ 1
In the final iteration of the for loop, because of the allowing condition the code seems to be accessing the string's null character (...str[i+1]) and assigning it to the previous location. For example, for the first case input "go over there", I suspect your actual output string (even if it's not shown) is "o\0gay ver\0oay here\0tay", which is NOT the same as "ogay veroay heretay". This can be fixed with some minor adjustments. // ... previous code // (strlen - 1) ensures str[i+1] at the last // iteration is the last letter, not null char for (int i=0; i < strlen - 1; i++) { // output word starting on second letter str[i] = str[i+1]; } // assign copy of first letter to last location str[strlen - 1] = copy; // output modified str and add "ay " cout << str << "ay "; Hope this helps :)
15th Nov 2022, 5:48 AM
Mozzy
Mozzy - avatar
+ 1
Mozzy Awesome, thank you for the explanation. I will admit I'm slighty disappointed that the test gives no error and therefore it's very difficult to troubleshoot. I have seen other solutions such as: int main() { for(string p; cin>>p;) cout << p.substr(1) << p[0] << "ay "; } but for me it's not a matter of simply getting 5 xp which I really don't need. Using things like substr have not yet been taught so I feel if I don't solve the problems using course material I'm only cheating myself. Your answer explains my error very well.👍 Thanks again, I really appreciate it. https://code.sololearn.com/cBS93z7KJsdY/?ref=app
15th Nov 2022, 6:06 AM
Scott D
Scott D - avatar