- 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

13th Apr 2021, 5:45 PM
OverdrivedProgrammer
7 odpowiedzi
+ 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()
13th Apr 2021, 9:14 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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.
13th Apr 2021, 9:25 PM
Jerry Hobby
Jerry Hobby - avatar
+ 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.
13th Apr 2021, 8:29 PM
ChaoticDawg
ChaoticDawg - avatar
+ 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;
13th Apr 2021, 6:30 PM
Jerry Hobby
Jerry Hobby - avatar
+ 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.
13th Apr 2021, 8:01 PM
Calvin Thomas
Calvin Thomas - avatar
+ 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.
13th Apr 2021, 9:05 PM
Jerry Hobby
Jerry Hobby - avatar
0
Wow. Never expected such attention to this post. Sorry for my negative attitude, it appears I have mood swings.
26th Apr 2021, 6:07 PM
OverdrivedProgrammer