Cash Register Problem

Hey guys, I'm trying to create a cash register program that shows you a list of items you can pick from, then when you pick one it will ask you how many.

I'm trying to output a "subtotal" that multiplies the cost of the item you chose with the number of items you want then multiplied to a 30% markup. I don't fully understand how relationships work in c++ but from what I wrote I hope you guys can discern what I'm trying to get at.

Also it is giving me an error regarding the line...
" inventory[1].getCost() * numberpick == newcost "

with

"C4553: '==' : operator has no effect; did you intend '='?"

Thanks guys!

Inventory Item Header
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
//Cash Register Header File
//By: Andrew Aquino

#pragma
#ifndef cashregister_h
#define cashregister_h
#include "Inventory Item Header.h"

using namespace std;


class CashRegister : public InventoryItem
{
private:
	double subtotal;
	double total;
	double tax;
public:
	CashRegister()
	{
		subtotal = 0;
		total = 0;
		tax = 0.06;
	}

	CashRegister(double sub, double tot, double tx)
	{
		subtotal = sub;
		total = tot;
		tax = tx;
	}

	void setSubtotal(double sub)
	{
		subtotal = sub;
	}

	void setTotal(double tot)
	{
		total = tot;
	}

	double getSubtotal() const
	{
		return subtotal == (1.3 * cost * units);
	}

	double getTotal() const
	{
		return total == (tax * subtotal);
	}
};

#endif cashregister_h 


Cash Register Header
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
// This class has overloaded constructors.

#pragma
#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <string>

using namespace std;
class InventoryItem
{
	protected:
		string description; // The item description
		double cost; // The item cost
		int units; // Number of units on hand
		int onhand;
	public:
	// Constructor #1
		InventoryItem()
		{ // Initialize description, cost, and units.
			description = "";
			cost = 0.0;
			units = 0;
			onhand = 0;
		}
	// Constructor #2
		InventoryItem(string desc)
		{ // Assign the value to description.
			description = desc;
	// Initialize cost and units.
			cost = 0.0;
			units = 0;
			onhand = 0;
		}
	// Constructor #3
		InventoryItem(string desc, double c, int u, int oh)
			{ // Assign values to description, cost, and units.
			description = desc;
			cost = c;
			units = u;
			onhand = oh;
		}
	// Mutator functions
	void setDescription(string d)
	{
		description = d;
	}
	void setCost(double c)
	{
		cost = c;
	}
	void setUnits(int u)
	{
		units = u;
	}
	void setonhand(int oh)
	{
		onhand = oh;
	}
	// Accessor functions
	string getDescription() const
		{ return description; }
	double getCost() const
		{ return cost; }
	int getUnits() const
		{ return units; }
	int getOnHand () const
	{ return onhand; }
};
#endif 


Cash Register CPP File
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
#include "Cash Register Header.h"
#include "Inventory Item Header.h"
#include <iostream>
#include <iomanip>

using namespace std;



int main()
{
	int pick, numberpick, newcost = 0;
	InventoryItem Item, SetItemCost;
	CashRegister subobj, totobj, taxobj;
	const int NUM_ITEMS = 5;

	InventoryItem inventory[NUM_ITEMS] = {
		InventoryItem("1 Hammer", 6.95, 12, 10),
		InventoryItem("2 Wrench", 8.75, 20, 10),
		InventoryItem("3 Pliers", 3.75, 10, 10),
		InventoryItem("4 Ratchet", 7.95, 14, 10),
		InventoryItem("5 Screwdriver", 2.50, 22, 10) };

	cout << setw(14) <<"Inventory Item"
		<< setw(8) << "Cost" << setw(8)
		<< setw(16) << "Units on Hand\n";
	cout << "-------------------------------------\n";

	for (int i = 0; i < NUM_ITEMS; i++)
	{
		cout << setw(14) << inventory[i].getDescription();
		cout << setw(8) << inventory[i].getCost();
		cout << setw(7) << inventory[i].getUnits() << endl;
	}

	cout << "\n";
	cout << "Enter #1 to #5 for item selection " << endl;
	cin >> pick; cout << "\n";

	cout << "How many? " << endl;
	cin >> numberpick; cout << "\n";

	if (pick = 1)
	{ inventory[1].getCost() * numberpick == newcost;}
	else if (pick = 2)
	{ inventory[2].getCost() * numberpick == newcost;}
	else if (pick = 3)
	{ inventory[3].getCost() * numberpick == newcost;}
	else if (pick = 4)
	{ inventory[4].getCost() * numberpick == newcost;}
	else if (pick = 5)
	{ inventory[5].getCost() * numberpick == newcost;}

	SetItemCost.setCost(newcost);

	cout << subobj.getSubtotal();

	cin.get();
	cin.ignore();
	return 0;
}

