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?

you can't enter a character when the program is expecting an int; if you take off the a at the end of 123a, it should work. Also, item_weight is declaired as an integer, and 12.2 is a double.

you can't enter a character when the program is expecting an int; if you take off the a at the end of 123a, it should work. Also, item_weight is declaired as an integer, and 12.2 is a double.

is there any method to make the programme can determine the input valid?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.