I'm getting the error "stoi is not a member of std" in myprogramminglab

15,877

Solution 1

std::stoi is a function available with c++11, so provide compiler option -std=c++11 if you have C++11 available compiler

Otherwise use atoi() in place of stoi() as follows:

   #include<cstdlib>

    hours   = std::atoi(hoursS.c_str());
    minutes = std::atoi(minutesS.c_str());

Solution 2

How do you not have control over that aspect of compiling? If you're using a GCC compiler, recompile with the following options:

g++ -std=c++11 <files>

You can also use the C atoi() function here: http://www.cplusplus.com/reference/cstdlib/atoi/

Also, to test your code, just comment out those lines and provide dummy values for those two variables just to make sure the rest of your code works as intended. Finally, I don't see anywhere in your code where the user is actually allowed to enter any values. Try allowing the user to enter in some values, verify them as if they were strings, then try to convert them to integers and do what you need to do.

Share:
15,877
madEngineer
Author by

madEngineer

Updated on June 04, 2022

Comments

  • madEngineer
    madEngineer almost 2 years

    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!

  • madEngineer
    madEngineer almost 9 years
    Thanks. It's unfortunate but like I replied upthread, the program needs to be submitted through a website called myprogramminglab, it's a companion site for my book that tests you on your code. I don't have access to the compiler (I can't even see what compiler it's using!) but I assume since it goes along with my book, and the book teaches c++11 that it's meant to compile in that way.
  • madEngineer
    madEngineer almost 9 years
    I'm trying atoi now. I put in the include directive that it lists for atoi and the only part of my code I altered was this: hours = atoi(hoursS); 52 minutes = atoi(minutesS); but got this error now CTest.cpp: In function 'void getTime(int&, int&, bool&)': CTest.cpp:51: error: cannot convert 'std::string' to 'const char*' for argument '1' to 'int atoi(const char*)' CTest.cpp:52: error: cannot convert 'std::string' to 'const char*' for argument '1' to 'int atoi(const char*)'
  • Steephen
    Steephen almost 9 years
    @ConfuciusSays look at my answer , you need to convert string to c string using c_str() function
  • Bizkit
    Bizkit almost 9 years
    The reason for that error is that the atoi() function is expecting a const char* which is not the std::string type you have declared. You need to convert your std::string to a const char*. To do that, use the member function c_str() of std::string. cplusplus.com/reference/string/string/c_str Also, fix your function signature for the getTime() function to match the definition.
  • madEngineer
    madEngineer almost 9 years
    This did the trick! I was missing the .c_str() when trying to use atoi. Thank you!
  • Steephen
    Steephen almost 9 years
    Great to know it helped you!!
  • madEngineer
    madEngineer almost 9 years
    You're right, I was misusing atoi and leaving out the c_str() That solved it. Thank you for your help!
  • Bizkit
    Bizkit almost 9 years
    Glad I could help! :)
  • galath
    galath almost 9 years
    Welcome to Stack Overflow! While your answer is in the context, please keep in mind it should address the error in the question. Here are a few tips.
  • Pablo Ariel
    Pablo Ariel over 5 years
    Providing -std=c++11 won't solve the problem, as it won't do -std=c++14 either.