# C++ Program to convert the string into an integer

C++ is a statically typed language. To write programs we need to define variables of specified types. Sometimes we need to read inputs from the console or files. In such a scenario the string data are read into the program. To convert them into other datatypes needs special operations. In this article, we shall discuss how to convert strings to integers in C++. There are a few different techniques to do so. Let us explore them one by one.

## Using stringstream in C++

C++ uses streams for different applications. Such streams are filestreams, standard input/output streams, etc. There is another stream which is known as stringstream. This takes a string as input and does the same operation as other streams. To use the stringstream we need to import the sstream header file. Through the insertion operator (>>) or extraction operator (<<) the stream data can be accessed.

### Syntax

#include < sstream >
stringstream streamObject ( <a string input> );


### Syntax

To use the stream to read a specific type of input, the syntax will be like the below

<data type> variable;
streamObject >> variable;


### Algorithm

Let us see the algorithm to understand how this works as a whole.

• Take a string object x as input
• Create one stringstream object say ss and pass x into the object
• Create an integer variable xInt
• Use insertion operator from ss to store integer into xInt

### Example

#include <iostream>
#include <sstream>

using namespace std;
int solve( string myString) {
int x;
stringstream ss( myString );
ss >> x;
return x;
}

int main()
{
string aNumber = "5126";
int convNumber = solve( aNumber );
cout << "The given number is: " << convNumber << endl;
cout << "100 more than the given number is: " << convNumber + 100 <<
endl;
}


### Output

The given number is: 5126
100 more than the given number is: 5226


## Using sscanf() in C++

Like the stringstream object, a similar way (also works in C) is by using the sscanf() function. Like the normal scanf() function, this takes the format string and the input as a character array. Now from the string, it reads the specified value and inserts it into the given variable pointed by the address of the variable. See the syntax for sscanf() function.

### Syntax

scanf ( <a string input>, <format string>, address(s) of variable );

### Algorithm

Let us see the algorithm to understand how this works as a whole.

• Take a string x as input in C-like character array format
• Use the sscanf() function with parameter x, the format string, and the variable address
• The variable value will directly be stored from the sscanf() function.

### Example

#include <iostream>
using namespace std;

int solve( string myString) {
int x;
sscanf( myString.c_str(), "%d", &x );
return x;
}

int main()
{
string aNumber = "215";
int convNumber = solve( aNumber );
cout << "The given number is: " << convNumber << endl;
cout << "100 more than the given number is: " << convNumber + 100 <<
endl;
}


### Output

The given number is: 215
100 more than the given number is: 315


The program is working the same as the previous one, but here is one thing we have to notice. The sscanf() method does not accept the C++ like string object. It takes a C-like character array. For that, we have used the c_str() function for the given string argument to convert it into a C-like character array.

## Using stoi() in C++

Another quick and easy solution to converting string to integer is using the stoi() function from the “string” header file. This function takes a string object as input, then converts it into its equivalent integer.

### Syntax

#include <string>
stoi ( >integer in string format> );


### Algorithm

• Take a string object x as input
• xInt = stoi( x )
• return xInt as integer variable from given string x.

### Example

#include <iostream>
#include <string>

using namespace std;
int solve( string myString) {
int x;
x = stoi( myString );
return x;
}

int main()
{
string aNumber = "625";
int convNumber = solve( aNumber );
cout << "The given number is: " << convNumber << endl;
cout << "100 more than the given number is: " << convNumber + 100 <<
endl;
}


### Output

The given number is: 625
100 more than the given number is: 725


There are a few variations of this function, like stol() to convert into long, stof() to convert into a float, and so on.

## Using atoi() in C++

The atoi() is quite similar to the stoi, but this is also available in C. This accepts the string in character array format. It can be accessed by importing the cstdlib library. Otherwise, there is no such major difference. Let us see the syntax.

### Syntax

#include <cstdlib>
atoi ( <integer in character array format> );


### Algorithm

• Take a string object x as input as C like character array format
• xInt = atoi( x )
• return xInt as integer variable from given string x.

### Example

#include <iostream>
#include <string>

using namespace std;
int solve( string myString) {
int x;
x = atoi( myString.c_str() );
return x;
}

int main()
{
string aNumber = "1024";
int convNumber = solve( aNumber );
cout << "The given number is: " << convNumber << endl;
cout << "100 more than the given number is: " << convNumber + 100 <<
endl;
}


### Output

The given number is: 1024
100 more than the given number is: 1124


## Conclusion

To convert strings to integers, there are a few different ways. The first two methods using stringstream and using sscanf() are the generic method to convert a string to any data without changing anything in the process, only the target variable type will be different. For stoi() and atoi(), these are only for converting strings to integers. There are some other equivalent functions to convert into other datatypes. The sscanf and atoi() are C-based functions for which, they do not accept string objects. We must convert our string to a character array using the c_str() function before using them.