Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9269299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:05:02+00:00 2026-06-18T15:05:02+00:00

I am really confused. I have to be missing something rather simple but nothing

  • 0

I am really confused. I have to be missing something rather simple but nothing I am reading about strtol() is making sense. Can someone spell it out for me in a really basic way, as well as give an example for how I might get something like the following to work?

string input = getUserInput;
int numberinput = strtol(input,?,?);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:05:04+00:00Added an answer on June 18, 2026 at 3:05 pm

    The first argument is the string. It has to be passed in as a C string, so if you have a std::string use .c_str() first.

    The second argument is optional, and specifies a char * to store a pointer to the character after the end of the number. This is useful when converting a string containing several integers, but if you don’t need it, just set this argument to NULL.

    The third argument is the radix (base) to convert. strtol can do anything from binary (base 2) to base 36. If you want strtol to pick the base automatically based on prefix, pass in 0.

    So, the simplest usage would be

    long l = strtol(input.c_str(), NULL, 0);
    

    If you know you are getting decimal numbers:

    long l = strtol(input.c_str(), NULL, 10);
    

    strtol returns 0 if there are no convertible characters at the start of the string. If you want to check if strtol succeeded, use the middle argument:

    const char *s = input.c_str();
    char *t;
    long l = strtol(s, &t, 10);
    if(s == t) {
        /* strtol failed */
    }
    

    If you’re using C++11, use stol instead:

    long l = stol(input);
    

    Alternately, you can just use a stringstream, which has the advantage of being able to read many items with ease just like cin:

    stringstream ss(input);
    long l;
    ss >> l;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am confused about something really simple in ASP.NET. I have seen many times
I am really confused about object relationships! I have two classes Person and Address.
I'm really confused about the visitor pattern and its uses. I can't really seem
Really confused about what's going on here. I have a class defined as follows:
I'm really confused, I just wrote this x = 0 y = 42 #Can
I'm really confused with an array im making, its gotten a bit confusing to
I am really confused i have a directory structure as follows. /foo/bar/project/ under project
I'm confused by this behavior: I have an out-of-the-box MVC3 app. I haven't really
Ok, I have really confused myself. We have this app that concates values and
I'm really confused. I have a KML feed at https://views.scraperwiki.com/run/hackney_council_planning_kml_output/ ? ...Which worked perfectly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.