www.digitalmars.com         C & C++   DMDScript  

c++ - SOS----------About cin

reply Mike <desksuse googlemail.com> writes:
It's from the Accelerated C++
I am puzzled about the "cin",it's said that "it discards any whitespace
characters in the standard input stream, then reads from the standard input
into variable",but why it outputs "Samuel Beckett",not "Samuel" and pause for
next input?

Thank you! *^_^*

______________________________________________________________________
"What does the following program do if, when it asks you for input, you type
two names (for example, Samuel Beckett)? Predict the behavior before running
the program, then try it.

#include <iostream>
#include <string>

int main()
{
    std::cout << "What is your name? ";
    std::string name;
    std::cin >> name;
    std::cout << "Hello, " << name
              << std::endl << "And what is yours? ";
    std::cin >> name;
    std::cout << "Hello, " << name
              << "; nice to meet you too!" << std::endl;
    return 0;
}
"
Apr 27 2007
parent Bertel Brander <bertel post4.tele.dk> writes:
Mike skrev:
 It's from the Accelerated C++
 I am puzzled about the "cin",it's said that "it discards any whitespace
 characters in the standard input stream, then reads from the standard input
 into variable",but why it outputs "Samuel Beckett",not "Samuel" and pause for
 next input?
I'm not sure what your question are, but std::cin >> xx; will stop at the first space, if xx is a std::string. What is left after the space will be stored untill the >> operator is used again. -- Just another homepage: http://damb.dk But it's mine - Bertel
Apr 27 2007