looks like you mixed up your equals == operator with the assignment operator =.

for example
1
2
	if (pick = 1)
	{ inventory[1].getCost() * numberpick == newcost;}


should look more like
1
2
3
4
	if (pick == 1)
	{
		newcost = inventory[1].getCost() * numberpick;
	}
Last edited on
Remember to comment after the #endif in your cashregister_h definition.
 
#endif //cashregister_h 


Also, to avoid including the same header files over again.
 
#pragma once 


For your pick check try using a switch, (Also i jsut noticed you're not pointing to anything in your inventory[] with inventory[5])
1
2
3
4
5
6
7
8
9
switch (pick)
{
case 1: inventory[0].getCost() * numberpick = newcost; break;
case 2: inventory[1].getCost() * numberpick = newcost; break;
case 3: inventory[2].getCost() * numberpick = newcost; break;
case 4: inventory[3].getCost() * numberpick = newcost; break;
case 5: inventory[4].getCost() * numberpick = newcost; break;
default: break;
}

I'm not quite sure what you're getting at with that * numberpick = newcost; though.
Last edited on
Thanks for the correction guys, heres my new code for the CPP file...

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
#include "Cash Register Header.h"
#include "Inventory Item Header.h"
#include <iostream>
#include <iomanip>

using namespace std;



int main()
{
	int pickItem, pickUnits;
	double newcost = 0;
	InventoryItem Item, SetItemCost, SetU, GetU, NewItem();
	CashRegister subobj, totobj, taxobj;
	const int NUM_ITEMS = 5;

	InventoryItem inventory[NUM_ITEMS] = {
		InventoryItem("Hammer", 6.95, 12, 10),
		InventoryItem("Wrench", 8.75, 20, 10),
		InventoryItem("Pliers", 3.75, 10, 10),
		InventoryItem("Ratchet", 7.95, 14, 10),
		InventoryItem("Screwdriver", 2.50, 22, 10) };

	cout << setw(14) <<"Inventory Item"
		<< setw(8) << "Cost" << setw(8)
		<< setw(16) << "Units on Hand\n";
	cout << "-------------------------------------\n";

	for (int i = 0; i < NUM_ITEMS; i++)
	{
		cout << setw(14) << inventory[i].getDescription();
		cout << setw(8) << inventory[i].getCost();
		cout << setw(7) << inventory[i].getUnits() << endl;
	}

	cout << "\n";
	cout << "Enter #1 to #5 for item selection " << endl;
	cin >> pickItem; cout << "\n";

	cout << "How many? " << endl;
	cin >> pickUnits; cout << "\n";

	switch (pickItem)
	{
	case 1: newcost = inventory[0].getCost() * pickUnits; break;
	case 2: newcost = inventory[1].getCost() * pickUnits; break;
	case 3: newcost = inventory[2].getCost() * pickUnits; break;
	case 4: newcost = inventory[3].getCost() * pickUnits; break;
	case 5: newcost = inventory[4].getCost() * pickUnits; break;
	default: break;
	}

	cin.get();
	cin.ignore();
	return 0;
}


as you can see I'm trying to create a program that lets the user pick the item in the array then it will call that item's cost and multiply it by the number of units the user wants to buy.

Once he has the new cost, I'm trying to put that new cost into the Cash Register's subtotal function...

still trying to wrap my head around on how I'm going to get around that but some guidance would help!
Last edited on
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
#include <iostream>
#include <string>

using namespace std;

#define ITEMS_QTY_STORE 5
#define ITEMS_BAG_QTY 3

struct Inventory
{
	string Name;
	string Description;
	float Price;
};
struct Shopper
{
	Inventory Bag[ITEMS_BAG_QTY];
	float Wallet;
};

int main()
{
	Inventory* store[ITEMS_QTY_STORE];
	for (int i = 0; i < ITEMS_QTY_STORE; ++i)
	{
		store[i] = new Inventory;
		store[i]->Name = "Item #";
		store[i]->Name += to_string(i);
		store[i]->Description = "This is an Item... ";
		store[i]->Description += (i + 1);
		store[i]->Price = (i * 1.25) + 1;
	}
	Shopper* myShopper;
	myShopper = new Shopper;
	myShopper->Wallet = 5;	// Starting Cash...
	
	while (true)
	{
		printf("Welcome to the Happy Grocery Store!\nOur current List of Items are...\n");
		for (int i = 0; i < ITEMS_QTY_STORE; ++i)
			printf("%d: %s \$%f (%s)\n", 
				i + 1, store[i]->Name.c_str(), store[i]->Price, store[i]->Description.c_str());
		
		int choice;
		cin >> choice;
		if (choice > 0 && choice < ITEMS_QTY_STORE)
		{
			if (store[choice]->Price < myShopper->Wallet)
			{
				// You can purchase this item!
			}
			else {
				// You cannot afford this item!
			}
		}
	}
	return 0;
}


It's getting late here so I couldn't write up a whole example, but this basically just covers another way you could do this. **Notice how I haven't included any algorithmic functionality to it yet.

Hope this helps for now!
Last edited on
thanks Peppercorn for the help but I wanted to create my own code and learn from it, I appreciate it tho.

I finally got the program to work more than how I wanted it to be :)

