-2

I want to read a string, the string can contain spaces.

I tried:

char* str;
cin >> str; // but I have to allocate a memory by making new

so I think about:

string str;
cin >> str;

but it reads until the first space.

can someone know how can I get it? maybe getline?

I can use only the includes of iostream and string.

Alon Shmiel
  • 6,753
  • 23
  • 90
  • 138

1 Answers1

1

Your suspicion is correct.

string str;
std::getline( cin, str );

This will read until a newline is detected, not just any whitespace character.

Drew Dormann
  • 59,987
  • 13
  • 123
  • 180