How can i read in a specific format using cin?
Example:-for reading a complex number, I would like the user to enter it as usual:x+yi,
so i want something like this:
cin>>x>>”+”>>y>>”i”;
But this is giving an error.What is the right way?Help greatly appreciated.
How can i read in a specific format using cin? Example:-for reading a complex
Share
A very simple solution:
In “real life” code you build/use a
class complex, and overload the operator>>.I try it in Ideone: http://ideone.com/ZhSprF
stdin:
3 + 4i–> stdout:Complex: 3+4istdin:
1E4L1e3g–> stdout:Error: x=10000, plus=L, y=1000, img=gstdin:
a+3i–> stdout:Error: x=0, plus=, y=0, img=stdin:
1e3+93E-2i–> stdout:Complex: 1000+0.93i