Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I'm trying to make a program that asks for three different employee names and salaries and then outputs the average of all three salaries, my problem is that the first getline(lastname, firstname) works fine but for the second one it skips to the salary question for some reason. Can somebody help me with this? Im just starting to use objects and classes in c++ so Im not very experienced!!
This is my code.// Compiler directives
#include <iostream>
#include "ccc_empl.h"
#include <string>
using namespace std;// main function
int main ()string wholeName2;
float sal2;cout << "Enter last and first name ";
getline(cin, wholeName2, '\n');
cout << endl;
cout << "Enter salary ";
cin >> sal2;
cout << endl;Employee emp2(wholeName2, sal2);
double newSal2 = emp2.get_salary();
emp2.set_salary(newSal2);// Second employee
string wholeName3;
float sal3;cout << "Enter last and first name ";
getline(cin, wholeName3, '\n');
cout << endl;
cout << "Enter salary ";
cin >> sal3;
cout << endl;Employee emp3(wholeName3, sal3);
double newSal3 = emp3.get_salary();
emp3.set_salary(newSal3);// Third employee
string wholeName4;
float sal4;cout << "Enter last and first name ";
getline(cin, wholeName4, '\n');
cout << endl;
cout << "Enter salary ";
cin >> sal4;
cout << endl;Employee emp4(wholeName4, sal4);
double newSal4 = emp4.get_salary();
emp4.set_salary(newSal4);float average = newSal4 + newSal3 + newSal2 / 3.0;
cout << "The average salary for all the employees is " << average <<
" ." << endl;
system ("pause");
return 0;
}

I'm a little rusty with C++ but I'm pretty sure you have to use the cin.ignore command. Use it after the cin of the numbers.
Example
cout << "Enter last and first name ";
getline(cin, wholeName2, '\n');
cout << endl;
cout << "Enter salary ";
cin >> sal2;
cin.ignore(100, '\n');
cout << endl;Do the same for the rest. What that does is skips up to 100 characters stopping only after it encounters '\n'.
If that doesn't work put it other places I can't remember exactly how it goes but it should be something like that. Keep me posted.

As the two above posts are hinting at but not explaining, the problem is that your 'cin' for salary statements are leaving a newline on the input stream. So when the next 'getline' comes along, it reads until it finds a newline, which happens to be the next character. So the getline is done and the name fields(wholeName3 & wholeName4) are empty and the program moves on to the next cin without waiting for input.
The best solution is BlueRaja's suggestion to flush the input buffer with cin.flush().
This removes the newline from the buffer, so that getline has to stop and wait for you to input.

1. Put the following after each cin statement: fflush(stdin);
Example:
cin >> sal2;
fflush(stdin);2. If you are using Visual C++ 6.0 then a fix is also needed in one of the system header files.
In system header file "string" make the follwing change:
else if (_Tr::eq((_E)_C, _D))
{_Chg = true;
_I.rdbuf()->snextc(); // <-- delete this line
_I.rdbuf()->sbumpc(); // <-- add this line
break; }This error was corrected in Visual C++ .NET.
FIX: getline Template Function Reads Extra Character
http://support.microsoft.com/default.aspx?scid=kb;EN-US;q240015

Yea, i had the same problem and that fixed mine.
So if the cin.flush thing didnt work then who knows :) (Don will, he knows everything ;);).... p.s. Don, thanks for the books :-D)GIMPS

So basically, it took four extra replies for everyone to say "You were right, BlueRaja."?
:|AKhalifman@hotmail.com

BR - I gave you credit for having the best solution!! :)
I wouldn't have commented, except that I think that it's helpful to explain why, not just how. Neo (and others) will learn more if you explain what the problem is and why the solution fixes it. Just my opinion tho.

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |