Splitting a string with using a custom delimiter in c++ using cin

43 Views Asked by At

I need to take in the following string and split it into two strings: one before the hyphen, and one after the hyphen. I then need to change the data type to an int. However I need to use cin.

Here is the string: 1-6

I tried using other methods, although my lecturer said I'm not allowed to.

1

There are 1 best solutions below

0
273K On

split it into two strings one before the hyphen

std::string before;
std::getline(std::cin, before, '-');

and one after the hyphen

std::string after;
std::getline(std::cin, after);

I then need to change the data type to an int.

const int b = std::stoi(before);
const int a = std::stoi(after);