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

Your problem isn't that the new dvd isn't be added. The problem is that when you initialize the vector to 5 dvd objects the default constructor creates 5 blank dvd objects. So when you push_back one it's number 6. Try using an empty vector and setting a limit when you add new ones.

Try this to append the file: ofstream outputFile("dvds.txt",ios_base::app); // Create dvds.txt

On a side note, it's very easy to get yourself confused when you use the same name for 2 different but concurrent objects. You use i for the loop then in the loop you use i for the new dvd object. Try naming it something meaningful like newdvd

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.