+ 1

Why isn't cin.get() accepted on SoloLearn's code playground?

https://code.sololearn.com/cl6h7OtF8EcI/?ref=app

7th Nov 2018, 4:38 PM
marcopl
marcopl - avatar
11 Respuestas
+ 12
Ipang I tested it, it seems that cpg_hack is just a macro for cin >> Check out these codes separately, #include <iostream> #define cpg_hack cin >> using namespace std; int main() { char var; var = cin.get(); cout << var << endl; var = cin.get(); cout << var << endl; return 0; } #include <iostream> #define cpg_hack cin >> using namespace std; int main() { char var; cin >> var; cout << var << endl; var = cin.get(); cout << var << endl; return 0; } What I have figured out is that when compiler sees the cin >> in the macro it asks for input only once and then stores. In the above codes, enter two characters each on a separate line and see the output, only the first entered character gets printed. So it seems that cin.get() still doesn't take any input in CPG That's what my opinion is after testing it :)
7th Nov 2018, 8:10 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 12
I got it, now it makes sense. When entering two values on a separate line it was actually taking both inputs but the second was newline all the time which can't be seen when printed. After typecasting and printing it, then the second output was 10 which is a newline ASCII. Now I also came to know the difference between cin.get and cin.getline. cin.getline trims the newline but cin.get don't.
7th Nov 2018, 8:54 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 12
Thank you too, it also helped me learning a new thing.
7th Nov 2018, 9:05 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 6
Oh okay type_guy, well, I think it might work outside Code Playground on an IDE (never tested though), but in case you're curious, you can add: #define cpg_hack cin >> Before main function, this is a weird sort of *workaround* for Code Playground, honestly I couldn't quite explain why or how, a dear friend taught me this trick before. But still, don't forget to cout << var; otherwise it's not outputting anything : )
7th Nov 2018, 7:52 PM
Ipang
+ 5
nAutAxH AhmAd actually it works, but it doesn't obviously seem to be, cin.get works for multiple reads, the output you saw was the first character and a '\n' (new line character) because the 1st character is entered followed with new line (Enter). If you put the characters in the same line it works, given the two characters are printable. You can cout << (int) var; to verify this and see that the second character is a new line (ASCII 10). Also try with a loop to get multiple reads, here it reads 10 times; #include <iostream> #define cpg_hack cin >> using namespace std; int main() { char var; for(int i {0}; i < 10; i++) { var = cin.get(); cout << var; } return 0; }
7th Nov 2018, 8:43 PM
Ipang
+ 5
Nice discussion guys, also learned something from this. Thanks type_guy and nAutAxH AhmAd : ) Bed time now ...
7th Nov 2018, 9:04 PM
Ipang
+ 4
type_guy can you explain why the specific need to use cin.get()? I'm sure you know a simple cin >> var will do the job, so I'm assuming you must have a certain reason.
7th Nov 2018, 7:25 PM
Ipang
+ 3
Can you attach the code to the Description section so people can see and help you with it?
7th Nov 2018, 6:58 PM
Ipang
+ 2
Thanks everybody!! very interesting 😄
7th Nov 2018, 10:28 PM
marcopl
marcopl - avatar
+ 1
Ipang Sure!
7th Nov 2018, 7:02 PM
marcopl
marcopl - avatar
+ 1
Ipang Yeah I know I could have used the >> operator but I was just trying out a curiosity of mine 😅
7th Nov 2018, 7:37 PM
marcopl
marcopl - avatar