Jeff said:
Rolf Magnus said:
What do you mean by "I can't"? Show a short piece of code that
demonstrates
your problem.
Here is the code:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
using std::string;
int main()
{
string st;
cin >> st;
cout << '\n' << st << endl;
return 0;
}
The string behaves like char[].
No, it behaves like a std::string.
When a space is introduced and charactrer follow, the space and following
characters do not print.
This is not the string class doing this, but the
operator << used with 'cout'.
In VC++ 5.0 everything printed.
With that exact same code, I strongly doubt that (or if
it did, that compiler was not complying with the standard
in this respect.
All that having been said, there is indeed a function
in the standard library which will do what you want:
'std::getline()' (declared by <string>). It's a 'free'
(i.e. nonmember) function. Use it like this:
#include <iostream>
#include <string>
int main()
{
std::string st;
std::cout << "Type a string: ";
std::getline(std::cin, st);
std::cout << "You typed: " << st << '\n';
return 0;
}
-Mike