I need some help in 3 files of code from a check-out program that I am playing with. I think I forgot to declare some variable, and some weird syntax with semicolon. Text file contain the info:
4101 BRAEBURN_REG 1 0.99 101.5
4021 DELICIOUS_GDN_REG 1 0.89 94.2
4020 DELICIOUS_GLDN_LG 1 1.09 84.2
4015 DELICIOUS_RED_REG 1 1.19 75.3
4016 DELICIOUS_RED_LG 1 1.29 45.6
4167 DELICIOUS_RED_SM 1 0.89 35.4
4124 EMPIRE 1 1.14 145.2
4129 FUJI_REG 1 1.05 154.5
4131 FUJI_X-LGE 1 1.25 164.1
4135 GALA_LGE 1 1.35 187.7
4133 GALA_REG 1 1.45 145.2
4139 GRANNY_SMITH_REG 1 1.39 198.2
4017 GRANNY_SMITH_LGE 1 1.49 176.5
3115 PEACHES 1 2.09 145.5
4011 BANANAS 1 0.49 123.2
4383 MINNEOLAS 1 0.79 187.3
3144 TANGERINES 1 1.19 135.5
4028 STRAWBERRIES_PINT 0 0.99 104
4252 STRAWBERRIES_HALF_CASE 0 3.99 53
4249 STRAWBERRIES_FULL_CASE 0 7.49 67
94011 ORGANIC_BANANAS 1 0.99 56.3
Thank you for your help.
product.h file
#ifndef product_h
#define product_h
#include <string>
#include <iostream>
using namespace std;
class Inventory
{
private:
int plu; //plu code
string name; //product name
int type; //type unit or weight
double price; //price
double stock; //amount in store
public:
//Product default consturctor
void Product();
//product constructor with all parameters
void Product(int, string, int, double, double);
//func prototypes
int getPLU();
string getName();
int getType();
double getPrice();
double getStock();
};
#endif
product.cpp file
#include <iostream>
#include <fstream>
#include <string>
#include "product.h"
using namespace std;
void Inventory::Product()
{
plu = 0;
name = "unknown";
type = 0;
price = 0;
stock = 0;
}
void Inventory::Product(int pl, string n, int t, double pr, double s)
{
plu = pl;
name = n;
type = t;
price = pr;
stock = s;
}
int Inventory::getPLU()
{ return plu;
}
string Inventory::getName()
{ return name;
}
int Inventory::getType()
{ return type;
}
double Inventory::getPrice()
{ return price;
}
double Inventory::getStock()
{ return stock;
}
finally store.cpp file
#include <iostream>
#include <fstream>
#include <string>
#include "product.h"
using namespace std;
Product inventory[100];
int count = 100;
int menu(void)
{
int choice;
cout<<"1. Begin checkout\n";
cout<<"2. Exit\n";
do
{
cout<<"Enter number of choice: ";
cin>>choice;
}while((choice < 1)||(choice > 2));
return choice;
};
int writeFile(void) //update the new stock after purchasing
{
ofstream outFile("new_inventory.txt");
outFile.open("new_inventory.txt");
if (!outFile)
{
cout << "Cannot open file.\n";
return 1;
}
for (int i = 0; i < count; i++)
{
outFile << inventory[i].getPLU() << " " << inventory[i].getName() << " " << inventory[i].getType() << " " << inventory[i].getPrice() << " " << inventory[i].getStock() << "\n";
}
outFile.close();
return 0;
}
void checkout(void) //can purchase until no more in store
{
double total = 0;
double units;
int input = -1;
int plu = -1;
do
{
PLUCode= -1;
cout<< "Enter PLU code (enter -1 to quit): ";
cin>> input;
if (input = -1)
break;
for (int i = 0; i < count; i++) //check for PLU code
{
if (inventory[i].getPLU() == input)
{
plu = i;
break;
}
}
if (plu == -1)
{
cout<<"PLU code was invalid.\n";
continue;
}
if (inventory[plu].getType() == 0)
cout << "Enter number of units: ";
else cout << "Enter weight: ";
cin >> units;
total += units * inventory[PLUCode].getPrice();
inventory[PLUCode].changeStock(units); //update the stock
} while (input != -1);
cout << "Total is $" << total;
if (total > 50)
{
total = total *.95;
cout << "Your total is over $50, so you will have a 5% discount \n Your final total is $" << total;
}
}
int main()
{
int plu;
string name;
int type;
double price;
double stock;
count = 0;
ifstream inFile ("inventory.txt");
if (inFile.is_open())
{
while (!inFile.eof())
{
cin >> plu >> name >> type >> price >> stock;
Product temp = Product(plu,name,type,price,stock);
inventory[count] = temp;
count++;
}
inFile.close();
}
int choice;
do
{
cout<< "Menu: ";
choice = menu();
if (choice == 1)
checkout();
else
writeFile();
} while(choice != 2);
}