Vector of objects not updating outside of function

What I'm trying to do is initialize a vector of objects, pass the vector to a function, then initialize a new object with a constructor. After setting the object members in the function, I add that object to the vector. The problem is the member functions aren't updating outside of the function.

menu function
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
//Function prototypes
void menu();
void addDVD(int, vector<DVD>&);

int main()
{
	// Call menu function
	menu();

	//TODO: DVD List Class has methods to add DVD, remove DVD, update DVD
}

void menu()
{
	static int numDVD;	// Number of DVDs user wants to add
	string	strNum;

	// Create vector of DVD objects
	vector<DVD> dvdVector(5);

		cout << endl << "How many DVDs would you like to add? (no more than 5): ";
		getline(cin, strNum);
		numDVD = atoi(strNum.c_str());
		addDVD(numDVD, dvdVector);	// Call addDVD function
		cout << "DVD: " << dvdVector[0].getTitle() << endl; // Returns empty like the default constructor
}


addDVD function
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
void addDVD(int numDVD, vector<DVD>& dvdVector)
{
	string title,	// DVD title
		actorOne,	// Name of actor one
		actorTwo,	// Name of actor two
		strLength,	// Length of DVD (in string for getline)
		strYear;	// Year of DVD (in string for getline)
	int length,		// Length of DVD
		year,		// Year of DVD
		i;			// Loop counter

	// TODO: Make it so file is appended instead of overwritten
	ofstream outputFile("dvds.txt"); // Create dvds.txt

	for(i = 0; i < numDVD; i++)
	{
	cout << "DVD #" << i+1 << endl
	<< "----------------" << endl;

	cout << "DVD title: ";
	getline(cin, title);
	outputFile << title << endl;

	cout << "Year: ";
	getline(cin, strYear);
		year = atoi(strYear.c_str());
		outputFile << year << endl;
			
	cout << "Length (in minutes): ";
	getline(cin, strLength);
		length = atoi(strLength.c_str());
		outputFile << length << endl;

	cout << "Main Actor: ";
	getline(cin, actorOne);
	outputFile << actorOne << endl;

	cout << "Supporting Actor: ";
	getline(cin, actorTwo);
	outputFile << actorTwo << endl;
	cout << endl;
	
	DVD i(title, year, length, actorOne, actorTwo); 

	i.setTitle(title);
	i.setYear(year);
	i.setLength(length);
	i.setActorOne(actorOne);
	i.setActorTwo(actorTwo);
	dvdVector.push_back(i);
	}

	// Close file
	outputFile.close();

	// Return to menu
	menu();
}


DVD.cpp
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
// Implementation file for DVD class

#include "DVD.h"	// Include DVD class
#include <iostream>

//**********************************************************
//	Default constructor
//**********************************************************

DVD::DVD()
{
	title = "";
	length = 0;
	year = 0;
	actorOne = "";
	actorTwo = "";
}


//*******************************************************************************
//	Constructor accepts args for title, year, length, and actors
//******************************************************************************
DVD::DVD(string t, int y, int l, string a1, string a2)
{
	title = t;
	year = y;
	length = l;
	actorOne = a1;
	actorTwo = a2;
}

//**********************************************************
//	Getters and setters for DVD title, year, length, and two main actors
//**********************************************************

void DVD::setTitle(string t)
{
	title = t;
}

void DVD::setActorOne(string a1)
{
	actorOne = a1;
}

void DVD::setActorTwo(string a2)
{
	actorTwo = a2;
}

void DVD::setLength(int l)
{
	length = l;
}

void DVD::setYear(int y)
{
	year = y;
}


string DVD::getTitle() const
{
	return title;
}

string DVD::getActorOne() const
{
	return actorOne;
}

string DVD::getActorTwo() const
{
	return actorTwo;
}

int DVD::getLength() const
{
	return length;
}

int DVD::getYear() const
{
	return year;
}


DVD.h
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
// The DVD class gets the DVD title, year, length, and two main actors

#ifndef DVD_H
#define DVD_H
#include <string>

using namespace std;

