Computing.Net > Forums > Programming > getline in c++

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

getline in c++

Reply to Message Icon

Name: Neo22
Date: September 28, 2004 at 11:04:09 Pacific
OS: winxp home
CPU/Ram: 3.0 / 256
Comment:

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;
}



Sponsored Link
Ads by Google

Response Number 1
Name: stroke6463
Date: September 28, 2004 at 12:43:41 Pacific
Reply:

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.


0

Response Number 2
Name: BlueRaja
Date: September 28, 2004 at 13:39:57 Pacific
Reply:

Flush the input buffer.

I think it's cin.flush().

AKhalifman@hotmail.com


0

Response Number 3
Name: Don Arnett
Date: September 28, 2004 at 16:08:30 Pacific
Reply:

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.


0

Response Number 4
Name: egkenny
Date: September 28, 2004 at 17:46:04 Pacific
Reply:

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



0

Response Number 5
Name: gimmpy224
Date: September 28, 2004 at 18:52:10 Pacific
Reply:

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


0

Related Posts

See More



Response Number 6
Name: Neo22
Date: September 29, 2004 at 21:42:55 Pacific
Reply:

Thanks guys, by flusing the input buffer my program works just fine!!!!


0

Response Number 7
Name: BlueRaja
Date: September 29, 2004 at 21:49:21 Pacific
Reply:

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

AKhalifman@hotmail.com


0

Response Number 8
Name: Don Arnett
Date: September 30, 2004 at 09:14:14 Pacific
Reply:

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.


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: getline in c++

getline in C++ www.computing.net/answers/programming/getline-in-c/16332.html

ungetc in c++ www.computing.net/answers/programming/ungetc-in-c/8266.html

Reading Specific Lines in C++ www.computing.net/answers/programming/reading-specific-lines-in-c/2843.html