   Functions!
  Hello,I just started learning functions again (from a book), and I have a problem returning a vector. When i try and print out the results of the input from the vector, it doesnt print anythingHeres my code:#include <iostream>#include <string>#include <vector>#include <algorithm>using namespace std;// Prototypesvoid welcome();string name();string address();string phoneNum();string email();string previous_jobs();string hobbies();void exit();int main(){   welcome();   string aName = name();    string aAddress = address();    string aPhone = phoneNum();    string aEmail = email();    string aJobs = previous_jobs();    cout << "Previous Jobs: " << aJobs;   exit();   return 0;    }void welcome(){    cout << "Welcome to theLogitech Job Application!\n";}string name(){    string personsName;   cout << "\nPlease enter the following Information:\n\n";    cout << "Name: ";    cin >> personsName;   return personsName;}string address(){    string personsAddress;   cout << "\nAddress: ";    cin >> personsAddress;   return personsAddress;}string phoneNum(){    string personsNum;   cout << "\nPhone Number: ";    cin >> personsNum;   return personsNum;}string email(){    string personsEmail;   cout << "\nEmail: ";    cin >> personsEmail;   return personsEmail;}string previous_jobs(){    string pJobs;    char more;    int nJobs;   cout << "\nNumber of Previous Jobs (0 if none): ";    cin >> nJobs;   vector<string> jobs(nJobs);    vector<string>::const_iterator cIter;   if(nJobs > 0)    {   for(int i = 0; i != nJobs; i++)   { cout << "Job " << i+1 << ": "; cin >> pJobs; jobs.push_back(pJobs); return jobs;   }}}void exit(){    cout << "\nThank you for Applying!\n\n";    system("PAUSE");}-Thanks for any help.   
  I don't see anywhere in your code a place where you attempt to return a vector.   ;  the vector jobs is scoped local to the function (it's defined inside the function).  When the function ends the vector is destroyed.  So it won't remember previous jobs because each time you call that function and it exits, the history is erased.  If you want data to survive beyond the funciton local scope you'll have to define the variable at the global scope (i.e. inside main or something) and pass it as an argument to that function.When passing vectors to functions be sure to pass by reference rather than by value:bad:void myFunction( std::vector<std::string> aVector);good:void myFunction( std::vector<std::string> &aVector);the "bad" version will copy the data to a new vector that dies when the function ends (thus erasing history and using 2x the memory).  the "good" version will use the same vector inside the function and thus the data will not die when the function exits.-me   ;Quote:Original post by Palidinethe vector jobs is scoped local to the function (it's defined inside the function).  When the function ends the vector is destroyed.  So it won't remember previous jobs because each time you call that function and it exits, the history is erased.  If you want data to survive beyond the funciton local scope you'll have to define the variable at the global scope (i.e. inside main or something) and pass it as an argument to that function.Well, that would be the case if he was returning a pointer to it. Just returning the vector will create a copy IIRC, so that should be fine. The problem is, he's not returning the vector, he's returning the first element:   for(int i = 0; i != nJobs; i++)   { cout << "Job " << i+1 << ": "; cin >> pJobs; jobs.push_back(pJobs); return jobs;   }This should probably be:   for(int i = 0; i != nJobs; i++)   { cout << "Job " << i+1 << ": "; cin >> pJobs; jobs.push_back(pJobs);   }   return jobs;;Quote:Original post by bytecoderQuote:Original post by Palidinethe vector jobs is scoped local to the function (it's defined inside the function).  When the function ends the vector is destroyed.  So it won't remember previous jobs because each time you call that function and it exits, the history is erased.  If you want data to survive beyond the funciton local scope you'll have to define the variable at the global scope (i.e. inside main or something) and pass it as an argument to that function.Well, that would be the case if he was returning a pointer to it. Just returning the vector will create a copy IIRC, so that should be fine. The problem is, he's not returning the vector, he's returning the first element:   for(int i = 0; i != nJobs; i++)   { cout << "Job " << i+1 << ": "; cin >> pJobs; jobs.push_back(pJobs); return jobs;   }This should probably be:   for(int i = 0; i != nJobs; i++)   { cout << "Job " << i+1 << ": "; cin >> pJobs; jobs.push_back(pJobs);   }   return jobs;I tried that, but i generated an error. After that i tried "return jobs" and it worked, but only returned the first element. I know why, but i cannot find a way to return all of it. The way Palidine works, so thats the way im doing it now, but please tell me how to return a vector and the elements in it without using reference, if its possible.;Quote:Original post by NUCLEAR RABBITQuote:Original post by bytecoderQuote:Original post by Palidinethe vector jobs is scoped local to the function (it's defined inside the function).  When the function ends the vector is destroyed.  So it won't remember previous jobs because each time you call that function and it exits, the history is erased.  If you want data to survive beyond the funciton local scope you'll have to define the variable at the global scope (i.e. inside main or something) and pass it as an argument to that function.Well, that would be the case if he was returning a pointer to it. Just returning the vector will create a copy IIRC, so that should be fine. The problem is, he's not returning the vector, he's returning the first element:   for(int i = 0; i != nJobs; i++)   { cout << "Job " << i+1 << ": "; cin >> pJobs; jobs.push_back(pJobs); return jobs;   }This should probably be:   for(int i = 0; i != nJobs; i++)   { cout << "Job " << i+1 << ": "; cin >> pJobs; jobs.push_back(pJobs);   }   return jobs;I tried that, but i generated an error. After that i tried "return jobs" and it worked, but only returned the first element. I know why, but i cannot find a way to return all of it. The way Palidine works, so thats the way im doing it now, but please tell me how to return a vector and the elements in it without using reference, if its possible.It gave an error because your return type has to match the type of the thing that you return. That is, if you want to return a vector of string, you have to write the function in such a way that it says it returns a vector of string; and then you will want to assign the result of the function call to a vector of string.//...vector<string> aJobs = previous_jobs();//...vector<string> previous_jobs() {  // as before, but with the other noted corrections}Remember, a function only returns once per call.