storing vectors and functions passing

hi im trying out this code where i have to store two vectors info from 2 diffrent txt files including ',' and ';'and this has to be in the function so i can call it back in an other function please i need help ive been trying to working on this for weeks .the parameter has to be the file names too

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
 vector<string> createLists(string storyConnectors,string storyQuestions)
{ 
	string lines;
	vector<string>storyConnectorss;
	ifstream connectors(storyConnectors);
	
	while (getline(connectors,lines, ';'))
	{ //reads one string 
		storyConnectorss.push_back(lines);
	}//add it to data vector
	connectors.close();
	
	string lines2;
	vector<string>storyQuestionss;
	ifstream storyQuestionsss(storyQuestions);
    while (getline(storyQuestionsss,lines2,','))
	{ 
		storyQuestionss.push_back(lines2);

	}
	storyQuestionsss.close();
	
	
	

	// return both vectors
	//ques and connectors
	return storyConnectorss , storyQuestionss;//the other vecotr
}
A function can return only 1 value;
One option would be to create a struct that contains 2 vectors or use std::pair.
The other option is to make the function more generic:
1
2
3
4
5
vector<string> createList( const string& filename, char separator );

// used:
vector<string> connectors = createList( "something", ';' );
vector<string> questions  = createList( "other", ',' );
Last edited on
Why not pass the delimiters instead?
1
2
3
4
5
6
7
8
9
10
vector<string> createList(string filename, char delim) {
    string lines;
    vector<string>result;
    ifstream fin(filename);
    while (getline(fin, lines, delim))
    {
        result.push_back(lines);
    }
    return result;
}


Then you can say in the function that would have called what you wrote
1
2
vector<string> storyConnectorss = createList(storyConnectors,';');
vector<string> storyQuestionss = createList(storyQuestions,',');
it has to be in a one function thats why :( any way thanks for taking your time to help me if you can i can show you the reqviurement and get an small idea

* task is to read and store the story elements, from one (or more) text files, and store this data in appropriate variables. This data will be used to generate the randomised story lines in the next task. Create a function called createLists() and call it from the runMenu() function, when option [3] Read and Store Data from File is selected. This function must accept a filename as a parameter (“*.txt”) and successfully read and store the data. The data is to be stored in two collection variables named storyConnectors, and storyQuestions. You can choose to read the data from a single file, storing each data set in the different variables, OR read and store each data set with three separate calls to the same function with different arguments OR use another suitable method of your own devising*
separate calls to the same function with different arguments OR use another suitable method of your own devising

That looks like the very opposite of "has to be".
the thing is he doesnt allow us to use the code we havent learnt yet :(
You've learned all the code that keskiverto and salemc propose. Look at Salem C's answer again. What doesn't make sense to you?
the part where delim comes as salemC mentioned and the part( const string& filename, char separator ) what does the & do
The & means that the parameter 'filename' is passed by reference to the function. The 'filename' is a reference, not a copy.

That is optional. The function can be vector<string> createList( string filename, char separator ); if that is more comfy. (Just like salem c did made it.)

What in the 'delim' does not make sense?
vector<string> createLists(string storyConnectors,string storyQuestions)
{
storyConnectors="storyConnectors.txt";
string lines;
vector<string>storyConnectorss;
ifstream connectors(storyConnectors);

while (getline(connectors,lines, ';'))
{ //reads one string
storyConnectorss.push_back(lines);
}//add it to data vector

//for (unsigned int i = 0; i < storyConnectorss.size(); i++)
//{
//cout << storyConnectorss[i] << endl;
//}

connectors.close();



storyQuestions = "storyQuestions.txt";
string lines2;
vector<string>storyQuestionss;
ifstream storyQuestionsss(storyQuestions);
while (getline(storyQuestionsss,lines2,','))
{
storyQuestionss.push_back(lines2);
}
storyQuestionsss.close();

//for (unsigned int i = 0; i < storyQuestionss.size(); i++)
//{
// cout << storyQuestionss[i] << " " << endl;
//}

return storyConnectorss,storyQuestionss;

okay in here can you tell me where i should change? and i called out the function by changing the position of return i get the out put of the return after the , i need the out put of both im sorry if this is annoying you :( but is really hard for me cus i have no background when it comes to programming languages
Please use the code tags - https://www.cplusplus.com/articles/jEywvCM9/

> the thing is he doesnt allow us to use the code we havent learnt yet :(
You're never going to learn much if you're stuck with a snail pace teacher

1
2
3
4
5
6
7
// one vector is returned, and the other is stored through a reference parameter.
vector<string> createLists(string storyConnectors,string storyQuestions, vector<string> & storyConnectorss) {
  // your code
  // Don't declare a local storyConnectorss, it's already provided to you as a parameter.
 
  return storyQuestionss;
}


Or (since you're not going to be using std::pair as suggested by Learner2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct pair_of_vectors {
    vector<string>storyConnectorss;
    vector<string>storyQuestionss;
};

pair_of_vectors createLists(string storyConnectors,string storyQuestions ) {
   // your code

  // make a return result pair
  pair_of_vectors pair;
  pair.storyConnectorss = storyConnectorss;
  pair.storyQuestionss = storyQuestionss;
  return pair;
}


Since we're not on your course, it's hard to guess what your syllabus is, what you do know (or what you should know).
Topic archived. No new replies allowed.