edit: this question has been marked as a duplicate. I did indeed look through all the previous similar questions I could find and haven’t found an answer. Basically, I am not able to control how the program compiles (though I think it’s already using c++11), so I’m either looking for a reason why stoi isn’t working in this context OR any alternative statements that could serve the same purpose.
I’m pretty new to c++ and am working on this project for class. It has to be submitted through myprogramminglab.com so I can’t modify the compiler. The problem I’m running into is I get the following error:
CTest.cpp: In function 'void getTime(int&, int&, bool&, std::string)': CTest.cpp:38: error: 'stoi' is not a member of 'std' CTest.cpp:39: error: 'stoi' is not a member of 'std'
I understand from googling that this usually would mean that my compiler isn’t configured for C++11. But like I said I have no control over that aspect of myprogramminglab. Am I missing something in my code that might be able to get stoi up and running. Or if not, is there an “old” way of doing this that I can use? I can’t find a good solution in my book (though I admit I might just not know what to look for) and can’t test the rest of my code until I get past this compile error.
If it’s not obvious from the code, the assignment it to take input in the format HH:MM xm and calculate how many minutes are in between the two times, and output the amount in minutes (and hours and minutes) of that difference. I also have to use a function named computeDifference with the parameters mentioned (though I added the string parameter because I wanted to get the input outside of the function).
#include <iostream> #include <string> using namespace std; int computeDifference(int hours_par, int minutes_par, bool isAM_par, int hoursF_par, int minutesF_par, bool isAMF_par); void getTime(int& minutes, int& hours, bool& isAM); int main() { int hours, minutes, fut_hours, fut_minutes, difference; bool isAM, fut_isAM; cout << "Enter start time, in the format 'HH:MM xm', where 'xm' is\n"; cout << "either 'am' or 'pm' for AM or PM:"; getTime(hours, minutes, isAM); cout << "Enter future time, in the format 'HH:MM xm', where 'xm' is\n"; cout << "either 'am' or 'pm' for AM or PM:"; getTime(fut_hours, fut_minutes, fut_isAM); difference = computeDifference(hours, minutes, isAM, fut_hours, fut_minutes, fut_isAM); cout << "There are " << difference << " minutes (" << (difference - (difference%60))/60 << " hours and " << difference%60 << " minutes) between" << hours << ":" << minutes<< " and " << fut_hours << ":" << fut_minutes; return 0; } int computeDifference(int hours_par, int minutes_par, bool isAM_par, int hoursF_par, int minutesF_par, bool isAMF_par) { int start_total = 0, future_total = 0; start_total += hours_par * 60; start_total += minutes_par; if (isAM_par) start_total += 720; future_total += hoursF_par * 60; future_total += minutesF_par; if (isAMF_par) future_total += 720; return future_total - start_total; } void getTime(int& minutes, int& hours, bool& isAM, string timestamp) { string hoursS, minutesS; hoursS = timestamp.substr(0, 2); minutesS = timestamp.substr(3, 2); hours = std::stoi(hoursS); minutes = std::stoi(minutesS); isAM = ("am" == timestamp.substr(6, 2)); cout << hours << " " << minutes << " " << isAM; cout << timestamp; }
I’ve tried it a few different ways, for example without the std:: part. But this seems to give me the least errors…
Any help would be greatly appreciated! Thanks![