using namespace std;
int main(int argc, char *argv[])
{
int item_weight; // ITEM WEIGHT IN GRAMS
double item_unit_price; //ITEM UNIT PRICE ($)
int quantity_required; // QUANTITY REQUIRED
//ENTER ITEM WEIGHT IN GRAMS-----------------------------------------------------
cout << "ENTER ITEM WEIGHT IN GRAMS: "; // prompt for input item_weight
cin >> item_weight; // read item_weight from user
while(!cin)
{
cout<<"Incorrect datatype input, please enter again."<<endl;
cin.clear();
cin.sync();
cout << "ENTER ITEM WEIGHT IN GRAMS: ";
cin >> item_weight;
}
//ENTER ITEM UNIT PRICE-------------------------------------------------------
cout << "ENTER ITEM UNIT PRICE ($): "; // prompt for input item_unit_price
cin >> item_unit_price; // read item_unit_price from user
while(!cin)
{
cout<<"Incorrect datatype input, please enter again."<<endl;
cin.clear();
cin.sync();
cout << "ENTER ITEM UNIT PRICE ($): ";
cin >> item_unit_price;
}
//ENTER QUANTITY REQUIRED------------------------------------------------------
cout << "ENTER QUANTITY REQUIRED: "; // prompt for input quantity_required
cin >> quantity_required; // read quantity_required from user
while(!cin)
{
cout<<"Incorrect datatype input, please enter again."<<endl;
cin.clear();
cin.sync();
cout << "ENTER QUANTITY REQUIRED: ";
cin >> quantity_required;
}
If I enter the item_weight with the number 123a, there is abnormal behaviour , how can I fix?
And also if I enter 12.2 in item_weight also with problem.
How can I check the input?