atoi instead of stoi

Hello,

So I'm working on a program that computes the difference between two times that are input by the user. Below is a function to take the input, split it into the variables I need using stoi.

Out of curiosity, I was wondering how this same function would be written by using atoi instead, for pre C11.
I'm struggling to find a format to follow to make the conversion myself.

Thanks in advance! :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 void start_time(int& start_hr, int& start_min, bool& start_xm, string& start_str)
{
	string str;
	cout << "Enter start time, in the format 'HH:MM xm', where 'xm' is" << endl;
	cout << "either 'am' or 'pm' for AM or PM:" << endl;
	std::getline(std::cin, str); // get the input of the time
	start_str = str; // will use this for final return on main

	// find the points to split the string
	std::size_t splitA = str.find(':');
	std::size_t splitB = str.find(' ');

	// split and convert the string to hour, minute and xm
	start_hr = std::stoi(str.substr(0, splitA));
	start_min = std::stoi(str.substr(splitA + 1, splitB - splitA));
	start_xm = ("am" == str.substr(splitB + 1));
	
}
Topic archived. No new replies allowed.