Reputation: 4281
The following code works fine and also check if user has input right amount of items, but it fails when the input has a trailing empty line.
string item1, item2, item3;
while(cin.good) {
//this allows me to both check if user input enough items
//EDIT: and if items are of right type so I can cerr
if (cin >> item1 && cin>> item2 && cin>> item3) {
doStuff(item1,item2,item3);
}else {
cerr<<"bad input! Not enough input items or wrong type"<<endl;
}
}
Can I change cin.good to something else to solve the situation when there is a trailing empty line? I'll accept other solutions too.
EDIT: I realize I can't use while(cin >> item1) because then I can't cerr the error message if item1 is wrong type.
Upvotes: 0
Views: 173
Reputation: 9850
I think that:
while(cin >> item1) {
//this allows me to check if user input enough items
if (cin >> item2 && cin >> item3) {
doStuff(item1,item2,item3);
} else {
cerr<<"bad input! Not enough input items"<<endl;
}
}
will do what you want.
Upvotes: 2