string streams problem

hi.

i have a fill with a full person's name and I want to do a void funcion that prints on the screen only the first and last name.

I tried many ways (stringstream, sub strings, ...) but none is working

can someone help??


P.S: does sring stream read the white space or ends when finds it?
Can you post your attempt?
I didn't post 'cause my attempt is useless.

I don't know how to use string streams well.

Is it the best way of solving my problem?
Can you show a sample of the input file.

Also, can you post an attempt to open the file and read a record from the file.

Take a look at this and perhaps other tutorials: http://www.cplusplus.com/doc/tutorial/files/
this is my best attempt


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

void readFile();

int main (int argc, char* argv[])
{

readFile();

return ;
}



void readFile()
{

ifstream read ("file.txt", ios::in);

if ( !read)
cout<<Error!<<endl;
else
{
stringstream name;
string full;

while (getline(read, full))

name.str(full)


// I don't know what else to do
// Also don't know if This is the best way




the input file in each line has a full name like:

Jonh Mccar Allen Peterson

and I want to print in the screen John Peterson



Your names can be represented as an array of strings. The names would be held as:
1
2
3
4
entry[0] -> "John"
entry[1] -> "Mccar"
entry[2] -> "Allen"
entry[3] -> "Peterson"
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.

Do you follow so far?
are you saying something like this?


#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

void readFile();

int main (int argc, char* argv[])
{

readFile();

return ;
}



void readFile()
{

ifstream read ("file.txt", ios::in);

if ( !read)
cout<<Error!<<endl;
else
{
vector<string> name;
string full;
while(getline(readFile,full))
{
name.push_back(full);
cout << name.at(0)<< " " << name.at(name.size()-1);
}
}
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
        }
    }
Topic archived. No new replies allowed.