class DVD
{
private:
	string title,
		actorOne,
		actorTwo;
	int year,
		length;

public:
	DVD();	// Default constructor
	DVD(string, int, int, string, string);

	// Setters
	void setTitle(string);
	void setActorOne(string);
	void setActorTwo(string);
	void setLength(int);
	void setYear(int);
	
	// Getters
	string getTitle() const;
	string getActorOne() const;
	string getActorTwo() const;
	int getLength() const;
	int getYear() const;
};

#endif 
Last edited on
Arguments passed by value and by reference http://www.cplusplus.com/doc/tutorial/functions/
closed account (G30GNwbp)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Create vector of DVD objects
vector<DVD> dvdVector(5);

void menu(vector<DVD>& dvdVector )
{
	static int numDVD;	// Number of DVDs user wants to add
	string	strNum;



		cout << endl << "How many DVDs would you like to add? (no more than 5): ";
		getline(cin, strNum);
		numDVD = atoi(strNum.c_str());
		addDVD(numDVD, dvdVector);	// Call addDVD function
}
Thank you both for replying, but can you guys elaborate on your posts? I also updated the OP with some lines of code I forgot.
I hate to double post and bump so soon, but this program is due in 6 hours and I'm literally borderline failing this class. The professor is unavailable to help, and I don't know where else to turn. I'm not asking for someone to write this program for me, I just need help understanding why my member variables won't update outside of the function.
In menu, a new vector of DVDs is created. A reference to that vector is fed to addDVD, but, instead of returning it calls menu which creates a new vector of DVDs. A reference to that vector is fed to addDVD, but instead of returning, it calls menu which creates a new vector of DVDs...

Do you see where that's going?
Yes, I understand now that I must return the vector? The new code is below, but it still isn't working:

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
// Function prototypes
void menu();
vector<DVD> addDVD(int, vector<DVD>&);



vector<DVD> addDVD(int numDVD, vector<DVD>& dvdVector)
{
string title,	// DVD title
		actorOne,	// Name of actor one
		actorTwo,	// Name of actor two
		strLength,	// Length of DVD (in string for getline)
		strYear;	// Year of DVD (in string for getline)
	int length,		// Length of DVD
		year,		// Year of DVD
		i;			// Loop counter

	// TODO: Make it so file is appended instead of overwritten
	ofstream outputFile("dvds.txt"); // Create dvds.txt

	for(i = 0; i < numDVD; i++)
	{
	cout << "DVD #" << i+1 << endl
	<< "----------------" << endl;

	cout << "DVD title: ";
	getline(cin, title);
	outputFile << title << endl;

	cout << "Year: ";
	getline(cin, strYear);
		year = atoi(strYear.c_str());
		outputFile << year << endl;
			
	cout << "Length (in minutes): ";
	getline(cin, strLength);
		length = atoi(strLength.c_str());
		outputFile << length << endl;

	cout << "Main Actor: ";
	getline(cin, actorOne);
	outputFile << actorOne << endl;

	cout << "Supporting Actor: ";
	getline(cin, actorTwo);
	outputFile << actorTwo << endl;
	cout << endl;
	
	DVD i(title, year, length, actorOne, actorTwo); 

	i.setTitle(title);
	i.setYear(year);
	i.setLength(length);
	i.setActorOne(actorOne);
	i.setActorTwo(actorTwo);
	dvdVector.push_back(i);
	}

	// Close file
	outputFile.close();

	// Return to menu
	return dvdVector;
}
Yes, I understand now that I must return the vector?

No. What you must do is return to the caller at some point. Since you receive the vector by reference, any changes you make to it will persist in the original, so there is no need to return a copy of the vector.

And you probably don't want to create a vector of dvds inside menu that ceases existing when menu returns.
Don't return vector, and don't create vector inside menu. Okay. What about this?

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
// Rich Dunne
// Final: This program will allow the user to keep track of a DVD collection. The program provides a menu for the user to be able to add, delete, update and display the information in a DVD.

#include "DVD.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

// Function prototypes
void menu(vector<DVD>&);
void addDVD(int, vector<DVD>&);
void removeDVD();
void displayCollection(int, vector<DVD>& dvd);

