Convert string to int in C++

Convert String to int in C++

We can convert string to int in different ways. In this article, we will discuss 5 methods to convert String to int in C++.

Convert String to int in C++ using stoi()

This method was introduced in C++ 11. It is the easiest way to convert ‘string’ to ‘int’ in c++.

Code:

Output:

12345
1
999999

Note: ‘stoi()’ only returns the integer part of the decimal number without rounding off.

Convert String to int in C++ using atoi()

‘atoi()’ function can be used to convert character array or string literal to ‘int’ data type. It is defined in the ‘cstdlib’ header file.

Note: atoi() function will return zero if the value passed is invalid or if it encounters any other issue. ‘stoi()’ function will give the error message in that case.

Code:

Output:

12345
1
999999
0

Convert String to int in C++ using stringstream class

We can use stringstream class for converting strings of digits to int. This method is useful for earlier versions of C++.

Code:

Output:

123456789

Convert String to int in C++ using strtol() function (string to long integer)

The strtol() method is used to convert strings to long integers. It is a function of the C standard library and can be implemented in C++. It will not consider the white space characters at the beginning of the string and it will consider other parts as numbers. It will stop when it will encounter the first character which is not a number. (It is a better option than atoi()).
Macro Constant ERANGE=Result too large or small (out of range)
EINVAL=Invalid argument

Syntax:

Note: If the value passed for conversion is too small or too large, then it will store ERANGE in errno. If the value is too large, then it will return a value of LONG_MAX. If the value is too small, then it will return a value of LONG_MIN.

Code:

Output:

Converted string to number: 1234567890

Convert String to int in C++ using Spirit.x3 parser library of Boost

In c++, there is a header-only library Spirit.x3 of boost which can be used for parsing in C++. It can be used to parse integers with its numeric parsers. We will use the ‘boost::spirit::x3::int_parser’ for parsing the integer.

Code:

Output:

INTEGER VALUE: 123456789

Conclusion

In this article, we discussed five different methods for how to convert string to int in C++. Sometimes it can be difficult to handle errors with atoi() because if you take 0 as a valid input, then it will be difficult to identify if it is an error or not. For C++ 03 and earlier versions, the stringstream method can be used.

Happy Learning!!

Related Posts

  • Trim String in C++
    19 July

    Trim String in C++

    Table of ContentsUsing Boost string algorithms to trim string in C++Using find_first_not_of() and find_last_not_of()` to trim strings in C++Using find_if() to trim the string in C++Using stringstream to trim the strings in C++Using a customized function to trim the strings in C++Conclusion When we take an input from an user, strings can have unwanted whitespaces […]

  • JSON parser in C++
    19 July

    JSON Parser in C++

    Table of ContentsSimple JSON Parser in C++ using JsonCpp libraryConclusion In this post, we will see about JSON parser in C++. There is no native support for JSON in C++. We can use a number of libraries that provide support for JSON in C++. We will use JsonCpp to parse JSON files in C++ which […]

  • Enum to String in C++
    19 July

    Convert enum to string in C++

    Table of ContentsUsing stringify macro method to convert enum to String in C++Using const char* Array to convert enum to String in C++Using a custom-defined function to convert enum to String in C++Conclusion There are a variety of methods to convert enum to String in C++. In this article, we will discuss some of the […]

  • C++ random number between 0 and 1
    11 June

    C++ random number between 0 and 1

    Table of ContentsUsing the rand() function to generate random number between 0 and 1 in C++Using the std::uniform_real_distribution() function to generate random number between 0 and 1 in C++ Random numbers are frequently used in programming for test case generation, cryptography, and other related purposes. In this tutorial, we will generate random number between 0 […]

  • String contains C++
    07 June

    Check if string contains substring in C++

    Table of ContentsUsing find() function to check if string contains substring in C++.Using strstr() method to check if string contains substring in C++Using boost library to check if string contains substring in C++Using custom functions to check if string contains substring in C++Conclusion There are different ways to check if string contains substring in C++. […]

  • Convert float to String in C++
    24 May

    Convert float to String in C++

    Learn about how to convert float to String in C++ in different wayds.

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.