Problem with cin >> and getline

Below is a simple function i created for my program but i am running into a problem. When I build the program and this function gets called, it totally skips the first initial input (last name) but it does print out the cout. I also tried using cin>> to get the input, but there are some inputs that i need to include white spaces which is the reason why im using getline. but if i use cin>> for one input and getline for another, when a space is entered, it stores that information into the next variable instead of storing it all into one variable. Any ways to fix this? I read that there are problems when you mix up >> operator with getline. Please help

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
void AddCust()
{
	cout << string(50, '\n'); //clear screen with blank spaces
	string lastName;
	string firstName;
	string StreetAddress;
	string city;
	string state;
	int zipcode;
	int phoneNumber;
	
	cout<<"Please fill out the appropriate fields:"<<endl;

	cout<<"Last Name: ";
	getline(cin,lastName);

	cout<<"First Name: ";
	getline(cin,firstName);
		
	cout<<"Street Address: ";
	getline(cin,StreetAddress);
		
	cout<<"City: ";
	getline(cin,city);
	
	cout<<"State: ";
	getline(cin,state);
	
	cout<<"Zipcode: ";
	cin>>zipcode;
	
	cout<<"Phone Number: ";
	cin>>phoneNumber;
	
}
Last edited on
Indeed, its >>'s fault. The thing is that cin>> reads one word and stops immediately, thus leaving the '\n' from pressing Enter in the stream. This '\n' is then found by getline. It would be good to have a cin.ignore() after every cin>> so that there is never any rubbish in the stream. Only adding cin.ignore() on line 13 (that is, before line 15) should work fine too.
Thank you i really do appreciate your help. That fixed the problem.
Topic archived. No new replies allowed.