samedi 24 octobre 2020

Output to file result in an empty text file?

hi this is a method function inside a class and i was willing to take some data from the user which is supposedly a salesman and output this data to a text file , the code runs fine with no errors and the condition works but the file is always empty no matter what !!!!!!

int cust_id,employ_id,total,paid,recieved,method,to_qnt=0,r;
vector <int>    pr_id;
vector <int>    pr_quantity;
fstream sales;
sales.open("d:/C/gramarly/Data/sales.txt",ios::app);     //ios::app stands for append mode and it will create the file if does not exist
system("CLS");

//TIME
{
time_t now=time(0);                                       //visit https://www.tutorialspoint.com/cplusplus/cpp_date_time.htm
tm *local = localtime(&now);
cout <<"\t\t\t\t\t\t\t\t\t\tDate: "<< local->tm_mday <<"/"<<1+local->tm_mon<<"/" << 1900 + local->tm_year<<"\tTime: " <<local->tm_hour<<":" <<local->tm_min <<endl;
}
//TIME

cout<<"Customer id:";
cin>>cust_id;
cout<<"Employee id:";
cin>>employ_id;
cout<<"Total Items Purchased:";
cin>>total;
pr_id.resize(total);                                       //resizing the vector in order to be able to hold items the total
pr_quantity.resize(total);
for(int r=0;total>to_qnt;r++){
   cout<<"===================="<<endl;
   cout<<"product no("<<r+1<<") id:";
   cin>>pr_id.at(r);
   //check for id validity
   cout<<"product quantity:";
   cin>>pr_quantity.at(r);
   to_qnt=to_qnt+pr_quantity.at(r);


}
cout<<"===================="<<endl;
cout<<"\nsaving please wait....";
pr_id.resize(r);
pr_quantity.resize(r);
cout<<"\rpurchase method (0 for cash || 1 for credit card) :";
cin>>method;
while(method > 1 || method < 0 ){
   cout<<"\n\aPlease choose an answer from 0 to 1 !!!!!!\n";
    cin.clear();
   system("PAUSE");
   cin.ignore();
   cout<<"purchase method (0 for cash || 1 for credit card) :";
   cin>>method;
}
cout <<"paid amount:";
cin>>paid;
//check if the paid amount is equal to the total price of the items purchased
cout<<"tax (14%):"<<paid*0.14;
recieved=paid-(paid*0.14);
if(sales.is_open()){
   sales<<cust_id;
   cout<<"\n\a FINISHED";
}```

Aucun commentaire:

Enregistrer un commentaire