I have a file that contains lines like:

2/3/4 5/6/7 8/9/10

and I want to extract the 2, the 5, and the 8

So I am parsing the line by the ' ' delmiter to get
2/3/4
5/6/7
8/9/10

then I need to parse each of those to get the stuff up to the first '/'

indata.open(filename);
string line;
getline(indata, line);

//line is now "hello world yay"

stringstream Parsed(line);

//I can read to ' ' delimiter with >>

string a, b, c;
Parsed >> a; // a == "hello"
Parsed >> b;  // b == "world"
Parsed >> c; // c == "yay"

//Now I want to parse a by the '/' character
string d;
getline(a, d, '/');

I get this error

error: no instance of overloaded function "getline" matches the argument list
argument types are: (std::string, std::string, char)

Why does this not work?

Thanks!

Dave

Recommended Answers

All 4 Replies

I have a file that contains lines like:

2/3/4 5/6/7 8/9/10

and I want to extract the 2, the 5, and the 8

So I am parsing the line by the ' ' delmiter to get
2/3/4
5/6/7
8/9/10

then I need to parse each of those to get the stuff up to the first '/'

indata.open(filename);
string line;
getline(indata, line);

//line is now "hello world yay"

stringstream Parsed(line);

//I can read to ' ' delimiter with >>

string a, b, c;
Parsed >> a; // a == "hello"
Parsed >> b;  // b == "world"
Parsed >> c; // c == "yay"

//Now I want to parse a by the '/' character
string d;
getline(a, d, '/');

I get this error

error: no instance of overloaded function "getline" matches the argument list
argument types are: (std::string, std::string, char)

Why does this not work?

Thanks!

Dave

a is a string, not a stream. getline works with streams, not strings. I don't think you need or want getline in this case. Instead, you can use the find function from the string library, along with the substr function from the string library to extract what you want from the string a:

http://www.cplusplus.com/reference/string/string/find.html
http://www.cplusplus.com/reference/string/string/substr.html

I disagree. I think that getline() is one of those under-valued little functions that make simple parsing like this a breeze.

But like VD said, you must use it on a stream. You actually almost have it already.

vector <int> parse_date( string date )
  {
  vector <int> result;
  stringstream datestream( date );
  string s;
  while (getline( datestream, s ))
    {
    int i;
    if ((result.size() > 3) or (!(stringstream( s ) >> i)))
      {
      // invalid date
      result.clear();
      break;
      }
    result.push_back( i );
    }
  return result;
  }

int main()
  {
  vector <int> date;
  string user_input;
  cout << "Please enter a date as Y/M/D> ";
  getline( cin, user_input );
  date = parse_date( user_input );

  if (date.empty())
    cout << "That wasn't a date!\n";
  else
    cout << "The Americans would say: "
         << date[1] << '/' << date[2] << '/' << date[0] << endl;

  return 0;
  }

If you use STL algorithms you can reduce all that to four or five lines of code.

I dont see where you set the delimiter? Is it magically splitting the date string at '/' ??

Yoinks! Good catch.

Line 6 should read [b]while[/b] (getline( datestream, s, '/' )) Sorry about that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.