I'm not entirely sure what the problem is, but something doesn't good in your code when you're mixing the getline() statements and in >> operators...
You see, when you use istream >> a_variable , it always leaves a trailing newline in the input stream. Then when you try to use getline(), you end up with a newline character instead of the input you expected to get.
The best solution would be to simply use all getline() statements instead of mixing them, however if you don't want to, you can simply clear the input buffer like this:
cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
This works, its essentially the same you had though. I added code to flush the keyboard buffer after each integer input. That may have been your problem.
#include <string>
#include <vector>
#include <iostream>
using namespace std;
class dt
{
public:
int month,day,year;
dt() {month = day = year = 0;}
};
class EmailHeader
{
public:
string to;
string from;
string subject;
dt Date;
};
//overloaded input operator
istream& operator>>(istream& is, vector<EmailHeader>& ehVec)
{
EmailHeader headerInput;
cout << "Enter an email Header: Ctrl-Z to quit" << endl;
while(getline(is,headerInput.to))
{
getline(is, headerInput.from);
getline(is, headerInput.subject); //crash occurs here on second iteration after the
cout << "month: "; // user hits the return key, so input below is skipped
is >> headerInput.Date.month;
is.ignore();
cout << "day: ";
is >> headerInput.Date.day;
is.ignore();
cout << "Year: ";
is >> headerInput.Date.year;
is.ignore();
ehVec.push_back(headerInput);
cout << "Enter another email Header: Ctrl-Z to quit" << endl;
}
return is;
}
int main()
{
vector<EmailHeader> emVec;
cin >> emVec;
//other code
return 0;
}
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
The getline() allows me to input a string with spaces where as the >> cuts it off before the first space character.
Well, yes I realized that. But many newbies don't realize the hidden gotchas of mixing functions which grab entire lines, such as fgets() and getline() with functions which simply scan for what they need, like cin >> and scanf().
cin >> works fine on its own, because although it leaves a trailing newline in the input buffer, it removes it when it reads next. So you can imagine this works perfectly until you use a function which reads until the first newline. Then you have problems.
The best thing to do is to only use cin >> when you know what you're doing (even though tons of online tutorials use it), so for most newbies it would in fact be much safer practice to only use getline() for grabbing their input.Do you know of a better method of reading in the strings with spaces and ints? The best method is to read in everything with getline(), and then parse out what you need from that. Although it sounds more complex, it's quite safe. And it's less complex if you pop that result into a string stream, which you can then safely use the >> operator without worrying about newlines.
John A
Vampirical Lurker
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
I used VC++ 2005 Express to test your code.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343