Need Help Understanding How To Input A FullName

I was beginning to make a text based game when I couldn't even make a user input a full name. It has really got me confused, unfortunitly and sadly I cannot get it to work.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

using namespace std;


int main()

{
cout << "Your name: ";

string Name;

cin >> Name;

cout << "Your name is " << Name << endl;

return 0;
}


I just want a user to be able to enter any kind of name. But as i was testing the program, I noticed that when you type a long name like Henry John Finch, the output says Your name is Henry. The program ignores everything after the space and have no idea on how to fix this problem.
std::cin terminates when it encounters a space. Use std::getline which will store characters until it encounters <enter>.

e.g.
std::string Name;
std::getline(std::cin, Name);
std:: cout << "Your name is " << Name << std::endl;


http://www.cplusplus.com/reference/string/string/getline/?kw=getline
Last edited on
Thanks for all the help!
Topic archived. No new replies allowed.