Why doesn't this code print?

Pages: 12
Aug 25, 2014 at 5:48pm
Write your question here.
Hey guys, when I choose option 7 in my program it doesn't print. Can anyone 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
148
149
150
151
152
153
154
155
156
157
158
// 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 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++)
	{
		Product p = products[x];
        cout<< p.getProductId();
		cout<< p.getProductName ();
		cout<< p.getBuyPrice ();
		cout<< p.getSellPrice ();
		cout<< p.getSoldQty ();
	}
}

int _tmain(int argc, _TCHAR* 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:
			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;
			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;
  
}
  
  
}
Last edited on Aug 27, 2014 at 3:25am
Aug 25, 2014 at 6:40pm
Have you added items to the "inventory" first?

Also, why does your "product" class manage the "product" and the "inventory" shouldn't they be two distinct classes?
Last edited on Aug 25, 2014 at 6:41pm
Aug 25, 2014 at 8:14pm
I think this topic will be helpful: http://www.cplusplus.com/doc/tutorial/basic_io/
Aug 25, 2014 at 9:55pm
how will basic io help me? And yes there already is products in inventory.
Aug 26, 2014 at 6:12pm
someone?

Aug 26, 2014 at 6:21pm
And yes there already is products in inventory.

No there aren't. You exit main right after your switch statement so there is no loop and no way that case 5 and case 7 both execute in the same instance. The contents of memory are destroyed once your program exits.
Aug 26, 2014 at 6:33pm
Oh I'm really sorry...I didn't realize you had your return inside the while loop. Move line 145 after the brace on line 146.
Aug 27, 2014 at 3:20am
I did that and now it's giving me this error:
1>c:\users\ashish\desktop\neels programming folder\store\store\store.cpp(145): error C2360: initialization of 'p' is skipped by 'case' label
1> c:\users\ashish\desktop\neels programming folder\store\store\store.cpp(142) : see declaration of 'p'
Aug 27, 2014 at 3:22am
Would you please show the code you have currently?
Aug 27, 2014 at 3:26am
code updated
Aug 27, 2014 at 3:32am
Did you see it?
Aug 27, 2014 at 3:41am
Ah, I overlooked earlier. The thing is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
		case 5:
			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;
			Product p (id,name,bprice,sellprice,qty);
			ucStore.addProduct(p);
			break;
		case 1:
		    ucStore.displayInv();
			cout<< "What would you like to buy";
			cin>> product;


You didn't put braces around the object created in a certain case so it will exist in the following cases though it will not be initialized and probably contain garbage information.

Just explicitly tell it you want it only in that case
1
2
3
4
{
    Product p (id,name,bprice,sellprice,qty);
    ucStore.addProduct(p);
}


This may help you understand better http://stackoverflow.com/questions/5685471/error-jump-to-case-label

Also, why are your cases in descending instead of ascending order? :P
Last edited on Aug 27, 2014 at 3:43am
Aug 27, 2014 at 3:46am
Thanks, that helped but can you explain to me why option 7 doesn't work?
Aug 28, 2014 at 1:54am
Is anyone on?
Aug 28, 2014 at 1:55am
Well, you are very vague. What exactly do you mean by "option 7 doesn't work?"
Aug 28, 2014 at 11:08pm
When I select option 7 it just goes back to the main menu.
Aug 29, 2014 at 12:27am
Put the main menu in a function then and call it when the user enters '7'. There is no reason to over analyse this one.
Sep 1, 2014 at 11:48pm
But my program is supposed to add new items to the displaymenu as they are added.
Sep 2, 2014 at 7:32am
are you actually using a debugger? If you step through the code you'd get a better understanding of what's going on.
Sep 2, 2014 at 11:19pm
I Have no idea how to use a debugger
Pages: 12