I've written this piece of code that is meant to receive values using an argument istr for products and it's been working fine, but I realized you can't just leave a field blank. One of my fields deals with a product's units and some products do not have units, which in that case the user would simply press enter to proceed to the next received value, but the problem is that it just keeps going to a newline until a proper value is received. What can I do to accept blank values as well? I'm just focused on the unit field, but I provided the entire function if anyone needed to see it. Here's my code:
std::istream& AmaProduct::read(std::istream& istr) { //receives values using istr argument
char skuField[10];
char nameField[100];
double priceField;
char taxField;
int qtyField;
char unitField[11];
int qtyNeededField;
if (!istr.fail()) {
err_.clear();
}
cout << "Sku: ";
istr >> skuField;
sku(skuField);
cout << "Name: ";
istr >> nameField;
name(nameField);
cout << "Unit: ";
istr >> unitField;
unit(unitField);
err_.clear();
cout << "Taxed? (y/n): ";
istr >> taxField;
if (taxField == 'Y' || taxField == 'y' || taxField == 'N' || taxField == 'n') {
taxed(taxField == 'Y' || taxField == 'y');
istr.ignore(500, '\n');
}
else {
err_.message("Only (Y)es or (N)o are acceptable");
istr.setstate(ios::failbit);
}
if (err_.isClear()) {
cout << "Price: ";
istr >> priceField;
price(priceField);
}
if (istr.fail() && err_.isClear()) {
err_.message("Invalid Price Entry");
}
if (err_.isClear()) {
cout << "Quantity On hand: ";
istr >> qtyField;
quantity(qtyField);
}
if (istr.fail() && err_.isClear()) {
err_.message("Invalid Quantity Entry");
}
if (err_.isClear()) {
cout << "Quantity Needed: ";
istr >> qtyNeededField;
qtyNeeded(qtyNeededField);
}
if (istr.fail() && err_.isClear()) {
err_.message("Invalid Quantity Needed Entry");
}
return istr;
}
}