We can define a data structure to represent that: typedef std::vector<std::string> full_name_type;
The first name would be entry[0], and the last would be entry[ entry.size() - 1].
You don't really want a readFile function, you want a readLine that returns this array of names that you can print.
Sort of, The crux of the problem is that you need to read a line like Jonh Mccar Allen Peterson and not confuse it with other lines.
For example, if you write code like:
1 2 3 4 5 6
while (input_file_stream)
{
std::string name;
input_file_stream >> name;
std::cout << name << std::endl;
}
then you don't know when you've finished one name and starting on the next. So you need to read an entire line and then parse that seperately.
You can use an input stream that is sourced from the string you've read and parse it in the same way. So your code would look more like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
while (input_file_stream)
{
std::string name;
input_file_stream >> name; // read an entire line into name
std::vector<std::string> parsed_name; // an array for the parts of the name
std::istringstream input_str_stream(name); // an input stream based on the full name
while (input_str_stream) // keep reading while there are parts of the name to read
{
std::string str; // a temporary string to read the name part into
input_str_stream >> str; // read the name part
parsed_name.push_back(str); // add it to the arrat for the parts of the name
}
if (parsed_name.size() > 0)
{
// print the first and last entries in parsed_name
}
}