How to split a string into a pre determined amount of parts

I am making a function that has to split an inputted string into 3 parts. The string will be inputted in the following format, Book Title|Year Published|Author . Each part is split using the '|' character, I have to take this string and assign them to three different variables and I am unsure of how to do this. Any help would be greatly appreciated.

Thank you.
If you are able to directly extract user input, you can simply use getline with cin.

For example, if the user inputs The Hobbit|1937|Tolkien, then you can do:

1
2
3
4
5
6
7
8
string title;
getline(cin, title, '|');

string year;
getline(cin, year, '|');

string author;
getline(cin, author); // last one, no | character 
Last edited on
Topic archived. No new replies allowed.