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);
}

Edited 4 Years Ago by ambinh: n/a

You have a fair bit of code there... could you use [ CODE ] tags to get it highlighted for us?

I edit the post

That was helpful! I noticed a few things. One, on line 28 I do not think you need to open the file as I think the constructor will have done that for you. The main problem though I believe lies in line 100. You have this inFile and are intending to read from it, but it appears that you accidentally read from cin instead. Change cin>>plu.... to inFile>>plu...

That was helpful! I noticed a few things. One, on line 28 I do not think you need to open the file as I think the constructor will have done that for you. The main problem though I believe lies in line 100. You have this inFile and are intending to read from it, but it appears that you accidentally read from cin instead. Change cin>>plu.... to inFile>>plu...

I made some changes in 3 files but still 2 errors left. Here check the revised codes:
product.h

#ifndef product_h
#define product_h
#include <string>
#include <iostream>
using namespace std;

class Product
{
private:
	int plu;
	string name;
	int type;
	double price;
	double stock;

public:
	//Product default consturctor
	Product();
	//product constructor with all parameters
	Product(int, string, int, double, double);
	//func prototypes
	int getPLU();
	string getName();
	int getType();
	double getPrice();
	double getStock();
        double getChangeStock(double);
};
#endif

product.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "product.h"

using namespace std;

Product::Product()
{
	plu = 0;
	name = "unknown";
	type = 0;
	price = 0;
	stock = 0;
}

Product::Product(int pl, string n, int t, double pr, double s)
{
	plu = pl;
	name = n;
	type = t;
	price = pr;
	stock = s;
}

int Product::getPLU()
{	return plu;
}

string Product::getName()
{	return name;
}

int Product::getType()
{	return type;
}

double Product::getPrice()
{	return price;
}

double Product::getStock()
{	return stock;
}
double Product::getChangeStock(double units)
{
	if (units > stock)
		cout << "Out of stock!!!\n";
	else
		stock = stock - units;

	return stock;
}

store.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "product.h"

using namespace std;

//Global variables
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)
{
	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)
{
	double total = 0;
	double units;
	int input = -1;
	int plu = -1;
	do
	{
		plu = -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[plu].getPrice();
		inventory[plu].getChangeStock(units);
	} 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);
}

I am doing this in Microsoft Visual Studio 2008, and the debugging errors are:
store1.cpp(76) : error C2039: 'changeStock' : is not a member of 'Product'
product.h(8) : see declaration of 'Product'

The 'changeStock' is the remaining stock after the purchases

Edited 4 Years Ago by ambinh: n/a

First of all you made neither of the changes I suggested, so even after you stop getting errors your code will not work. Second note that you named the function getChangeStock, not changeStock so that is one of your errors.

First of all you made neither of the changes I suggested, so even after you stop getting errors your code will not work. Second note that you named the function getChangeStock, not changeStock so that is one of your errors.

:sad: I made mistake like that a lot. I fixed it, and it compiled successfully. However, when I enter the PLU code like 4101 like in the text file, the menu showed again :(.

That was helpful! I noticed a few things. One, on line 28 I do not think you need to open the file as I think the constructor will have done that for you. The main problem though I believe lies in line 100. You have this inFile and are intending to read from it, but it appears that you accidentally read from cin instead. Change cin>>plu.... to inFile>>plu...

I changed the line 100 according to you; line 28 I need to update a new inventory file after the purchases; I think I still have problem with the checkout() function.

Edited 4 Years Ago by ambinh: n/a

I don't think you ever made a PLUCode variable. I could be wrong, but it's definition is definately not easy to find.

I don't think you ever made a PLUCode variable. I could be wrong, but it's definition is definately not easy to find.

I found the dummy me at line 56 and 57; they are kind out getting in the way of the rest of the loop.

Thanks you for your time and help Lab. Appriciated:twisted:

This question has already been answered. Start a new discussion instead.