1

note: I just learned about Getline and Streams.

Instead of a space separating first name, last name, and age, how could I separate them with ^ or --?

Is there a function for this? Is this a stupid question, and if so, why?

-The reason for this question is because I was going to make a function for solving polynomial derivatives and failed miserably.

int main() {
  istringstream inSS;       // Input string stream
  string lineString;        // Holds line of text
  string firstName;         
  string lastName;         
  int    userAge;          


  getline(cin, lineString);


  inSS.str(lineString);


  inSS >> firstName;
  inSS >> lastName;
  inSS >> userAge;

  return 0;
}
shizhen
  • 12,251
  • 9
  • 52
  • 88
jponce
  • 19
  • 1
  • I don't see why you have to change the standard format.. If your real intention to solve the polynomial derivatives, better you make a question for that problem – gameon67 Jan 23 '19 at 04:22
  • [`std::getline` has an overload with a third parameter](https://en.cppreference.com/w/cpp/string/basic_string/getline) that lets you change the default newline delimiter to any other character you want to use. If you want to split on `'^'`, you can `getline(inSS , firstName, '^');`. Don't forget to test the stream state before using what you read to make sure you actually did read something. Thanks to [`operator bool`](https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool), `while (getline(inSS , tempstring, '^')) { use tempstring }` can be very handy. – user4581301 Jan 23 '19 at 05:15
  • Possible duplicate of [How to use stringstream to separate comma separated strings](https://stackoverflow.com/questions/11719538/how-to-use-stringstream-to-separate-comma-separated-strings) – eesiraed Jan 24 '19 at 04:01

1 Answers1

1

The free function getline also offers to have a custom delimiter like user4581301 said. However, it will only extract strings and you also have int.

A similar solution can be found in this answer and I have modified the code to fit your needs: changing the delimiter for cin (c++)

You can use imbue to have some custom delimiter. A simple example is below:

#include <locale>
#include <iostream>

template<char Delim>
struct alternativeDelimiter : std::ctype<char> {
  alternativeDelimiter() : std::ctype<char>(get_table()) {}

  static mask const* get_table()
  {
    static mask rc[table_size];
    rc[Delim] = std::ctype_base::space;
    return &rc[0];
  }
};

int main() {
  using std::string;
  using std::cin;
  using std::locale;

  cin.imbue(locale(cin.getloc(), new alternativeDelimiter<'^'>));

  string word;
  while(cin >> word) {
    std::cout << word << "\n";
  }
}

imbue does take ownership of the ctype, so no worries about calling delete yourself.

If you input some^text, the output will be

some
text

You can also use it with your example, of course.

If you extend the table by writing lines similar to line 11 (rc[Delim] = std::ctype_base::space) only changing Delim, you can have multiple characters that will be interpreted as space.

I am not sure in how far this solves your original problem of writing a math parser, though. The general terminology involves the concepts "parser" and "lexer" and you might research these concepts to build a reliable math solver. Hope it helps, too.

IceFire
  • 4,016
  • 2
  • 31
  • 51