File Handling Mini Cashiering


Home || News || Stories || Installer ||

 

Sample Cashiering 

#include <fstream>
#include <iostream>
using namespace std;
 
int main ()
 {
   int price,quantity,total,money,change;
   string result = "Insufficient Money!";

   // open a file in write mode.
   ofstream outfile;
   outfile.open("sample.txt");

   cout << "Writing to the file" << endl;

   cout << "Enter Price: ";
   cin>>price;

   cout << "Enter Quantity: ";
   cin>>quantity;

   total = price * quantity;
   cout<<"Total: "<<total <<endl;

   cout << "Enter Money: ";
   cin>>money;

 if(money>=total)
 {
   change = money - total;
   cout<<"Change: "<<change <<endl;

 }

 else
 {
     
     cout<<result<<endl;
 }



   // write inputted data into the file.
   outfile << price << endl;
   outfile << quantity << endl;
   outfile << total << endl;
   outfile << money << endl;

   if(money >= total)
   {
   outfile << change << endl;
   }
   else
   {
     
   }
 
   if(money >= total)
   {

   }
   else
   {
     outfile << result << endl;
   }


   // close the opened file.
   outfile.close();


   // open a file in read mode.
   ifstream infile;
   infile.open("sample.txt");
 
   cout << "Reading from the file" << endl;
   infile >> price;
   infile >> quantity;
   infile >> total;
   infile >> money;
   infile >> change;
   infile >> result;

   // write the data at the screen.
   cout<<"Price: " << price << endl;
   cout<<"Quantity: " << quantity << endl;
   cout<<"Total " << total << endl;
   cout<<"Money " << money << endl;
   cout<<"Change " << change << endl;
   

   // close the opened file.
   infile.close();

   return 0;
}


reading and writing to a file


























Mga Komento