hi...i'm trying to improve a simple program that i made...
this is a cash receipt program...

to avoid the program form crashing,i insert a code of cin.fail()
which will determine wether the data type the user entered is correct or not...
if the data type is wrong that the program will tell the user that the data type that she/he entered is wrong..
and then will allow the user to enter the correct data type again..

this program goes well when determine the data type,but the problem is that it is not even give me to enter back the correct data type when i was wrong..

for example,if i enter "dghttsg" when this program asking me for the id of the product which have the data type of int,it will alert me that i were wrong but i'm not be able to enter the value back even if i already put cin>>product.id on my code....


below is the part of the program :


if (cin.fail()){
      do{

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product ID Again : ";
           cin >> product[i].id;//whats wrong here??
            
          }while(cin.fail());
}

cout<<endl;

      cout << "Enter the Product Name : ";
      cin >> product[i].name;


      cout << "Enter the Price For Single Item : ";
      cin >> product[i].price;

      cout << "Enter The Quantity : ";
      cin >> product[i].quantity;

      cout<<endl;

      cout << "Would You like to enter another product? (Y/N) ";
      cin >> answer;


      i++;

      cout<<endl;
}
      }while(answer == 'y' || answer == 'Y');

Thanks for your time.. :)

I can't try to correct your code since you didn't post the full code or a part that would be enough to make it understandable.

So, check this code snippet and apply it to you code.

This part :

if (cin.fail()){
      do{
 
           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product ID Again : ";
           cin >> product[i].id;//whats wrong here??
 
          }while(cin.fail());
}

You need to clear the stream if it fails, so :

if (cin.fail()){   
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product ID Again : ";
           cin >> product[i].id;//whats wrong here??
 
          }while(cin.fail());
}

hm...
why the code that you gave me doesnt work??
when i entered the wrong input,it continues asking me about the next information..

what i understand form the code that you gave me is that it will ask the user back about the id if the data type is wrong right??
but why it is not working??
this is the full code that i've modified.. :

#include <iostream>
#include <iomanip>
using namespace std;

int main () {

//A class named record to organize the data more easily
struct record{
   string id;
   string name;
   double price;
   int quantity;
   double sale;
   };

  record product[100];
  int id[100];


      int i=0;
      char answer;

      cout<<"--------------------------------------------------------------------------------";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"--------------------------------------------------------------------------------";
      cout<<endl;



do{

        cout<<"Enter the Product ID ?:";
        cin>>product[i].id;

//why this is not working correctly???
if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product ID Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}

      cout << "Enter the Product Name : ";
      cin >> product[i].name;


      cout << "Enter the Price For Single Item : ";
      cin >> product[i].price;

      cout << "Enter The Quantity : ";
      cin >> product[i].quantity;

      cout<<endl;

      cout << "Would You like to enter another product? (Y/N) ";
      cin >> answer;


      i++;

      cout<<endl;

      }while(answer == 'y' || answer == 'Y');


cout<<"--------------------------------------------------------------------------------";
cout<<"ID |"<<setw(13)<<"ITEM |"<<setw(12)<<"PRICE |"<<setw(10)<<"QTY |"<<setw(10)<<"SALE"<<endl;
cout<<"--------------------------------------------------------------------------------";

 int index;
for(index=0; index<i; index++){

product[index].sale=(product[index].price)*(product[index].quantity);

// Display all the info about that product
cout <<product[index].id<<setw(10)<<product[index].name<<setw(10)<<"$"<<fixed<<setprecision(2)<<product[index].price<<setw(10)
<<product[index].quantity<<setw(10)<<product[index].sale;

cout<<endl;
}

cout<<endl;

int total = 0;
for(int counter=0; counter < i; counter++)
{
total += product[counter].sale;
}

cout<<"TOTAL SALE IS :"<<total;
cout<<endl;

double payment;

cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
cout<<endl;

while (payment<total){
cout<<"The Amount Of Payment Is Insufficient !"<<endl;
cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
}

cout<<endl;
cout<<"------------------------"<<endl;
cout<<"TOTAL SALE      :$"<<total<<endl;
cout<<"RECEIVE PAYMENT :$"<<payment<<endl;
cout<<"BALANCE         :$"<<payment-total<<endl;
cout<<"------------------------";
cin.get();
}

