Reading a string until '-' is encountered

The input looks something like "Bob-Harper"
I want to read "Bob" as string1, "-" as char1, and "Harper" as string2
At the moment when I use input >> string1; it reads the entire Bob-Harper
Is there a simple way to read until the flag "-" is encountered?
std::string first, second;
char delimeter('-'); //It is always '-' is it?
std::getline(std::cin, first, delimeter);
std::getline(std::cin, second);

Basically std::getline() allows you to give a third parameter which is "up to which character should I read". Default is '\n' which is newline character.

So in my code I read all characters from cin to first until '-' encountered, discard it and read other part into second until newline.
Last edited on
Topic archived. No new replies allowed.