Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading getline from cin into a stringstream (C++)

So I'm trying to read input like this from the standard input (using cin):

Adam English 85
Charlie Math 76
Erica History 82
Richard Science 90

My goal is to eventually store each data piece in its own cell in a data structure I have created, so basically I want to parse the input so each piece of data is individual. Since each row of input is inputted by the user one at a time, each time I get an entire row of input that I need to parse. Currently I am trying something like this:

stringstream ss;
getline(cin, ss);

string name;
string course;
string grade;
ss >> name >> course >> grade;

The error I am having is that XCode is telling me there's no matching function call to getline which is confusing me. I have included the string library, so I'm guessing the error has to do with using getline to read in from cin to a stringstream? Any help here would be appreciated.

like image 268
user5482356 Avatar asked Mar 13 '16 18:03

user5482356


People also ask

Does Getline work with Stringstream?

You cannot std::getline() a std::stringstream ; only a std::string . Read as a string, then use a stringstream to parse it.

Can I use Getline after CIN?

The getline() function in C++ is used to read a string or a line from the input stream. The getline() function does not ignore leading white space characters. So special care should be taken care of about using getline() after cin because cin ignores white space characters and leaves it in the stream as garbage.

How do I use Getline with delimiter?

Using std::getline() in C++ to split the input using delimiters. We can also use the delim argument to make the getline function split the input in terms of a delimiter character. By default, the delimiter is \n (newline). We can change this to make getline() split the input based on other characters too!

Does Getline work with C strings?

Reading strings: get and getlineThe functions get and getline (with the three parameters) will read and store a c-style string. The parameters: First parameter (str) is the char array where the data will be stored.


2 Answers

You are almost there, the error is most probably1 caused because you are trying to call getline with second parameter stringstream, just make a slight modification and store the data within the std::cin in a string first and then used it to initialize a stringstream, from which you can extract the input:

// read input
string input;
getline(cin, input);

// initialize string stream
stringstream ss(input);

// extract input
string name;
string course;
string grade;

ss >> name >> course >> grade;

1. Assuming you have included:

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

using namespace std;
like image 91
Ziezi Avatar answered Sep 17 '22 15:09

Ziezi


You cannot std::getline() a std::stringstream; only a std::string. Read as a string, then use a stringstream to parse it.

struct Student
{
  string   name;
  string   course;
  unsigned grade;
};

vector <Student> students;
string s;
while (getline( cin, s ))
{
  istringstream ss(s);
  Student student;
  if (ss >> student.name >> student.course >> student.grade)
    students.emplace_back( student );
}

Hope this helps.

like image 22
Dúthomhas Avatar answered Sep 17 '22 15:09

Dúthomhas