C++ “cin” only reads the first word [duplicate]

Using >> on a stream reads one word at a time. To read a whole line into a char array:

cin.getline(str, sizeof str);

Of course, once you’ve learnt how to implement a string, you should use std::string and read it as

getline(cin, str);

It would also be a very good idea to get a compiler from this century; yours is over 15 years old, and C++ has changed significantly since then. Visual Studio Express is a good choice if you want a free compiler for Windows; other compilers are available.

Leave a Comment