Where is my bug? | Sololearn: Learn to code for FREE!


Where is my bug?

I am trying to restart development of my previous code - Encryption. Now my code (for some mystical reason) doesn't want to take any input. Update: It works! (Kind of...) Sorry for previous negative attitude. https://code.sololearn.com/c1A15A24A17a/?ref=app

4/13/2021 5:45:46 PM


7 Answers

New Answer


Jerry Hobby it probably is a SL issue of some kind. Also, cin.getline() and getline() are not the same. cin.getline() is for C style string where getline() is used with C++ strings. It seems the issues are happening whenever C string inputs are used. I haven't seen it yet using the std::getline()


ChaoticDawg I didn’t realize they were different. I tried getline() first, but it didn’t compile. But I’m a little rusty, so I used cin.getline and that worked. Maybe the problem is related somehow. Because when I used only cin.getline() twice, it didn’t work. but when I used cin.getline() followed by cin << on the next line, both inputs worked. I’m guessing stdin doesn’t map properly / reliably. Interesting. Thanks.


Technically, fgets() should work with C++, but it is a C function not a C++ function. In C++ to get input from stdin, cin is used. cin >> text; cin >> action; Using this your code works. Note that cin will take input until the first whitespace is reached. Idk, if SL removed the use of fgets() or if it is a bug, but you can contact them using the feedback option in the app to let them know the issue. You can also change your code to use getline if desired. string text; string action; getline(cin, text); getline(cin, action); https://www.cplusplus.com/doc/tutorial/basic_io/ Scroll down to the inputs section.


I tried this with cin.getline(text, 50) as well. It still doesn't work. It works in my desktop compiler, so there's some issue with sololearn's environment. When it does work, this is the correct code. Getline takes in the whole line. cin.getline(text, 50); cin >> action;


Try using cin. It works (I don't kbow much Cpp, so pardon me if I'm wrong). Also, you can use !strcmp() instead of strcmp() == 0.


ChaoticDawg I tried cin.getline(text, 50) when testing his code. It worked a couple times then quit working. I think there was an issue with the server. Not sure about the fgets() though. I didn’t test that.


Wow. Never expected such attention to this post. Sorry for my negative attitude, it appears I have mood swings.