Vector of objects not saving data members

I'm playing with classes, why won't my viewInventory() function produce the values that I've added to the objects in the vector m using the addInventory() function? viewInventory() doesn't even enter the for loop. Focus starting line 90.

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
#include "stdafx.h"
#include <string>
#include <vector>
#include <iostream>
using namespace std;


class Machine {
public:
	Machine(string manufac, string mdl, double p, int q)
	{
		manufacturer = manufac;
		model = mdl;
		price = p;
		quantity = q;
	}
	void setManufacturer(string manufac)
	{
		manufacturer = manufac;
	}
	void setModel(string mdl)
	{
		model = mdl;
	}
	void setPrice(double p)
	{
		price = p;
	}
	void setQuantity(int q)
	{
		quantity = q;
	}

	string getManufacturer(void)
	{
		return manufacturer;
	}
	string getModel(void)
	{
		return model;
	}
	double getPrice(void)
	{
		return price;
	}
	double getPrice(int q)
	{
		return price * q;
	}
	int getQuantity(void)
	{
		return quantity;
	}
private:
	string manufacturer;
	string model;
	double price;
	int quantity;
};
void addInventory(vector<Machine>& m)
{
	int mIndex = 0;
	char resp = 'Y';
	string manufac;
	string mdl;
	double p;
	int q;
	while (resp == 'Y' || resp == 'y') {
		cout << "Add Inventory\n--------------\n\n";
		cout << "Please enter item's manufacturer: ";
		getline(cin, manufac);
		cout << "Please enter item's model name: ";
		getline(cin, mdl);
		cout << "Please enter item's price: ";
		cin >> p;
		cout << "Please enter quantity available of this item: ";
		cin >> q;
		cin.ignore();
		Machine mTemp(manufac, mdl, p, q);
		m.push_back(mTemp);
		cout << "\n\nWould you like to add another item? (Y or N): ";
		mIndex++;
		cin >> resp;
	}
}

void viewInventory(vector<Machine>& m)
{
	cout << "View Machine Inventory\n------------------------\n\n";
	for (unsigned int i=0; i<m.size(); i++)
	{
		cout << "Machine Item # " << i << "\n---------------------\n";
		cout << "Manufacturer: " << m[i].getManufacturer() << endl;
		cout << "Model: " << m[i].getModel() << endl;
		cout << "Price: " << m[i].getPrice() << endl;
		cout << "Quantity: " << m[i].getQuantity() << endl;
	}
}

int main()
{
	int exit = 0;
	while (true) {
		vector<Machine> m;
		int menuUserInput;
		cout << "Construction Inventory\n\n";
		cout << "Select an option\n-------------------\n";
		cout << "1.) Add item to inventory\n";
		cout << "2.) Search item in inventory\n";
		cout << "3.) View all items in inventory\n";
		cout << "4.) Exit\n\n";
		cin >> menuUserInput;
		cin.ignore();
		switch (menuUserInput) {
			case 1:
				addInventory(m);
				break;
			/* case 2:
				searchInventory(m);
				break; */
			case 3:
				viewInventory(m);
				break;
			case 4:
				exit = 1;
				break;
			default:
				cout << "You didn't enter a valid option. Please try again.\n\n";
				break;
		}
		if (exit == 1) {
			break;
		}
	}
}
Last edited on
m is created on line 104 and goes out of scope on line 134 so in each iteration of the loop you will use a new vector named m. Define m outside the loop if you want to use the same vector throughout the whole loop.
Last edited on
oops. Thank you peter.
Topic archived. No new replies allowed.