9 Replies - 1600 Views - Last Post: 14 December 2013 - 06:25 PM Rate Topic: -----

#1 redunne2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-December 13

Vector of objects not updating outside of function

Posted 14 December 2013 - 04:27 PM

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
//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
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
// 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
// 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



Is This A Good Question/Topic? 0
  • +

Replies To: Vector of objects not updating outside of function

#2 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,939
  • Joined: 08-August 08

Re: Vector of objects not updating outside of function

Posted 14 December 2013 - 04:37 PM

//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


Does that compile??? You have cout outside of any function, including main().
Was This Post Helpful? 0
  • +
  • -

#3 redunne2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-December 13

Re: Vector of objects not updating outside of function

Posted 14 December 2013 - 04:43 PM

Oops, that line is actually inside the menu function. Must have copy/pasted wrong or something, but it's definitely inside the menu function. I would fix that if I knew how to edit posts.
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,939
  • Joined: 08-August 08

Re: Vector of objects not updating outside of function

Posted 14 December 2013 - 05:05 PM

Well, this is just wrong:
	// Return to menu
	menu();


Let's see your full code.
Was This Post Helpful? 0
  • +
  • -

#5 redunne2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-December 13

Re: Vector of objects not updating outside of function

Posted 14 December 2013 - 05:13 PM

I made some changes, namely returning the vector from addDVD.

// 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> addDVD(int, vector<DVD>&);
void removeDVD();
void displayCollection(int, vector<DVD>& 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,
		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:
			menu();
	}
	cout << "DVD: " << dvdVector[0].getTitle() << endl;
}

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;
	
	// 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
	return dvdVector;
}

void removeDVD()
{

}

void displayCollection(int numDVD, vector<DVD>& dvd)
{
	// TODO: Get output from text file
	/*
	string input;
	ifstream inputFile;
	inputFile.open("dvds.txt", ios::in);

	if (inputFile.is_open())
	{
			while(!inputFile.eof())
			{
					getline (inputFile,input);
					cout << input << endl;
			}
	}
	else 
		cout << "Error opening file" << endl;
	*/
	if(numDVD == 0)
	{
		cout << "There are no DVDs in your collection." << endl;
		menu();
	}
	else
	{
		for(int i = 0; i < numDVD; i++)
		{
		cout << "DVD #" << i+1 << endl
			<< "----------------" << endl
			<< "DVD: " << dvd[i].getTitle() << endl
			<< "Year: " << dvd[i].getYear() << endl
			<< "Length: " << dvd[i].getLength() << endl
			<< "Main Actor: " << dvd[i].getActorOne() << endl
			<< "Seconday Actor: " << dvd[i].getActorTwo() << endl << endl;
		}
		menu();
	}
	//inputFile.close();
}



Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,939
  • Joined: 08-August 08

Re: Vector of objects not updating outside of function

Posted 14 December 2013 - 05:21 PM

Ok, so that's main.cpp. Now show your other files exactly as you have them.
Was This Post Helpful? 0
  • +
  • -

#7 redunne2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-December 13

Re: Vector of objects not updating outside of function

Posted 14 December 2013 - 05:23 PM

DVD.cpp
// 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
// 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


Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,939
  • Joined: 08-August 08

Re: Vector of objects not updating outside of function

Posted 14 December 2013 - 05:37 PM

	// Add object to vector at end of addDVD
	vector<DVD> dvdVector(5);

Every time you call menu you create a new vector with 5 elements in it. You don't want to do that. Instead, you want to pass the vector to menu and then to addDVD() and only add an element with push_back().

This post has been edited by CTphpnwb: 14 December 2013 - 05:38 PM

Was This Post Helpful? 0
  • +
  • -

#9 redunne2   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-December 13

Re: Vector of objects not updating outside of function

Posted 14 December 2013 - 05:43 PM

I see. The program still isn't working, but am I on the right track with this?

// 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)};
	
	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();
}


Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3825
  • View blog
  • Posts: 13,939
  • Joined: 08-August 08

Re: Vector of objects not updating outside of function

Posted 14 December 2013 - 06:25 PM

This will initialize 5 elements of the vector with the default constructor, so what do you think element 0 will contain?
vector<DVD> dvdVector(5);

The first element you add will be the sixth element, or element 5.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1