-2
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string saveload;
int numProducts=0;
int counter=1;
int product;
double payment1;
double change=0;
double subtotal=0;
double payment=0;
int n;
string Id;
int option;
const char* products[] = {"Cardinal Steak w/ drink and dessert", "Cardinal Grilled Chicken w/ drink and dessert",
 "Cardinal Bangus w/ drink and dessert", "Mathematician Burger w/ drink and dessert", 
 "Cardinal Spring Rolls w/ drink and dessert", "Cardinal Steak", "Cardinal Grilled Chicken", "Cardinal Bangus",
 "Mathematician Burger", "Cardinal Spring Rolls", "Sweet Mapuan Potato", "Wedged Mapuan Potato",
 "Mashed Math Potato w/ gravy", "Physics Special Salad", "Chemistry Hot wings Special", "Sinko Shake",
 "Banofee", "Korean shaved ice with sugar, fruit syrup", "Nigerian Pepper Soup", "Sudanese Basbosa"};
 double prices[] = {450, 400, 200, 250, 350, 300, 280, 140, 130, 150, 80, 90, 60, 40, 100, 55, 60, 77, 77, 85};
 int items[100];

cout<<setw(120)<<"Organic Food Chain"<<endl;
cout<<"Please enter your Id"<<endl;
cout<<"Id: ";
cin>>Id;
if(Id=="a")
{
    cout<<"Password: *******"<<endl<<endl;
    cout<<"1. Order"<<endl;
    cout<<"2. Show the record"<<endl;
    cout<<"3. Log out"<<endl;
    cout<<"Choose: ";
    cin>>option;
    cout<<endl;
        if(option==1)
        {

            cout<<"How many products are you ordering?: ";
            cin>>numProducts;

            for(n = 0;n<numProducts;n++)
            {
            cout<<"Combo"<<endl;
            cout<<"1.Cardinal Steak w/ drink and dessert Php450"<<endl;
            cout<<"2.Cardinal Grilled Chicken w/ drink and dessert Php400"<<endl;
            cout<<"3.Cardinal Bangus w/ drink and dessert Php200"<<endl;
            cout<<"4.Mathematician Burger w/ drink and dessert Php250"<<endl;
            cout<<"5.Cardinal Spring Rolls w/ drink and dessert Php350"<<endl<<endl;
            cout<<"Alacarte"<<endl;
            cout<<"6.Cardinal Steak Php300"<<endl;
            cout<<"7.Cardinal Grilled Chicken Php280"<<endl;
            cout<<"8.Cardinal Bangus Php140"<<endl;
            cout<<"9.Mathematician Burger Php130"<<endl;
            cout<<"10.Cardinal Spring Rolls Php150"<<endl<<endl;
            cout<<"Sidedish"<<endl;
            cout<<"11.Sweet Mapuan Potato Php80"<<endl;
            cout<<"12.Wedged Mapuan Potato Php90"<<endl;
            cout<<"13.Mashed Math Potato w/ gravy Php60"<<endl;
            cout<<"14.Physics Special Salad Php40"<<endl;
            cout<<"15.Chemistry Hot wings Special Php100"<<endl<<endl;
            cout<<"Drink & Dessert"<<endl;
            cout<<"16.Sinko Shake Php55"<<endl;
            cout<<"17.Banofee Php60"<<endl;
            cout<<"18.Korean shaved ice with sugar, fruit syrup  Php77"<<endl;
            cout<<"19.Nigerian Pepper Soup Php77"<<endl;
            cout<<"20.Sudanese Basbosa Php85"<<endl;

            do{
            cout <<"Enter your selection: ";
            cin>>product;
            cout<<endl;
        }while(product < 1 || product > 20);

        items[n]=product-1;
        }
        subtotal=0;
for(n=0;n<numProducts;n++)
{
    cout<<left<<setw(25)<<products[items[n]]; 
    cout<<right<<setw(10)<<prices[items[n]]<<endl;
    subtotal=subtotal+prices[items[n]];
}

cout<<right;
cout<<"-----------------------------------------"<<endl;
cout<<"Total:  "<<setw(24)<<subtotal<<endl;
do{
cout<<"Please make payment: ";
cin>>payment1;
payment=payment+payment1;
if(payment > subtotal)
{
    change=payment-subtotal;
    cout<<"Your change is: "<<change;
    ofstream out("save.txt");
cout.rdbuf(out.rdbuf()); 
cout<<left<<setw(25)<<products[items[n]]; 
    cout<<right<<setw(10)<<prices[items[n]]<<endl;
    subtotal=subtotal+prices[items[n]];
    cout<<right;
cout<<"-----------------------------------------"<<endl;
cout<<"Total:  "<<setw(24)<<subtotal<<endl;
cout<<"Please make payment: "<<payment;
cout<<"Your change is: "<<change;

}
}while((payment-subtotal) < 0);    
    }
    if(option==2);

            }
else
{
    cout<<"Please enter the correct Id";
}
return 0;
}

I made the first few sentences but inside if statements I just followed one of the POS program in the net. The problem is that I have to save and load the record but I can not save it. The part that Im going to save is where the code ofstream out("save.txt"); cout.rdbuf(out.rdbuf()); You can totally erase this statement as long as I can save those information. For additional can you add there how to load?? Though it is not complete yet, I just need to know how to save and load the record in this program. please help. Thank you very much. By the way I am using Dev C++ ver.5.5.1.

zneh
  • 1
  • 1
  • It will be helpful to reduce the program to a smaller program so the problem can be duplicated easily. – R Sahu Sep 08 '14 at 18:48

1 Answers1

0

I suggest you eliminate trying to tie two streams and print the data twice, once to cout and once to the file.

Simplifying output by using structure.

You can simplify your output of a data record by using a function, or method, that takes a record (structure item). You could pass different streams to the function rather than duplicating code. But it all boils down to using a structure.

Let's group the items into a structure:

struct POS_Item // Point of Sale item
{
  double price;
  std::string description;
  unsigned int product_id;
};

Let's initialize a structure instance.

 POS_Item item; // create an instance

 // Initialize the item.
 item.price = 24
 item.description = "Mathematician Burger";
 item.product_id = 3;

Output a POS Item:

And create a function to output the item:

void output_item(std::ostream& out, const POS_Item& item)
{
  out << item.id << ". " << item.description << "   Php " << item.price << "\n";
}

To output the item to a file:

  output_item(out, item);

To output the item to the console:

  output_item(std::cout, item);

A Container of POS items:

Since the POS details are in a structure, you can have one container for all the records:

std::vector<POS_Item> item_container;

Adding an item to the container:

  item.id = 26;
  item.description = "Omelete";
  item.price = 24;
  item_container.push_back(item);

Output all the items in the container to a file:

  std::vector<POS_Item>::const_iterator iter;
  for (iter =  item_container.begin();
       iter != item_container.end();
     ++iter)
  {
    output_item(out, *iter);
  }

Using a structure will simplify your code making it easier to write and debug.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
  • Thank you very much for your effort explanation. Still it will take time for me to understand what you said since Im just a beginner but thank you very much again, Im sure it will help me a lot in my program hahaha – zneh Sep 09 '14 at 13:02