In this article, we will discuss how to convert a string to an integer in C++.
Table Of Contents
Method 1: Using istringstream
Create a istringstream object, and initialize it with the string value. Then fetch the integer value from this stingstream object using operator >>. This way we can convert a string into an integer in C++. Let’s see an example,
#include <iostream> #include <string> #include <sstream> int main() { std::string strValue = "77"; int number; // create & initialize istringstream object with string std::istringstream stringStream(strValue); // fetch number from string stream stringStream >> number; std::cout << number << std::endl; return 0; }
Output:
77
We converted the string value “77” to integer value 77.
Frequently Asked:
But remember, if your string has any non numeric character, then it will stop the conversion at that character only. All the numeric characters before that will be used for conversion.
Method 2: Using Boost Library’s lexical_cast<>
If you are using boost library, then you can directly cast the string value to integer using lexical_cast
. Let’s see an example,
#include <iostream> #include <string> #include <boost/lexical_cast.hpp> int main() { std::string strValue = "77"; int number; try { // Cast string to integer number = boost::lexical_cast<int>(strValue); std::cout << number << std::endl; } catch (boost::bad_lexical_cast const &e) { std::cout << "Not able to convert string to int" << std::endl; } return 0; }
Output:
77
We converted the string value “77” to integer value 77.
Best Resources to Learn C++:
But remember, if your string has any non numeric character, then it will throw the exception bad_lexical_cast
.
Method 3: Using std::stoi() function
We can convert string to integer by passing it to the stoi(). But if string has any non numeric character, then it can throw exception invalid_argument
. Also, if number is too big for integer, then it will throw exception out_of_range
. Let’s see an example,
#include <iostream> #include <string> int main() { std::string strValue = "77"; int number; try { // Convert string to integer number = std::stoi(strValue); std::cout << number << std::endl; } catch (std::invalid_argument const &e) { std::cout << "Exception invalid_argument" << std::endl; } catch (std::out_of_range const &e) { std::cout << "Exception out_of_range" << std::endl; } return 0; }
Output:
77
We converted the string value “77” to integer value 77.
Method 4: Using std::atoi() function
We can convert string to integer by passing character array to the atoi(). we can use the c_str(), to get the char pointer to internal characters, then we can pass that to the atoi(), to convert it to int. Let’s see an example,
#include <iostream> #include <string> #include <cstdlib> int main() { std::string strValue = "77"; // Convert string to integer int number = std::atoi(strValue.c_str()); std::cout << number << std::endl; return 0; }
Output:
77
We converted the string value “77” to integer value 77.
Method 5: Using sscanf()
We can use the sscanf() to scan the string based on pattern “%d”. If the string has only numeric value, then it will intialize an integer with it. Let’s see an example,
#include <iostream> #include <string> #include <cstdio> int main() { std::string strValue = "77"; int number; // Convert string to integer if (sscanf(strValue.c_str(), "%d", &number) == 1) { std::cout << number << std::endl; } else { std::cout << "Can not convert string to int \n"; } return 0; }
Output:
77
We converted the string value “77” to integer value 77.
Summary
We learned about different ways to convert a string to an int in C++. Thanks.