Make x value depending on the number of words

Alright, so I don't really know how to explain this, so I'll do my best; I want the user to write some words separated by a comma and a space, and then I want to create a variable for each word, how do I do that? Using getline?

Should I keep the values in a vector, an array or just as separate value? Thanks!
Last edited on
so do I just need this code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
std::vector<std::string> getNextLineAndSplitIntoTokens(std::istream& str)
{
    std::vector<std::string>   result;
    std::string                line;
    std::getline(str,line);

    std::stringstream          lineStream(line);
    std::string                cell;

    while(std::getline(lineStream,cell,','))
    {
        result.push_back(cell);
    }
    return result;
} 


and if so, how does it read the line? meaning that do I need to make a string with what the user wrote? Or how? Thank you again!
It was just meant to give you a hint.

Here's another bigger hint :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<iostream>
#include<vector>
#include<sstream>

using namespace std;
int main()
{
	

	std::string line("This,is,my,line,antelope,again");

	std::stringstream  lineStream(line);

	std::string cell("");
	
	// collect all the strings
	std::vector<std::string> result;
	while (std::getline(lineStream, cell, ','))
	{
		result.push_back(cell);
	}

	return 0;
}



Should I keep the values in a vector, an array or just as separate value?


I've used a vector, but it really depends what you want to do with these individual strings.
Last edited on
Perfect!! I already solved my main problem with this super tip :D

Thank you very much! Hope some day I can learn enough code to stop asking so many questions, and maybe start helping people :)

Have a great day!
No worries.
Asking questions is good :)
As you've helped me here, maybe you could help me with this: http://www.cplusplus.com/forum/beginner/146941/
I just have. I hope :)
Topic archived. No new replies allowed.