getline();

I'm not sure what is wrong with my getline(). The program doesn't cin for address or notes. any input would be helpful, Thanx

#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

struct Contact
{
string firstName;
string lastName;
string cellPhone;
string housePhone;
string otherPhone;
string address;
string emailAddress;
string notes;
};

void DisplayContact(Contact contact);
Contact AddContact();

int main()
{
DisplayContact(AddContact());
}

Contact AddContact()
{
string address;
string note;
string buffer;

Contact tempContact;

cout << setw(25) << "--New Contact--\n\n";

cout << "First Name - ";
cin >> tempContact.firstName;

cout << "Last Name - ";
cin >> tempContact.lastName;

cout << "Cell Phone - ";
cin >> tempContact.cellPhone;

cout << "House Phone - ";
cin >> tempContact.housePhone;

cout << "Other Phone - ";
cin >> tempContact.otherPhone;

cout << "Address - ";
getline (cin, tempContact.address);

cout << "E-mail - ";
cin >> tempContact.emailAddress;

cout << "Notes - ";
getline (cin, tempContact.notes);

return (tempContact);
}

void DisplayContact(Contact contact)
{
cout << contact.lastName << ", " << contact.firstName << endl;
cout << "Cell Phone - " << contact.cellPhone << endl;
cout << "House Phone - " << contact.housePhone << endl;
cout << "Other Phone - " << contact.otherPhone << endl;
cout << "Address - " << contact.address << endl;
cout << "E-mail - " << contact.emailAddress << endl;
cout << "Notes - " << contact.notes << endl;
}

Last edited on
It probably has to do with your mixing of cin>> and getline(). When you use cin>>, it probably leaves a '\n' in the buffer so the next getline() returns immediately because it hits that newline.
oic... is there anyway to fix this?
you can tell it to ignore the '\n'
try: http://www.cplusplus.com/forum/beginner/11366/
Last edited on
Topic archived. No new replies allowed.