Here's the polished program to anyone that needs to understand..

Inventory Item Header:
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
// This class has overloaded constructors.

#ifndef INVENTORYITEM_H
#define INVENTORYITEM_H
#include <string>

using namespace std;
class InventoryItem
{
	protected:
		string description; // The item description
		double cost; // The item cost
		int units; // Number of units on hand
		int onhand;
	public:
	// Constructor #1
		InventoryItem()
		{ // Initialize description, cost, and units.
			description = "";
			cost = 0.0;
			units = 0;
			onhand = 0;
		}
	// Constructor #2
		InventoryItem(string desc)
		{ // Assign the value to description.
			description = desc;
	// Initialize cost and units.
			cost = 0.0;
			units = 0;
			onhand = 0;
		}
	// Constructor #3
		InventoryItem(string desc, double c, int u, int oh)
		{ // Assign values to description, cost, and units.
			description = desc;
			cost = c;
			units = u;
			onhand = oh;
		}

	// Mutator functions
	void setDescription(string d)
	{
		description = d;
	}
	void setCost(double c)
	{
		cost = c;
	}
	void setUnits(int u)
	{
		units = u;
	}
	void setonhand(int oh)
	{
		onhand = oh;
	}
	// Accessor functions
	string getDescription() const
		{ return description; }
	double getCost() const
		{ return cost; }
	int getUnits() const
		{ return units; }
	int getOnHand () const
	{ return onhand; }
};
#endif 


Cash Register Header
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
//Cash Register Header File
//By: Andrew Aquino

#pragma
#ifndef cashregister_h
#define cashregister_h
#include "Inventory Item Header.h"

using namespace std;


class CashRegister : public InventoryItem
{
private:
	double subtotal;
	double total;
	double tax;
public:

	CashRegister()
	{
		subtotal = 0;
		total = 0;
		tax = 0.06;
	}

	CashRegister(double sb, double t, double tx)
	{
		subtotal = sb;
		total = t;
		tax = tx;
	}

	void setSubtotal(double sb)
	{
		subtotal = sb;
	}

	void setTotal(double t)
	{
		total = t;
	}

	void setTax(double tx)
	{
		tax = tx;
	}

	double getSubtotal() const
	{
		return subtotal;
	}

	double getTotal() const
	{
		return total;
	}

