Topic: [Q] Behaviour of cin >> string
Author: mcsweeny@freenet.calgary.ab.ca (M/K McSweeny)
Date: 1996/06/10 Raw View
Looking at the April 95 working paper, I cannot determine the expected
behaviour for code like:
string x;
// #1
cin >> x;
cout << x << endl;
// #2
cin >> x;
cout << x << endl;
// #3
cin >> x;
cout << x << endl;
Assume the input stream contains 'abc def ghi'. Should the output be:
abc
def
ghi
or
abc
abcdef
abcdefghi
That is, does 'cin >> x' reset the string prior to reading it? The
Plum-Hall libraries did reset the string, some newer environments are not.
Now assume that the words in the input stream are separated by two spaces
instead of one. Should a null word be read between each pair of spaces or
should all leading whitespace be gobbled up prior to reading each word?
Plum-Hall gobbled the whitespace, some newer environments return null
words.
Thanks.
Mark (mcsweeny@freenet.calgary.ab.ca)
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: clamage@Eng.Sun.COM (Steve Clamage)
Date: 1996/06/11 Raw View
In article 0806961009170001@10.0.2.15, mcsweeny@freenet.calgary.ab.ca (M/K McSweeny) writes:
>Looking at the April 95 working paper, I cannot determine the expected
>behaviour for code like:
...
>That is, does 'cin >> x' reset the string prior to reading it? The
>Plum-Hall libraries did reset the string, some newer environments are not.
>
>Now assume that the words in the input stream are separated by two spaces
>instead of one. Should a null word be read between each pair of spaces or
>should all leading whitespace be gobbled up prior to reading each word?
According to the latest working paper, the string extraction operator
appends characters to the string. No mention is made of resetting
the string, and indeed that would seem like peculiar behavior to me.
Initial whitespace skipping may be turned on or off. It is supposed
to be on by default. In that case, it doesn't matter how much
whitespace separates fields. All leading whitespace is skipped,
and data is read up to the next whitespace or until some other
termination condition is met.
If whitespace skipping is off, no whitespace will ever be read. Upon
encountering the first whitespace, no more input should occur by using
the string extractor. You would have to use unformatted input operations
to read or otherwise scan off the whitespace.
---
Steve Clamage, stephen.clamage@eng.sun.com
---
[ comp.std.c++ is moderated. To submit articles: Try just posting with your
newsreader. If that fails, use mailto:std-c++@ncar.ucar.edu
comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
Comments? mailto:std-c++-request@ncar.ucar.edu
]
Author: jpotter@falcon.lhup.edu (John E. Potter)
Date: 1996/06/14 Raw View
Steve Clamage (clamage@Eng.Sun.COM) wrote:
: In article 0806961009170001@10.0.2.15, mcsweeny@freenet.calgary.ab.ca
: (M/K McSweeny) writes:
: >That is, does 'cin >> x' reset the string prior to reading it?
: According to the latest working paper, the string extraction operator
: appends characters to the string. No mention is made of resetting
: the string, and indeed that would seem like peculiar behavior to me.
I am obviously missing something here. For a char c[20] and and cin
containing "Hello world", cin >> c >> c produces "world". It seems
that for a string s, cin >> s >> s would produce "Helloworld". The
latter seems *peculiar* to me. Could you clarify?
John
[ comp.std.c++ is moderated. To submit articles: try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ FAQ: http://reality.sgi.com/employees/austern_mti/std-c++/faq.html ]
[ Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu ]