remove the "\n"s -- that is only for cout. Not allowed to have literals (text in quotes) on the cin line.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
remove the "\n"s -- that is only for cout. Not allowed to have literals (text in quotes) on the cin line.
There is some syntax of cin that allowes us to ignore the \ns.. Isn't it?
thekashyap
Practically a Posting Shark
811 posts since Feb 2007
Reputation Points: 254
Solved Threads: 75
>There is some syntax of cin that allowes us to ignore the \ns.. Isn't it?
You're probably thinking of the ws manipulator, but it discards all whitespace. If you want more control, you have to write your own manipulator:
class scan {
public:
scan ( const char *init ): fmt ( init ) {}
friend istream& operator>> ( istream& in, const scan& s )
{
while ( *s.fmt != '\0' && in && in.peek() == *s.fmt ) {
in.get();
++s.fmt;
}
if ( *s.fmt != '\0' )
in.setstate ( ios::failbit );
return in;
}
private:
mutable const char *fmt;
};
And of course you can write custom manipulators so that they're used in exactly the same way as standard manipulators. This is only slightly more awkward than what the OP had before:
cin>> first >> scan ( "\n" ) >> middle >> scan ( "\n" ) >> last;
Of course, that's effectively a no-op because most formatted input discards leading whitespace by default. That's why removing those strings altogether still works as expected.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
jbennet
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601