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 8861973
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:36:56+00:00 2026-06-14T15:36:56+00:00

I am trying to use the atoi function in order to obtain conversion from

  • 0

I am trying to use the atoi function in order to obtain conversion from string to int. The thing is that I have a string array which contains both integers and string values.

From what I’ve read, in order to get the error code from it, the function must return 0 :

string s = "ssss";
int i = atoi(s.c_str())
if (i == 0)
    cout<<"error"<<endl;
end;

How should I proceed if my string value is 0 ?

Another issue is with this string : string s = "001_01_01_041_00.png". The atoi function returns the value 1. Shouldn’t it return 0. Why does it return 1?

  • 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-14T15:36:58+00:00Added an answer on June 14, 2026 at 3:36 pm

    That is why atoi is unsafe to use. It doesn’t detect and inform the program if the input is invalid.

    C++11 has introduced std:stoi which is safe, as it throws exception if input is invalid in some way. There are also two other variants : std::stol and std:stoll. See the online documentation for detail:

    • std::stoi, std::stol, std::stoll

    Your code would become this:

    try {
         string s = "ssss";
         int  i = std::stoi(s); //don't call c_str() 
         //if (i == 0) no need to check!
         std::cout << i << endl;
    }
    catch(std::exception const & e)
    {
         cout<<"error : " << e.what() <<endl;
    }
    

    Note that the runtime type of e could be either std::invalid_argument or std::out_of_range depending on the cause of the throw. You could just write two catch blocks if you want them to handle differently.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying use a from a multi-dimensional array that I create in another
I have been trying use the edit_post_link() function to contain an image. All of
I'm trying to convert only the digits from a string to an int vector,
I am trying use gem tire to search in my application. I have tables
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Hi I'm trying use a datepicker on a field I have. I'm trying to
Trying to use Powershell to script the removal of specific custom errors from an
i'm trying to use SDL and read user input, for that I need to
I'm trying to parse a string from WebSockets connection in Go language. I'm implementing
I am trying use a javascript function while passing php variables in it. For

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.