Reading getline from cin to stringstream (C ++)

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

Adam English 85
    Charlie Math 76
    Erica's Story 82
    Richard Science 90

My goal is to ultimately save each piece of data in its own cell in the data structure that I created, so basically I want to analyze the input so that each piece of data is individual. Since each line of input is entered by the user one at a time, every time I get a whole line of input that I need to parse. I'm currently trying to do something like this:

stringstream ss;
getline(cin, ss);

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

, , , XCode , getline, . string, , getline cin stringstream? .

+4
4

, , 1 , getline stringstream, std::cin string, stringstream, :

// 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. , :

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

using namespace std;
+8

std::getline() a std::stringstream; a std::string. , .

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 );
}

, .

+7

cin >> name >> course >> grade;, >> .

+2

using namespace std , , API, std std::, , std::getline(). CSV , , . stdin, CSV int . regex_token_iterator, , , .

// 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();
      }
   }
}
0

All Articles