Reading getline from cin into a stringstream (C++)
Asked Answered
F

4

18

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.

Fraudulent answered 13/3, 2016 at 18:58 Comment(1)
Do you realise that your program will only work if name and course do not contain spaces? It will fail for "John Smith English" or "Adam Computer Science"...Undertake
D
25

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;
Dhiman answered 13/3, 2016 at 19:2 Comment(0)
L
11

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.

Lozada answered 13/3, 2016 at 19:4 Comment(0)
A
3

You can just use cin >> name >> course >> grade; because >> will read until whitespace anyway.

Ardine answered 13/3, 2016 at 19:1 Comment(5)
Adding a line-by-line parsing layer is usually actually a good thing, though.Essay
this is best answer for meDecember
"will read until whitespace": it is not accurate. It'll read until the whitespace or tab or end of lineGrotesque
@Grotesque tab and end-of-line characters are considered whitespaceUnsure
you're right @YazanAlsalem. Specifically: cplusplus.com/reference/istream/istream/operator-free and cplusplus.com/reference/cctype/isspace for detailed info.Grotesque
S
0

Either you don't have a using namespace std in your code or you're not fully qualifying calls made to the API's in the std namespace with an std:: prefix, for example, std::getline(). The solution below parses CSV instead to tokenize values that have whitespace in them. The logic for stdin extraction, parsing the CSV, and converting grade from string to int are all separated. The regex_token_iterator usage is probably the most complicated part, but it uses pretty simple regex for the most part.

// foo.txt:

// Adam,English,85
// Charlie,Math,76
// Erica,History,82
// Richard,Science,90
// John,Foo Science,89

// after compiling to a.exe, run with:
// $ ./a.exe < foo.txt 

// output
// name: Adam, course: English, grade: 85
// name: Charlie, course: Math, grade: 76
// name: Erica, course: History, grade: 82
// name: Richard, course: Science, grade: 90
// name: John, course: Foo Science, grade: 89

#include <iostream>
#include <sstream>
#include <regex>
#include <vector>

using namespace std;

typedef unsigned int uint;

uint stoui(const string &v) {
   uint i;
   stringstream ss;
   ss << v;
   ss >> i;
   return i;
}

string strip(const string &s) {
   regex strip_pat("^\\s*(.*?)\\s*$");
   return regex_replace(s, strip_pat, "$1");
}

vector<string> parse_csv(string &line) {
   vector<string> values;
   regex csv_pat(",");
   regex_token_iterator<string::iterator> end;
   regex_token_iterator<string::iterator> itr(
      line.begin(), line.end(), csv_pat, -1);
   while (itr != end)
      values.push_back(strip(*itr++));
   return values;
}

struct Student {
   string name;
   string course;
   uint grade;
   Student(vector<string> &data) : 
      name(data[0]), course(data[1]), grade(stoui(data[2])) {}
   void dump_info() {
      cout << "name: " << name << 
      ", course: " << course << 
      ", grade: " << grade << endl;
   }
};

int main() {
   string line;
   while (getline(cin, line)) {
      if (!line.empty()) {
         auto csv = parse_csv(line);
         Student s(csv);
         s.dump_info();
      }
   }
}
Snowbird answered 30/8, 2017 at 11:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.