Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Retrieving user entered data with getline.

Status
Not open for further replies.

mgbeye

Programmer
May 30, 2001
47
US
Ok here it is. I have a simple program set up to prompt for the Employee name, id and pay. I have set up employee as a structure with three parts. My problem is with the get line that reads in the name. It works fine for the first Employee, but stores garbage in the variables and exits the program in the second two. Does anyone know how to stop it???

cout << &quot;EMPLOYEE 1&quot; << endl << &quot;---------&quot; << endl;
cout << &quot;Enter Employee's name then hit Return TWICE: &quot;;
getline(cin, Employee1.Name, '\n');
cout << &quot;Enter the Employee's ID: &quot;;
cin >> Employee1.ID;
cout << endl;
cout << &quot;Enter the Employee's pay (in dollars): &quot;;
cin >> Employee1.Pay;
cout << endl << endl;

cout << &quot;EMPLOYEE 2&quot; << endl << &quot;---------&quot; << endl;
cout << &quot;Enter Employee's name then hit Return TWICE: &quot;;
getline(cin, Employee2.Name);
cout << &quot;Enter the Employee's ID: &quot;;
cin >> Employee2.ID;
cout << endl << endl;
cout << &quot;Enter the Employee's pay (in dollars): &quot;;
cin >> Employee2.Pay;
cout << endl << endl;

cout << &quot;EMPLOYEE 3&quot; << endl << &quot;---------&quot; << endl;
cout << &quot;Enter Employee's name then hit Return TWICE: &quot;;
getline(cin, Employee3.Name, '\n');
cout << &quot;Enter the Employee's ID: &quot;;
cin >> Employee3.ID;
cout << endl << endl;
cout << &quot;Enter the Employee's pay (in dollars): &quot;;
cin >> Employee3.Pay;
cout << endl << endl;

HELP!!!
 
if you just want to delimit it by spaces cin>> will work fine. Also, try cin.getline(str,length,' ') where ' ' is the character that will stop the input.

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top