Yannick De Koninck <Ya***************@ugent.be> wrote:
Hello,
I have the following construction:
#define STRINGLENGTH 30
..
char* Name;
...
Name = new char[STRINGLENGTH];
cin>>Name;
cout<<Name;
But the problem is, when I enter a string that contains spaces (eg: "it's
raining"), only the first part of this string is printed (so for the example
"it's" would be printed). Is there a way to avoid this and to read and pront
the entire string?
The problem is that the >> operator by default stops at a whitespace, so
it only reads up until the space after "it's". I believe there is a
cin.getline() function that you can use. However, I prefer to use
std::string, which automagically takes care of the length of the string.
For example,
#include <iostream>
#include <string>
int main()
{
std::string name;
std::getline(std::cin, name); // note that this is different from
// the member function cin.getline()
// that I mentioned above
std::cout << name << '\n';
}
--
Marcus Kwok
Replace 'invalid' with 'net' to reply