Why doesn't this code print?

Pages: 12
I suspect it has to do with the way you are creating/adding new products on line 142-143. You may want to create a product pointer somewhere near line 108, then create new instances of the product class to add to the store in case 5.

108: Product *p;

142-143:
1
2
p = new Product(id,name,bprice,sellprice,qty);
ucStore.addProduct(*p);
Last edited on
I Have no idea how to use a debugger

Then you should definitely learn. If you don't want to i'd seriously consider never writing a program again.

A good start would just be to insert a load of cout statements printing intermediate values to the screen at different points in the code.
Last edited on
But can someone explain how to fix this???!!! I will learn how to use a debugger.
Anyone??????
Did you try my suggestion? I suspect the scope of your product variables is expiring before you call your print routine. I suppose that would cause a seg fault trying to access them, but maybe you're getting lucky with that piece. I would create a new memory space for each of your products explicitly like I stated.
The only thing wrong with it is your formatting.
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// Store.cpp : Defines the entry point for the console application.
//

#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;

class Product
{
public:
	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 ;
}
class Store
{
private:
	int calcprofits;
	vector <Product> products;
public:
	void addProduct (Product &p);
	void deleteProduct (int id);
	void displayMenu ();
	void displayInv ();
};

void Store::addProduct (Product &p)
{
	products.push_back (p);
}
void Store::deleteProduct (int id)
{
	products.erase(products.begin() + id);
}
void Store ::displayMenu ()
{
	cout<< "What Would You Like To Do" <<endl;
	cout<< "1. Sell" << endl;
	cout<< "2. Restock" <<endl;
	cout<< "3. CalcProfits" <<endl;
	cout<< "4. CalcSales" <<endl;
	cout<< "5. Add Product" <<endl;
	cout<< "6. Delete Product" <<endl;
	cout<< "7 Print Inventory" <<endl;
	cout<<  "8. Exit" <<endl;
	cout<< "(Enter A Number That Corresponds With Choice)" <<endl;
}
void Store ::displayInv ()
{
	for(int x = 0; x < products.size() ; x++)
	{
	    //this needs better formatting
		Product p = products[x];
        cout<< p.getProductId();
		cout<< p.getProductName ();
		cout<< p.getBuyPrice ();
		cout<< p.getSellPrice ();
		cout<< p.getSoldQty ();
	}
}

int main(int argc, char* argv[])
{
	Store ucStore;
	cout<< "Welcome to The Union City Store";
	while(true)
	{
		ucStore.displayMenu();
		int choice;
		cin>> choice;
		int id;
		string name;
		double bprice;
		double sellprice;
		int qty;
		int product;
		switch (choice)
		{
		case 7:
			ucStore.displayInv();
			break;
		case 5:
		 //this needs better formatting try adding a space before getting input otherwise it looks like -- Enter product id1 instead of something like --Enter product id: 1
			cout<< "Enter product id";
		    
			cin>> id;
			cout<< "Enter product name";
			
			cin>> name;
			cout<< "Enter buy price";
			
			cin>> bprice;
			cout<< "Enter sell price";
			
			cin>> sellprice;
			cout<< "Enter Quantity";
			
			cin>> qty;
			{ //create a new local scope inside this case
			    Product p (id,name,bprice,sellprice,qty);
			    ucStore.addProduct(p);
			}
			break;
		case 1:
		    ucStore.displayInv();
			cout<< "What would you like to buy";
			cin>> product;
		}
	
}

  return 0;
  
}
  
  
//}
Welcome to The Union City StoreWhat Would You Like To Do
1. Sell
2. Restock
3. CalcProfits
4. CalcSales
5. Add Product
6. Delete Product
7 Print Inventory
8. Exit
(Enter A Number That Corresponds With Choice)
5
Enter product id1
Enter product nameapple
Enter buy price10
Enter sell price20
Enter Quantity5
What Would You Like To Do
1. Sell
2. Restock
3. CalcProfits
4. CalcSales
5. Add Product
6. Delete Product
7 Print Inventory
8. Exit
(Enter A Number That Corresponds With Choice)
7
1apple10200What Would You Like To Do
1. Sell
2. Restock
3. CalcProfits
4. CalcSales
5. Add Product
6. Delete Product
7 Print Inventory
8. Exit
(Enter A Number That Corresponds With Choice)
 
 


And the things I mentioned earlier.

Don't worry guys I fixed it :)
Topic archived. No new replies allowed.
Pages: 12