	double getTax() const
	{
		return tax;
	}
};

#endif //cashregister_h 


Main CPP File...
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
118
119
120
121
122
123
124
125
#include "Cash Register Header.h"
#include "Inventory Item Header.h"
#include <iostream>
#include <iomanip>

using namespace std;



int main()
{
	double pickItem;
	double newtax = 0.06;
	double newcost = 0;
	int newunits = 0;
	int newonhand = 0;
	int stock = 0;
	const int NUM_ITEMS = 5;

	InventoryItem inventory[NUM_ITEMS] = {
		InventoryItem("Hammer", 6.95, 12, 10),
		InventoryItem("Wrench", 8.75, 20, 10),
		InventoryItem("Pliers", 3.75, 10, 10),
		InventoryItem("Ratchet", 7.95, 14, 10),
		InventoryItem("Screwdriver", 2.50, 22, 10) };

	cout << setw(14) <<"Inventory Item"
		<< setw(8) << "Cost" << setw(8)
		<< setw(16) << "Units on Hand\n";
	cout << "-------------------------------------\n";

	for (int i = 0; i < NUM_ITEMS; i++)
	{
		cout << setw(14) << inventory[i].getDescription();
		cout << setw(8) << inventory[i].getCost();
		cout << setw(7) << inventory[i].getUnits() << endl;
	}

	cout << "\n";
	cout << "Enter #1 to #5 for item selection: ";

	int flag1 = 0;
	while(flag1 = 1) {
		cin >> pickItem;
		if(pickItem < 1 || pickItem > 5)
		{ cout << "Please enter a number within range: ";}
		else
		{break;}
	};

	cout << "\n";

	if (pickItem == 1)

	{ newcost = inventory[0].getCost();}
	else if (pickItem == 2)
	{ newcost = inventory[1].getCost();}
	else if (pickItem == 3)
	{ newcost = inventory[2].getCost();}
	else if (pickItem == 4)
	{ newcost = inventory[3].getCost();}
	else if (pickItem == 5)
	{ newcost = inventory[4].getCost();}

	cout << "How much would you like to buy: ";

	int flag2 = 0;
	while(flag2 = 1) {
	cin >> newunits;
	if(newunits < 1)
	{ cout << "Please enter a positive number: ";}
	else
	{break;}
	};
	
	cout << "\n";

	CashRegister Register(
		(1.3*newcost)*newunits,
		1.06 * ((1.3*newcost)*newunits), newtax
		);

	cout << fixed << showpoint << setprecision(2);
	cout << "Your Subtotal is $" << Register.getSubtotal()
		<< " and your Total is $" << Register.getTotal()
		<< " with " << Register.getTax() << "% Tax included."
		<< "\n" << endl;

	cout << "In addition, there is ";

	if (pickItem == 1)
	{ inventory[0].setonhand(inventory[0].getUnits() - newunits);
	cout << inventory[0].getOnHand();}
	else if (pickItem == 2)
	{ inventory[1].setonhand(inventory[1].getUnits() - newunits);
	cout << inventory[1].getOnHand();}
	else if (pickItem == 3)
	{ inventory[2].setonhand(inventory[2].getUnits() - newunits);
	cout << inventory[2].getOnHand();}
	else if (pickItem == 4)
	{ inventory[3].setonhand(inventory[3].getUnits() - newunits);
	cout << inventory[3].getOnHand();}
	else if (pickItem == 5)
	{ inventory[4].setonhand(inventory[4].getUnits() - newunits);
	cout << inventory[4].getOnHand();}

	cout << " of ";

	if (pickItem == 1)
	{cout << inventory[0].getDescription();}
	else if (pickItem == 2)
	{cout << inventory[1].getDescription();}
	else if (pickItem == 3)
	{cout << inventory[2].getDescription();}
	else if (pickItem == 4)
	{cout << inventory[3].getDescription();}
	else if (pickItem == 5)
	{cout << inventory[4].getDescription();}
	
	cout << " left in stock." << endl;

	cin.get();
	cin.ignore();
	return 0;
};
Last edited on
Topic archived. No new replies allowed.