Store code help (parse error[i think])

I've been working with this code for an assignment and the first time I tried to compile it my computer got stuck in a loop where the log would just jump from one file to the next in an attempt to compile. Closed, opened again, tried again, this time it threw all kinds of parse errors but I don't quite understand what the problem is.

store.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include "product.h"
using namespace std;
int main()
{
    int plu;
    string productName;
    int type;
    double price;
    double inventory;
    c = 0;
     
    ifstream original ("inventory.txt");    //read from original inventory file
    if (original.is_open())
    {
        while (!original.eof())
        {
            original >> plu >> productName >> type >> price >> inventory;
            Product temp = Product(plu, productName, type, price, inventory);  
            inv[c] = temp;
            c++;
        }
        original.close();
    }
    int option;
    do
    {
        cout << "WELCOME!" << endl;
        option = menu();
        if (option == 1)
            checkout();
        else
            updateFile();
    }

while(option != 0);
system ("pause");
return 0;
}

Product inv[100];   //array of 100 objects
int c = 100;    //maximum products is 100

int menu(void)  //display user options
{
    int option;
    cout << "Enter 1 to begin checkout or 0 to exit" << endl;
    cin >> option;
    return option;
};

void updateFile()   //output results to a new file
{
    ofstream newInvFile("newinventory.txt");

    for (int p = 0; p < c; p++)
    {
        newInvFile << inv[p].getPLU() << " " << inv[p].getProductName() << " " << inv[p].getType() << " " << inv[p].getPrice() << " " << inv[p].getInventory() << endl;
    }
}

void checkout(void)
{
    double total = 0;
    double quantity;
    int input;
    int PLU = 0;
    
    do
    {
        PLU = 0;

        cout << "Enter PLU code or (0) to exit" << endl;
        cin >> input;
        if (input == 0)
            break;
        for (int p = 0; p < c; p++)
        {
            if (inv[p].getPLU() == input)
            {
                PLU = p;
                break;
            }
        }
        if (inv[PLU].getType() == 1)// Determine whether product is sold by weight or units
        {
            cout << "Weight: ";
        }
        else
        {
            cout << "Quantity: ";
        }
         
        cin >> quantity;
        total += quantity * inv[PLU].getPrice();
        inv[PLU].updateInventory(quantity);
    }
    while (input != 0);     
    cout << "Total: $" << total << endl;   
    if (total > 50)  //apply discount if total is over $50
    {
        total = total * 0.95;
        cout << "Your purchase of over $50 qualifies you for a 5% discount. Total: $" << total << endl;
    }
}


product.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
 #include <iostream>
#include <fstream>
#include <string>
#include product.h
using namespace std;

/*Product::Product()
{
    int plu = 0;
    string productName = "none yet";
    int type = 0;
    double price = 0;
    double inventory = 0;
}

Product::Product(int pl, string pn, int t, double pr, double i)
{
    plu = pl;
    productName = pn;
    type = t;
    price = pr;
    inventory = i;
}
*/
int Product::getPLU()
{   return plu;}

string Product::getProductName()
{   return productName;}

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

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

double Product::getInventory()
{   return inventory;}
 
double Product::updateInventory(double quantity)
{
    if (quantity > inventory)
        cout << "This item is not in stock." << endl;
    else inventory -= quantity;
    return inventory;
}


product.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 #ifndef store_product_h
#define store_product_h
#include <string>
#include <iostream>
using namespace std;
class Product
{
private:
    int plu;
    string productName;
    int type;
    double price;
    double inventory;
public:
    Product();
    Product(int, string, int, double, double);
    int getPLU();
    string getProductName();
    int getType();
    double getPrice();
    double getInventory();
    double updateInventory(double);
};
#endif 


inventory.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
Last edited on
I got it to compile. You need to fix your client file first. Declare before you use. You need to use prototypes at top before main. Also you should have class declarations in the header and definitions in the .cpp.. and remove your constructors from being commented out.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
#include "product.h"

using namespace std;
// Some directions for the user would help to, no idea what plu equals what but I am not sure what
// your plans for this program or if you were handed a sheet of options.
int menu(void); // Prototype
void updateFile(); // Prototype
void checkout(void); //Prototype

Product inv[100];   //array of 100 objects ---> these need to be here
int c = 100;    //maximum products is 100 ----> and not great idea to use global variables..

int main()
{
	int plu;
	string productName;
	int type;
	double price;
	double inventory;
	c = 0;
	
	ifstream original("inventory.txt");    //read from original inventory file
	if (original.is_open())
	{
		while (!original.eof())
		{
			original >> plu >> productName >> type >> price >> inventory;
			Product temp = Product(plu, productName, type, price, inventory);
			inv[c] = temp;
			c++;
		}
		original.close();
	}
	int option;
	do
	{
		cout << "WELCOME!" << endl;
		option = menu();
		if (option == 1)
			checkout();
		else
			updateFile();
	}

	while (option != 0);
	system("pause");
	return 0;
}



int menu(void)  //display user options
{
	int option;
	cout << "Enter 1 to begin checkout or 0 to exit" << endl;
	cin >> option;
	return option;
};

void updateFile()   //output results to a new file
{
	ofstream newInvFile("newinventory.txt");

	for (int p = 0; p < c; p++)
	{
		newInvFile << inv[p].getPLU() << " " << inv[p].getProductName() << " " << inv[p].getType() << " " << inv[p].getPrice() << " " << inv[p].getInventory() << endl;
	}
}

void checkout(void)
{
	double total = 0;
	double quantity;
	int input;
	int PLU = 0;

	do
	{
		PLU = 0;

		cout << "Enter PLU code or (0) to exit" << endl;
		cin >> input;
		if (input == 0)
			break;
		for (int p = 0; p < c; p++)
		{
			if (inv[p].getPLU() == input)
			{
				PLU = p;
				break;
			}
		}
		if (inv[PLU].getType() == 1)// Determine whether product is sold by weight or units
		{
			cout << "Weight: ";
		}
		else
		{
			cout << "Quantity: ";
		}

		cin >> quantity;
		total += quantity * inv[PLU].getPrice();
		inv[PLU].updateInventory(quantity);
	} while (input != 0);
	cout << "Total: $" << total << endl;
	if (total > 50)  //apply discount if total is over $50
	{
		total = total * 0.95;
		cout << "Your purchase of over $50 qualifies you for a 5% discount. Total: $" << total << endl;
	}
}

Last edited on
Thanks for you help! I really do appreciate it! And PLU is product label, its in the text file, supposed to be a sort of barcode number. Will clarify. Onto the random error I'm getting from the receipt program. Thanks again!
Topic archived. No new replies allowed.