hi...
i want to ask about my receipt program again..
this time ,i really like to know the ways how to clear the informations after the user has entered those informations that are neede by the program..
For example,take a look at this sample output :
--------------------------------------------------------------------------------------
CASH RECEIPT PROGRAM
--------------------------------------------------------------------------------------
ENTER THE PRODUCT ID :12345
ENTER THE PRODUCT NAME :test
ENTER THE PRICE FOR SINGLE ITEM :$12
ENTER THE QUANTITY :3
WOULD YOU LIKE TO ENTER ANOTHER ITEM ? (Y/N) : y
------------------------------------------------------------------------------------
So,what i want to know is how if i want to clear the informations about the previous product id,name,price and the quantity that the user have entered before proceed to ask the user again about the informations needed for the next item??
this is the full code of my program :
#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();
}