c++ store program

Write your question here.
Hi guys, my program is a mini-store. My problem is that I want to substract the number of the item that the user wants to buy out of qty in my Product class. Idk how can someone help.
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Store.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
class Store
{
private:
	int sell;
	int calcprofits;
};
class Product
{
private:
	int productId;
	string productName;
	double buyPrice;
	double sellPrice;
	int qty;
	int soldQty;
public:
	Product(int id, string name, double bprice, double sprice, int q);
	int getProductId() const;
	string getProductName() const;
	double getBuyPrice() const;
	double getSellPrice() const;
	int getSoldQty() const;
};

Product::Product(int id, string name, double bprice, double sprice, int q)
{
	productId = id;
	productName = name;
	buyPrice = bprice;
	sellPrice = sprice;
	qty = q;
	soldQty = 0;
}

int Product::getProductId() const {
	return productId;
}

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

double Product::getBuyPrice() const
{
	return buyPrice;
}

double Product:: getSellPrice() const
{
	return sellPrice;
}
int Product::getSoldQty() const
{
	return soldQty ;
}
int _tmain(int argc, _TCHAR* argv[])
{
	Product chips (1, "chips", .99 , 1.99, 50);
	Product pepsi (2, "pepsi", 1.99 , 2.99, 50);
	Product oranges (3, "oranges", 5.00, 10.00, 50);
	Product chocolate (4, "chocolate", 0.50, 0.99, 50);
	Product gum (5, "gum", 0.10 , 0.99 , 50);
	Product bread (6, "bread", 1.99 , 2.50, 50);
	Product icecream (7, "ice cream" , 1.00 , 3.00, 50);
	Product apples (8, "apples" , 2.00 , 4.00, 50);
	Product peanutbutter(9, "peanut butter", 1.00 , 3.00 , 50);
	Product eggs (10, "eggs", 2.00 , 5.00 , 50);
	int choice;
	cout<< "What Would You Like To Do" <<endl;
	cout<< "1. Sell" << endl;
	cout<< "2. Restock" <<endl;
	cout<< "3. CalcProfits" <<endl;
	cout<< "(Enter A Number That Corresponds With Choice)" <<endl;
	cin>> choice;
	if (choice = 1)
	{
		int choice2;
		cout<< "What Would You Like To Buy"<<endl;
		cout<< "1. Chips"<<endl;
		cout<< "2. Pepsi" <<endl;
		cout<< "3. Oranges" <<endl;
		cout<< "4. Chocolate" <<endl;
		cout<< "5. Bubble Gum" <<endl;
		cout<< "6. Bread" <<endl;
		cout<< "7. Ice Cream" <<endl;
		cout<< "8. Apples" <<endl;
		cout<< "9. Peanut Butter" <<endl;
		cout<< "10.Eggs" <<endl;
		cin>> choice2;
		if(choice2 == 1)
		{
			int num;
			cout<< "The Price For Chips is" << " " << chips.getSellPrice() << endl;
			cout<< "How many chips would you like to buy" <<endl;
			cin>> num;
			
		}
		if(choice2 == 2)
		{

		}
		if(choice2 == 3)
		{

		}
		if(choice2 == 4)
		{

		}
		if(choice2 == 5)
		{

		}
		if(choice2 == 6)
		{

		}
		if(choice2 == 7)
		{

		}
		if(choice2 == 8)
		{

		}
		if(choice2 == 9)
		{

		}
		if(choice2 == 10)
		{

		}
	}
	return 0;
}
  
  
You could just make a public function called "sell" or something like that to simply decrement the qty counter.

Something like:
1
2
3
4
5
6
7
int Product::sell() {
  if(qty > 0) {
    qty--; //decrement qty
    return 1; //success
  }
  return 0; //failed (no inventory)
}


Then when you actually make a sale, call this function on the item you are selling and check the return code to verify it worked.

Topic archived. No new replies allowed.