1

I am trying to have a string accept a character space. However when you type something like:

Mike Smith

the output is only

Mike

Is there an easy way to fix this? Can I do something like:

string playerName (50) <-- Where (50) is the length of the string

My Code below:

int main() {
string playerName;
cout << "Enter Player Name: ";
    cin >> playerName;

cout << playerName << endl;
}
  • Possible duplicate of [How to read a complete line from the user using cin?](http://stackoverflow.com/questions/5455802/how-to-read-a-complete-line-from-the-user-using-cin) – Enamul Hassan Nov 23 '15 at 00:41

2 Answers2

2

Use the following to read a whole line of input:

getline(cin, playerName);
Emil Laine
  • 41,598
  • 9
  • 101
  • 157
1

Use:

getline(cin, playerName);

But remember, you have to use the following line before getting any other data type after using the above line.

cin.ignore();
Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56