As we know, the string is nothing but the character array. But in the string, we can also store the numeric values. So it is possible to retrieve that numeric value and store it into numeric data types like int, float or double. We can convert the string to int in C++ using stoi(), atoi(), sscanf(). For the older version user, they can use the string stream class for the conversion.
Conversion using sscanf() and string stream is the simplest option for converting a string into an int. The other two functions stoi() and atoi() needs a parameter listing to return the appropriate output.
Table of Contents
Ways in C++ to convert string to int.
There are many ways to convert string to int in C++. We are going to learn all the possible ways for it.
1. Converting a string to int in C++ using string stream class.
For older C++ users, they can use string stream for converting the string into int in.
Example
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string str;
cout<<"Enter the number : ";
cin >> str;
stringstream print(str);
int x = 0;
print >> x;
cout << "Value of x: " << x;
}
Output
Enter the number : 392
Value of x: 392
In the above example, we have accepted a number from the user and passed it to the string stream object. It will return the integer in int format.
2. Converting a string to int in C++ using the inbuilt stoi() function.
The built-in stoi() function is very simple to use for newer C++ version users. Just passing the string to it will return a numeric value. If it contains any character, it will throw an exception.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cout<<"Enter the number : ";
cin >> str;
int n = stoi(str);
std::cout << "String = "<< str << std::endl;
std::cout << "Integer = "<< n << std::endl;
}
Output
Enter the number : 17
String = 17
Integer = 17
In the above example, we have accepted the number in string format and passed it to the stoi() it will return an integer. Simply store the return value of function into a variable and print it.
3. Converting a string to int in C++ using the inbuilt atoi() function.
The atoi() function works differently. If the string contains any character or white space, the function stops reading the string and returns it. If a user enters the 8y65 as an input, then atoi() will return only 8.
Example
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
string s;
cout<<"Enter the number:";
cin >> s;
int i = atoi(s.c_str());
cout <<"Int value stored in i:"<< i << '\n';
return 0;
}
Output
Enter the number:2345
Int value stored in i:2345
In the above example, we have taken input from the user and passed it to the atoi() function. It will return the result in an integer format.
4. Converting a string to int in C++ using sscanf() function
The sscanf() function is used to read data from the buffer and store the data into the given location.
Example
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
int main() {
cout<<"Enter a string:";
string s;
cin>>s;
int i;
if (sscanf(s.c_str(), "%d", &i) == 1) {
std::cout <<"Integer stored in int format:"<< i << '\n';
} else {
std::cout << "Something went wrong";
}
return 0;
}
Output
Enter a string:24576
Integer stored in int format:24576
We have used sscanf() to read the data from the string. We check if the sscanf function returns 1 or not. If it returns the 1 then the conversion is successful. If it returns other than 1 then we can say the conversion is unsuccessful.
How to convert int to string in C++?
There are two ways to convert int to string. The simplest way to convert the int data type into a string is by using the building to_string() function. Another way to convert int into a string is by using string streams.
1. Converting int to string using to_string() function.
The built-in function to_string is very simple to use. Pass the variable as a parameter to the function that you want to convert. The function will return it in string format.
Example
#include <iostream>
#include <string>
using namespace std;
int main() {
cout<<"Enter a number:";
int n;
cin>>n;
string str = to_string(n);
cout <<"Interger stored in string: "<<str;
return 0;
}
Output
Enter a number:59843
Integer stored in string: 59843
In the above example, we have accepted a number from the user and store it in the variable. Passing that variable to the function will return a string.
2. Converting int to string using string stream class.
We can also use string stream class for converting int to string. Create an object and pass the number to it. The object will return int in string format.
Example
#include <iostream>
#include<string>
#include<sstream> // for using stringstream
using namespace std;
int main() {
cout<<"Enter a number:";
int n;
cin>>n;
stringstream print;
print << n;
string str = print.str();
cout <<"Integer value stored in string: "<<str;
return 0;
}
Output
Enter a number:532
Integer value stored in string: 532
In the above example, we have accepted a number from the user and passed it to the string stream object. It will return the int in the string format.
Did you know? |
---|
1. For Loop In C++ |
2. Vectors In C++ |
3. Difference Between C and C++ |
4. C++ Swap |
5. C++ List |
6. C++ Hello World |
7. C++ Map |
8. Converting String To Int C++ |
9. Stack C++ |
10. Queue C++ |