what does this code do?

Hello

I am new to C++ and i am reading a tutorial and in the tutorial there is code about: cin.getline. And this the part that i don't understand:

1
2
3
4
5
6
7
8
9
    	cout << "First Name: ";
    	cin >> ws;
    	cin.getline(FirstName, 20);
    	cout << "Last Name: ";
    	cin >> ws;
    	cin.getline(LastName, 20);
    	cout << "Address: ";
    	cin >> ws;
    	cin.getline(Address, 40);


What i don't undersand is this :

cin >> ws;

What does this part of the code do?

I searched for it in this site and i found this:

http://www.cplusplus.com/reference/iostream/manipulators/ws/

but still can't understand what its job here in this code.

Can anyone break it down for me?

thanks.
getline leaves whitespaces where they are if they don't fit in the line
cin >> ws will discard those
It's a manipulator for an input stream. This means that by using that before calling getline for your inputting, you tell that (in this case) all whitespace should be ignored up to the point where the first non-whitespace character is encountered.

Inputting this:
1
2
        LOLOL LOL!
// Note the whitespaces at the start 

Would thus return:
1
2
LOLOL LOL!
// Note that the whitespace in the middle is still there 
Last edited on
Thanks Guys

i get it now thanks to your .
Last edited on
Topic archived. No new replies allowed.