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;

}

Dani AI

Generated

Short answer for : keep the inventory inside the ship object (as suggested) and provide controlled accessors. Either a print/list method you call when you need output, or an accessor that returns a const reference to the member vector so callers can read without copying. Never return a reference to a local vector from a function — that is undefined behavior.

A few practical improvements and gotchas:

  • Use std::string for the ship name instead of a raw char[] and strcpy. Read input with std::getline and accept parameters as const std::string&.
  • Make data members private and methods that do not modify state const.
  • Use std::size_t for sizes/indices and prefer range-based for loops. For single-item access, check bounds or use vector::at() to get an exception on bad access. (Side note: in 's listInventoryNumber example the code prints inventory[i] while the parameter is location — use inventory[location] and check bounds first.)

Example (modern, concise API):

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

class Ship {
private:
    std::string name;
    std::vector<std::string> inventory;
public:
    explicit Ship(const std::string& n = {}) : name(n) {}
    void setName(const std::string& n) { name = n; }
    void addItems(std::initializer_list<std::string> items) {
        for (const auto& s : items) inventory.emplace_back(s);
    }
    const std::vector<std::string>& getInventory() const noexcept { return inventory; }
    void listInventory() const {
        for (const auto& s : inventory) std::cout << s << '\n';
    }
};

Quick troubleshooting: if you see out_of_range or garbage when printing inventory[0], the vector is empty — check empty() or size() first. Returning a vector by value is also fine (RVO/move semantics avoid cost on modern compilers) but return a const-ref if you need zero-copy reads and you can guarantee the object still exists.

Recommended Answers

All 3 Replies

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.