help please..
thank you.... :)

it is only because you declare youre id as a string, Which is characters or numbers are valid.
The id must be in interger right?!

int id;

owh..i'm sory..
i forgot that i've changed its data type before this...
now this code is working :)

but,there is another problem now...
after i wrote these code on my program,the informations that need to be displayed at the end of the program went wrong..

The id,quantity,price,and the sale appeared to be wrong....
why is this happen???

once again this is the code :

#include <iostream>
#include <iomanip>
using namespace std;

int main () {

//A class named record to organize the data more easily
struct record{
   int id;
   string name;
   double price;
   int quantity;
   double sale;
   };

  record product[100];
  int id[100];


      int i=0;
      char answer;

      cout<<"--------------------------------------------------------------------------------";
      cout<<"\t\t\t\tCASH RECEIPT PROGRAM"<<endl;
      cout<<"--------------------------------------------------------------------------------";
      cout<<endl;



do{

        cout<<"Enter the Product ID ?:";
        cin>>product[i].id;

//why this is not working correctly???
if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product ID Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}

      cout << "Enter the Product Name : ";
      cin >> product[i].name;

if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Product Name Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}


      cout << "Enter the Price For Single Item : ";
      cin >> product[i].price;


if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Price For Single Item Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}


      cout << "Enter The Quantity : ";
      cin >> product[i].quantity;


if (cin.fail()){
      do{
           cin.clear() ; //clear the stream
           while(cin.get() != '\n'); //clear out the junk

           cout<<"Wrong Data Type of Input!";
           cout << "Please Enter the Quantity Again : ";
           cin >> product[i].id;//whats wrong here??

          }while(cin.fail());
}




      cout<<endl;

      cout << "Would You like to enter another product? (Y/N) ";
      cin >> answer;


      i++;

      cout<<endl;

      }while(answer == 'y' || answer == 'Y');


cout<<"--------------------------------------------------------------------------------";
cout<<"ID |"<<setw(13)<<"ITEM |"<<setw(12)<<"PRICE |"<<setw(10)<<"QTY |"<<setw(10)<<"SALE"<<endl;
cout<<"--------------------------------------------------------------------------------";

 int index;
for(index=0; index<i; index++){

product[index].sale=(product[index].price)*(product[index].quantity);

// Display all the info about that product
cout <<product[index].id<<setw(10)<<product[index].name<<setw(10)<<"$"<<fixed<<setprecision(2)<<product[index].price<<setw(10)
<<product[index].quantity<<setw(10)<<product[index].sale;

cout<<endl;
}

cout<<endl;

int total = 0;
for(int counter=0; counter < i; counter++)
{
total += product[counter].sale;
}

cout<<"TOTAL SALE IS :"<<total;
cout<<endl;

double payment;

cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
cout<<endl;

while (payment<total){
cout<<"The Amount Of Payment Is Insufficient !"<<endl;
cout<<"PLEASE ENTER THE PAYMENT RECEIVED :";
cin>>payment;
}

cout<<endl;
cout<<"------------------------"<<endl;
cout<<"TOTAL SALE      :$"<<total<<endl;
cout<<"RECEIVE PAYMENT :$"<<payment<<endl;
cout<<"BALANCE         :$"<<payment-total<<endl;
cout<<"------------------------";
cin.get();
}


The id,quantity,price,and the sale appeared to be wrong....
why is this happen???

What do you mean?! is it only the pattern of ( ID | QTY | PRICE | SALE)

Here is the output of your program. and it seems correct!

CASH RECEIPT PROGRAM
--------------------------------------------------------------------------------
Enter the Product ID ?:1
Enter the Product Name : test
Enter the Price For Single Item : 100
Enter The Quantity : 2

Would You like to enter another product? (Y/N) N

--------------------------------------------------------------------------------
ID | ITEM | PRICE | QTY | SALE
--------------------------------------------------------------------------------
1 test $100.00 2 200.00

TOTAL SALE IS :200
PLEASE ENTER THE PAYMENT RECEIVED :400


------------------------
TOTAL SALE : $200
RECEIVE PAYMENT : $400.00
BALANCE : $200.00
------------------------

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.