int main()
{
	// Create vector of DVD objects called dvdVector
	// Add object to vector at end of addDVD
	vector<DVD> dvdVector(5);

	// Call menu function
	menu(dvdVector);

	//TODO: DVD List Class has methods to add DVD, remove DVD, update DVD
}

void menu(vector<DVD>& dvdVector)
{
	static int numDVD;	// Number of DVDs user wants to add
	string	strNum,
		strChoice;	// Menu choice
	int choice;

	// Dynamically allocate array of DVD objects
	// TODO: DON'T FORGET TO DELETE AT END OF PROGRAM
	//DVD *dvd = new DVD{DVD(title, year, length, actorOne, actorTwo)};
	
	// Create vector of DVD objects called dvdVector
	// Add object to vector at end of addDVD
	//vector<DVD> dvdVector(5);
	

	cout << "DVD Collection Main Menu" << endl
		<< "--------------------------" << endl
		<< "1. Add DVDs" << endl
		<< "2. Edit DVDs" << endl
		<< "3. Remove DVDs" << endl
		<< "4. Display Collection" << endl
		<< "5. Exit" << endl
		<< "Choose an option (1-5): ";
	getline(cin, strChoice);
	choice = atoi(strChoice.c_str());	// Convert string to int

	switch (choice)
	{
		case 1:
			cout << endl << "How many DVDs would you like to add? (no more than 5): ";
			getline(cin, strNum);
			numDVD = atoi(strNum.c_str());
				while(numDVD > 5)
				{
					cout << endl << "How many DVDs would you like to add? (no more than 5): ";
					getline(cin, strNum);
					numDVD = atoi(strNum.c_str());
				}
			addDVD(numDVD, dvdVector);	// Call addDVD function
			break;
		case 2:
			// TODO: Get collection, ask which DVD user would like to edit, call addDVD function
		case 3:
			// TODO: Get collection, ask which DVD user would like to delete, call removeDVD function
		case 4:
			// TODO: If no DVDs, error else
			//displayCollection(numDVD, dvd);
			cout << "DVD: " << dvdVector[0].getTitle() << endl;
			break;
		case 5:
			cout << endl << "Terminating program." << endl << endl;
			exit;
			break;
		default:
			exit;
	}
	cout << "DVD: " << dvdVector[0].getTitle() << endl;
}

void addDVD(int numDVD, vector<DVD>& dvdVector)
{
	string title,	// DVD title
		actorOne,	// Name of actor one
		actorTwo,	// Name of actor two
		strLength,	// Length of DVD (in string for getline)
		strYear;	// Year of DVD (in string for getline)
	int length,		// Length of DVD
		year,		// Year of DVD
		i;			// Loop counter

	// TODO: Make it so file is appended instead of overwritten
	ofstream outputFile("dvds.txt"); // Create dvds.txt

	for(i = 0; i < numDVD; i++)
	{
	cout << "DVD #" << i+1 << endl
	<< "----------------" << endl;

	cout << "DVD title: ";
	getline(cin, title);
	outputFile << title << endl;

	cout << "Year: ";
	getline(cin, strYear);
		year = atoi(strYear.c_str());
		outputFile << year << endl;
			
	cout << "Length (in minutes): ";
	getline(cin, strLength);
		length = atoi(strLength.c_str());
		outputFile << length << endl;

	cout << "Main Actor: ";
	getline(cin, actorOne);
	outputFile << actorOne << endl;

	cout << "Supporting Actor: ";
	getline(cin, actorTwo);
	outputFile << actorTwo << endl;
	cout << endl;
	
	// Create instance of DVD class named same as current i
	DVD i(title, year, length, actorOne, actorTwo); 

	i.setTitle(title);
	i.setYear(year);
	i.setLength(length);
	i.setActorOne(actorOne);
	i.setActorTwo(actorTwo);
	dvdVector.push_back(i);
	}

	// Close file
	outputFile.close();

	// Return to menu
	//menu();
}
Topic archived. No new replies allowed.