Is it possible to read [1,2,3] as an input in c++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Is it possible to read [1,2,3] as an input in c++?

I am trying to read [1,2 3] as an input in c++ but not able read it. I am printing the size of the vector after input bit it is showing 0 only. How to read this type of input or any arbitrary input format in c++? https://code.sololearn.com/cmcNko40dS2g/?ref=app

12th Aug 2021, 4:44 AM
MahirShah
2 Answers
+ 4
In the very first iteration, you try to read '[' as an integer as part of the condition, before the if statement becomes effective. Since '[' can not be interpreted as an integer, the internal failbit of the stream is set, which in turn causes the loop to exit immediately. As a consequence, the vector will be empty. Here is just one idea to solve the issue by ignoring the unwanted characters: https://code.sololearn.com/c5GBEJ42vT6q/?ref=app
12th Aug 2021, 8:57 AM
Shadow
Shadow - avatar
12th Aug 2021, 11:53 AM
Ipang