Problem displaying getline function properly

I'm creating a contact card. Whenever I'm displaying the output, it's doing several things that it shouldn't be.

1. After the name, it skips a line
2. After that, it has the address, city and state all on one line.
3. Then the zip code is on the next line
4. The phone number never appears.

I'm not sure why it's doing this, but I think it has something to do with the getline function because I've never used it before, but I can't find anything online about why it's doing this.

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <string>
using namespace std;

//function prototypes
string firstName = " ";
string lastName	= " ";
string firstInitial = " ";
string lastInitial = " ";
string streetAddress = " ";
string city = " ";
string state = " ";
string zip = " ";
string phone = " ";

int main ()
{

	cout <<"Enter the customer's first name: ";
	cin >> firstName;
	cout <<"Enter the customer's last name: ";
	cin >> lastName;
	cout <<"Enter the customer's street address: ";
	getline (cin, streetAddress, '\n');
	cout <<"Enter the customer's city: ";
	getline (cin, city);
	cout <<"Enter the customer's state: ";
	getline (cin, state);
	cout <<"Enter the customer's zip code: ";
	getline (cin, zip);
	cout <<"Enter the customer's phone number: ";
	getline (cin, phone);

	firstInitial = firstName.substr (0,1);
	lastInitial = lastName.substr (0,1);


	//display output items
	cout << "This is how " << firstName << " " << lastName << "'s contact card will appear.";
	cout <<"\n";
	cout <<"******************************************************************"<< endl;
	cout <<"**********         " << firstInitial << lastInitial << "         **********"<<endl;
	cout <<"******************************************************************"<< endl;
	cout <<"********** " << firstName << " " << lastName <<endl;
	cout <<"********** " << streetAddress << endl;
	cout <<"********** " << city << ", "<< state << " " << zip << endl;
	cout <<"********** " << phone << endl;
	cout <<"******************************************************************"<< endl;

return 0;
} //end of main function 
After each cin >> .. there should be a cin.ignore(80,'\n');
When I put it only after the cin>> it didn't work, but if I put the cin.ignore after each input (even the getline), it displayed the output correctly but it made the user double click the enter key to get the next input prompt to display. Is there a way to do this without the need for two clicks of the enter key?
Changing this:
1
2
3
4
        cout <<"Enter the customer's first name: ";
	cin >> firstName;
	cout <<"Enter the customer's last name: ";
	cin >> lastName;


To:
1
2
3
4
5
6
        cout <<"Enter the customer's first name: ";
	cin >> firstName;
        cin.ignore(80, '\n');
	cout <<"Enter the customer's last name: ";
	cin >> lastName;
        cin.ignore(80, '\n');


Should work. That should also be the only thing that needs changed. If you're just putting it before your getline, place it before your first one, and your first one only.

Edit: You could eliminate the need for the cin.ignore altogether by just using getline in place of all of your cin's (What if the person's first name is Bobbi Jo?).
Last edited on
Topic archived. No new replies allowed.