0

So to start off, the goal of this function below is to read each line in the text file (TitanData.txt). Each line has 3 pieces of information: Age, Passenger Class, and if the passenger survived (saved as "TRUE" or "FALSE") in order. I've created 3 vectors: vector Age, vector PassengerClass, vector Survived. The issue seems to be that the push_back functions aren't working properly. I've been looking for solutions for hours and I have yet to come across one so hopefully I can get an answer here.

The function is below:

void ReadFromFile(vector<int> Age, vector<string> PassengerClass, vector<bool> Survived)
{
    bool survived;
    int age;
    string Passenger_Class;

    string val1, val2, val3;
    ifstream infile;
    infile.open("TitanicData.txt");
    if (infile)
    {
        while (infile >> val1 >> val2 >> val3)
        {
            age = stoi(val1);
            Age.push_back(age);


            Passenger_Class = val2;
            PassengerClass.push_back(val2);


            if (val3 == "TRUE")
            {
                survived = true;
                Survived.push_back(true);
            }
            if (val3 == "FALSE")
            {
                survived = false;
                Survived.push_back(false);
            }
        }
        infile.close();
    }
}
  • Sorry if this seems a little strange but is this homework? It seems you are using data from the titanic and the only reason that would require you to do something like this appears as something you would be assigned as an assignment. I have no issue with homework questions but I think you would benefit more from someone giving an explanatory answer to help you instead of giving a straight answer if this is homework. Essentially I am asking you this to help you get the best answer. – The Grand J Nov 16 '20 at 03:26
  • 1
    This function takes parameters by value instead of by reference, and that's why your `push_back`s have no effect, once this function returns. See the duplicate question for more information. See your C++ textbook for examples of how to pass function parameters by reference. Ot if you haven't learned references yet, you will need to use pointers. – Sam Varshavchik Nov 16 '20 at 03:27

0 Answers0