i'm so sory because i made mistake when asking about this before..
so here i'll make correction in this post....
actually what i want to ask is why my cin.fail() code do not works??
i want this program to ask the user back the same question if the user enter the wrong data type input....
i am using do while loop plus with the cin.fail() in my code but it seems to do the infinite loop when the user enter the wrong data type input...
below is my full code...
Thanks for helping :)
#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{
do{
cout << "Enter the Product id : ";
cin >> product[i].id;
}while(!isdigit(i));//this is actually what i want t do...//but whats wrong??????
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');
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();
}