How To Convert String To Int in C++ – The Complete Guide
Do you know that C++ offers a wide range of features that contribute to the major upgradation of programs? It has all the updated features of the C programming language that make it easier for developers to work with.
One such highly used feature of C++ is converting one data type into another for the better efficiency of programs. A string data type is a data type in C++ that is in the form of an array. It contains a sequence of characters. The string data type usually contains data in text format and Int data type contains data in numbers. This conversion is useful when you come across a file that needs numeric data but it is formatted in text. In this blog, we will learn about how to convert string to int in C++ by using some specific methods.
Techniques To Convert String To Integer in C++
You will notice that the numeric values appear in text format. They are hard to interpret when you are working with numeric functions. Hence, the conversion in C++ is used when you have a data sample that consists of numbers. The string data type is used in that particular sample.
For better formatting of data, the string data type is converted to int in C++ using any one of the following techniques:
- stoi() function: This function breaks down from string to integer when expanded, that is, ‘s’ stands for string, and ‘i’ stands for integer.
- atoi() function: This function breaks down from argument to integer when expanded, that is, ‘a’ stands for argument, and ‘i’ stands for integer.
- stringstream function: This function allows the string value to be read as a stream.
You can pursue an online C++ course if you wish to master this subject.
Converting String to Int Using stoi() Function
This function in C++ is used to convert the string argument text to int form. It is used by most developers which accounts for its popularity. The following parameters are used in its working:
- str: This parameter in the syntax is used to indicate the conversion of the string.
- Header File: The header file of this function is <string>.
- Return Value: The stoi() function will return the value as an integer.
Syntax:
int stoi(string str);
Example:
#include <iostream>
#include <string>
using namespace std;
int main() {
string str1 = "10";
string str2 = "20 twenty";
string str3 = "30 thirty 30";
int num1 = stoi(str1);
int num2 = stoi(str2);
int num3 = stoi(str3);
cout << num1 << endl;
cout << num2 << endl;
cout << num3 << endl;
return 0;
}
Output:
10
20
30
Converting String to Int Using atoi() Function
This function converts the literal character array string arguments into integer form in C++. It only works in C++ and C style strings using <cstdlib.h> as the header file. The following are the parameters that it works on:
- char: It indicates that the function only converts the character array string.
- Header File: It uses <cstdlib.h> as the header file.
- Return Value: It returns the integer value.
Syntax:
int atoi(char* ch);
Example:
#include <iostream>
#include <cstdlib> // Include the <cstdlib> header for atoi()
using namespace std;
int main() {
const char* str1 = "10";
const char* str2 = "20 twenty";
const char* str3 = "30 thirty 30";
int n1 = atoi(str1);
int n2 = atoi(str2);
int n3 = atoi(str3);
cout << n1 << endl;
cout << n2 << endl;
cout << n3 << endl;
return 0;
}
Output:
10
20
30
In the above-mentioned example, you have used three strings in the character array, str1, str2, and str3. The string value for conversion is 32, 45, and 57 respectively. The atoi() function will convert all the values in this string into integers. The output that you will receive will be, 32, 45, and 57.
Converting String to Int Using stringstream Function
This function allows you to read the string as a stream, hence it justifies its name “stringstream” which is a mixture of both. It is often used to convert the string data into integers. The following are some of its parameters:
- str: It indicates that the string has to read as a stream.
- Header File: The <sstream> is the header file in this function.
- Return Value: The return value has to be an integer.
Syntax:
stringstream strm(str);
Example:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
int main() {
string str = "32 thirty two";
stringstream strm(str);
int num;
strm >> num;
cout << num;
return 0;
}
In the above-mentioned example, you have used the “stringstream” function which takes input from the string and gives an output in the stream format.
stoi() Function vs atoi() Function
The table below explains the differences between the two functions that are used to convert string to integer in C++:
stoi() function | atoi() function |
This function can be used with both the styles of programming languages, that is, C++ and C. | This function only supports the styles of C programming. |
The header file of this function is <string>. | The header file of this function is <stdlib.h>. |
If you use non integer in this function as a starting character then it gives an error. | If you use non integer in this function as a starting character then it gives no errors. |
Conclusion
In this blog, we learned everything about how to convert string to int in C++. The methods used for the conversion are all pre-defined. This conversion is mostly done when you want to convert the string text format into numerical integers for a better understanding of a file.