So i have a program i am writing,and i have come to problem. I want to be able to pass a set of three strings into a function, which should then take them and add them to a Vector. All good so far, now is where my problem develops, how do i get that vector out of the function. I want to able to able to have a separate function with in the same class, that will print all three items in the vector. i want it to be a separate Function so i can call when i want

#include <iostream>
#include <string>
#include <vector>
#include <cstring> 

using namespace std;

class ship 
{
	char Name[256];


public:
	void setName(char *nam)
	{
		strcpy(Name, nam);
	cout << Name;
	}
	
	vector<string> inven(string frt, string sec, string thir)
	{
	
		vector<string> inventor;
		inventor.push_back (frt);
		inventor.push_back (sec);
		inventor.push_back (thir);
		cout << inventor[0]; 
		return inventor; 
		
	}
};

int main()
{
	
	char shipcall[256];
	ship player;
	vector<string> main_inventor; 
	cout << "welcome to space \n";
	string frt = "Credits";
	string sec = "cargo";
	string thir = "Armaments";
	
	cout << " Name your ship\n";

	cin.get(shipcall,156);
	


	player.setName(shipcall);

	player.inven(frt, sec,thir);
	
	
	cout << main_inventor[0]; 
	

	system ("pause");
	return 0;

}

Allow me to make some adjustments.

#include <iostream>
#include <string>
#include <vector>
#include <cstring> 
#include <cstdlib>

using namespace std;

class ship 
{
	char Name[256];
	vector<string> inventory;


public:
	void setName(char *nam)
	{
		strcpy(Name, nam);
	cout << Name;
	}
	
	void addToInventory(string frt, string sec, string thir)
	{
		inventory.push_back (frt);
		inventory.push_back (sec);
		inventory.push_back (thir);
		cout << inventory[0]; //Is this really needed?
	}
	void listInventory()
	{
		for(int i=0;i<inventory.size();i++)
		{
			cout << inventory[i]; 
		}
	}
};

int main()
{
	
	char shipcall[256];
	ship player;
	cout << "welcome to space \n";
	string frt = "Credits";
	string sec = "cargo";
	string thir = "Armaments";
	
	cout << " Name your ship\n";

	cin.get(shipcall,156);
	
	player.setName(shipcall);

	player.addToInventory(frt, sec,thir);
	
	player.listInventory();

	system ("pause");
	return 0;

}
class ship 
{
private: // <====================
	char Name[256];
	vector<string> inventory;


public:
	void setName(char *nam)
	{
		strcpy(Name, nam);
	cout << Name;
	}
	
	void addToInventory(string frt, string sec, string thir)
	{
		inventory.push_back (frt);
		inventory.push_back (sec);
		inventory.push_back (thir);
		cout << inventory[0]; //Is this really needed?
	}
	void listInventory()
	{
		for(int i=0;i<inventory.size();i++)
		{
			cout << inventory[i]; 
		}
	}
};

is good, however, use the private: for the variables you are using

also use

void listInventoryNumber(int location)
{
if(location < inventory.size() && location >= 0 )
       cout << inventory[i]; 
else
       cout << "invalide Location " << location << " location must be 0 - " << inventory.size(); 
}

sorry I missed the code formatter,

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.