what does this code do?

Jan 13, 2011 at 4:07pm
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.
Jan 13, 2011 at 4:11pm
getline leaves whitespaces where they are if they don't fit in the line
cin >> ws will discard those
Jan 13, 2011 at 4:11pm
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 Jan 13, 2011 at 4:12pm
Jan 13, 2011 at 4:36pm
Thanks Guys

i get it now thanks to your .
Last edited on Jan 13, 2011 at 4:37pm
Topic archived. No new replies allowed.