www.digitalmars.com Home | Search | C & C++ | D | DMDScript | News Groups | index | prev | next
Archives

D Programming
D
D.gnu
digitalmars.D
digitalmars.D.bugs
digitalmars.D.dtl
digitalmars.D.dwt
digitalmars.D.announce
digitalmars.D.learn
digitalmars.D.debugger

C/C++ Programming
c++
c++.announce
c++.atl
c++.beta
c++.chat
c++.command-line
c++.dos
c++.dos.16-bits
c++.dos.32-bits
c++.idde
c++.mfc
c++.rtl
c++.stl
c++.stl.hp
c++.stl.port
c++.stl.sgi
c++.stlsoft
c++.windows
c++.windows.16-bits
c++.windows.32-bits
c++.wxwindows

digitalmars.empire
digitalmars.DMDScript
electronics



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

↑ ↓ ← 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
↑ ↓ → 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