- 3
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
7 Answers
+ 3
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()
+ 3
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.
+ 2
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.
+ 1
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;
+ 1
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.
+ 1
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.
0
Wow. Never expected such attention to this post. Sorry for my negative attitude, it appears I have mood swings.