Help with last function please

hey guys I just need help clearing up my errors in my last function thanks!

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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;

class Product {
public:
Product();
Product(string newName, double newPrice, int getQuantity);
string getName() const;
double getPrice() const;
double getQuantity() const;
void setName(string newName);
void setPrice(double newPrice);
void setQuantity(int newQuantity);
double getValue() const;
void print()const;
void read();

private:
string name;
double price;
int quantity;
};

Product::Product() {
name = "Unknown";
price = 0.0;
quantity = 0;
}

Product::Product(string newName, double newPrice, int getQuantity) {
name = newName;
price = newPrice;
quantity = getQuantity;
}

string Product::getName() const {
return name;
}

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

double Product::getQuantity() const {
return quantity;
}

void Product::setName(string newName) {
name = newName;
}

void Product::setPrice(double newPrice) {
price = newPrice;
}

void Product::setQuantity(int newQuantity) {
quantity = newQuantity;
}

void Product::read() {
cout << "Enter the name of the product: ";
cin >> ws;
getline(cin, name);
cout << "Enter the price for a " << name << ": ";
cin >> price;
cout << "Enter the quantity of the " << name << ": ";
cin >> quantity;
}


double Product::getValue() const {
return price * quantity;
}

void Product::print() const {
cout << name << setw(17 - name.length()) << price << setw(8) << quantity << setw(13) << getValue() << endl;
}

// Add a new product to the store
void addProduct(vector<Product>& store);

// List the products in the store
void listProducts(vector<Product>& store);

// Delete a product in the store
void deleteProducts(vector<Product>& store);

// Sell a product in the store
void sellProducts(vector<Product>& store);

// Restock a product in the store
void restockProducts(vector<Product>& store);


// For testing

int main() {
const int SIZE = 3;
vector<Product> store(SIZE);
store[0] = Product("Surboard", 560.00, 100);
store[1] = Product("Leash", 20.00, 150);
store[2] = Product("Wax", 4.00, 200);

int choice = 1;
while (choice != 0) {
cout << "\n0. Exit program\n"
<< "1. Report inventory\n"
<< "2. Add a new product\n"
<< "3. Delete a product\n"
<< "4. Sell a product\n"
<< "5. Restock a product\n"
<< "Choice (0-5): ";
cin >> choice;
cin.ignore(1000, '\n');
if (choice == 1) {
listProducts(store);
} else if (choice == 2) {
addProduct(store);
} else if (choice == 3) {
deleteProducts(store);
} else if (choice == 4) {
sellProducts(store);
} else if (choice == 5) {
restockProducts(store);
} else if (choice != 0) {
cout << "\nInvalid choice!\n";
}
}
cout << "\nGoodbye!\n";

return 0;
}

void addProduct(vector<Product>& store) {
cout << "\nAdding a new product:\n";
Product prod;
prod.read();
store.push_back(prod);
}

void listProducts(vector<Product>& store) {
cout << "\nListing products:\n"
<< "# Name: Price: Quantity: Value:\n";
for (unsigned num = 0; num < store.size(); num++) {
cout << (num + 1) << " ";
store[num].print(); // call print() in Product
}
}

void deleteProducts(vector<Product>& store) {
cout << "\nDeleting a product:\n"
<< "# Name: Price: Quantity: Value:\n";
Product prod;
for (unsigned num = 0; num < store.size(); num++) {
cout << (num + 1) << " ";
store[num].print(); // call print() in Product
}
cout << "Enter the number you wish to delete: ";
int pos;
cin >> pos;
for (unsigned i = pos; i < store.size() - 1; i++) {
store[i] = store[i + 1];
}
store.pop_back();
}

void sellProducts(vector<Product>& store){
cout << "\nSelling a product:\n"
<< "# Name: Price: Quantity: Value:\n";
Product prod;
for (unsigned num = 0; num < store.size(); num++) {
cout << (num + 1) << " ";
store[num].print(); // call print() in Product
}
cout << "Enter the product number that you want to sell: ";
int position;
cin >> position;
Product temp = store[position];
cout << "Enter how many of the product you sold: ";
int num1;
cin >> num1;// Also getting an error code here asking for int
prod.getQuantity() = num1 - prod.setQuantity(int newQuantity);
store[position] = temp;
}

void restockProducts(vector<Product>& store){
cout << "\nRestocking a product:\n"
<< "# Name: Price: Quantity: Value:\n";
Product prod;
for (unsigned num = 0; num < store.size(); num++) {
cout << (num + 1) << " ";
store[num].print(); // call print() in Product
}
cout << "Enter the product number that you want to restock: ";
int position;
cin >> position;
Product temp = store[position];
cout << "Enter how many of the product you want to add: ";
int num1;
cin >> num1;
prod.getQuantity() = num1 + prod.setQuantity(int newQuantity);// i know i figured this before but it didn't save correctly
store[position] = temp;
}
Can you state the error/problem?

Aceix.
Last edited on
185: error: expected primary-expression before "int"
In function `void restockProducts(std::vector<Product, std::allocator<Product> >&)':
204: error: expected primary-expression before "int""

Here you go sorry bout that thanx
I do not actually see anything error-like in the code. But declaring the variables at the start of the funtion is a good idea.

Aceix.
thanx I thought it looked okay too but I remember I had this problem before, so just declare all variables right under function decleration?
If I remember its an assignment issue

prod.getQuantity() = amount - prod.setQuantity(int newQuantity);// it doesn't like me assigning an int to a function.
You cannot declare a variable and pass it to a function in one go. Declare the variable on the line before and then pass just the variable -- i.e. without "int."
hey thanks again guys!! yeah I tried that and it it still giving the same error, I think maybe I'm not sending it to the right parameter maybe??
I tested out your program with the fix I mentioned. The new error is not the same.
Consider the following line:
 
prod.getQuantity() = num1 - prod.setQuantity(newQuantity);


According to your function definition, setQuantity is a void function. Therefore, you attempted to do this:
 
double = int - void  //Wut 


Another problem I see is that you attempt to assign value to the value returned from getQuantity(). You can visualize the effect of your instruction like this:
1
2
//if quantity == 1
1 = int - void //doubly wut 


I believe you meant to create a mutator function:
1
2
3
4
5
6
int&   //Return a reference so you can modify the variable
    Product::getQuantity(){ //Removed "const" because this is not a  read-only function
   return quantity;
}
//...Then the instruction translates to...
quantity = int + void


Edit: changed "double&" to "int&" because quantity is int which does not match getQuantity's original return type.
Last edited on
Topic archived. No new replies allowed.