954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Vectors, functions, class, help

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;

}
travelingt93
Newbie Poster
1 post since Feb 2012
Reputation Points: 10
Solved Threads: 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;

}
DeanMSands3
Junior Poster
187 posts since Jan 2012
Reputation Points: 37
Solved Threads: 27
 
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(); 
}
Ab000dy_85
Junior Poster in Training
56 posts since Dec 2007
Reputation Points: 12
Solved Threads: 5
 

sorry I missed the code formatter,

Ab000dy_85
Junior Poster in Training
56 posts since Dec 2007
Reputation Points: 12
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: