Pre-requisite:
Before checking the examples of this tutorial, you have to check the g++ compiler is installed or not in the system. If you are using Visual Studio Code, then install the necessary extensions to compile the C++ source code to create the executable code. Here, the Visual Studio Code application has been used to compile and execute the C++ code.
Using atoi() function:
The atoi() function is used to return a number by converting a string created by a char array to an integer. The cstdlib header file is required to include to use this function.
Syntax:
Create a C++ file with the following code to convert a string of numbers into an integer using the atoi() function. Here, the strcpy() function has been used to convert the string into a char array. The input string value has converted into a char array, and the converted value has been used in the atoi() function to get the integer value of the string. Next, the converted integer will be printed if the conversion is done properly.
#include <iostream>
//Include for using the atoi() function
#include <cstdlib>
//Include for using the strcpy function
#include <cstring>
intmain() {
//Declare a string variable
std::string strData;
//Declare a chracter array variable
charstrarr[50];
//Take a number from the user
std::cout<>strData;
//Convert the string into a charcater array
strcpy(strarr, strData.c_str());
//Convert the character array into a integer
int number = std::atoi(strarr);
//Print the number
std::cout<<"The converted number is = "<< number <<'\n';
return0;
}
Output:
The following output will appear if 6090 is taken as input after executing the code.
Using stoi() function:
The atoi() function is used to return a number by converting a string value into an integer. The first argument of this function is mandatory, and the others arguments are optional. The syntax of this function is given below.
Syntax:
Create a C++ file with the following code to convert the string into an integer using the stoi() function. After executing the code, the input value taken from the user will be converted into a number and printed if the input value is a valid number. If the input value contains any alphabet or non-numeric character, then the invalid_argument exception will be generated, and an error message will be printed.
#include <iostream>
intmain()
{
//Declare a string variable
std::string strData;
//Take a number from the user
std::cout<>strData;
//Convert the string into number with error handling
try {
//Convert the string into integer
int number = std::stoi(strData);
//Print the converted number
std::cout<<"The converted number is = "<< number <<'\n';
}
//Handle error if invalid number is given
catch (std::invalid_argumentconst&e) {
std::cout<<"Input value is not a number.\n";
}
return0;
}
Output:
The following output will appear if 4577 is taken as input after executing the code.
The following output will appear if hello is taken as input after executing the code.
Using string streams:
Using the istringstream() function is another way to convert the string into an integer by using the’>>’ operator. Create a C++ file with the following code to convert string data into an integer by using the istringstream() function. A string value of numbers has been assigned into a string variable in the code that has been used as the argument value of the istringstream() function. Next, the converted integer value has been printed.
#include <iostream>
//Include for using the istringstream() function
#include <sstream>
intmain()
{
//Declare a string variable
std::string strData = "12345";
//Declare an integer variable
intnumber;
//Convert the string into the integer
std::istringstream(strData) >> number;
//Print the converted number
std::cout<<"The converted number is = "<< number <<'\n';
return0;
}
Output:
The following output will appear after executing the code.
Using sscanf() function:
Using the sscanf() function is another way to convert the string into an integer. The cstdio header file is required to include for using this function. Create a C++ file with the following code to convert a string value into the integer using the sscanf() function. After executing the script, a string value will be taken from the user. If the input value is a valid number value, then the input value will be converted into the integer and printed; otherwise, an error message will be printed.
#include <iostream>
//Include for using the sscanf() function
#include <cstdio>
intmain() {
//Declare a string variable
std::string strData;
//Declare an integer variable
intnumber;
//Take a number from the user
std::cout<>strData;
if (sscanf(strData.c_str(), "%d", &number) == 1) {
//Print the converted number
std::cout<<"The converted number is = "<< number <<'\n';
}
else {
//Print the error message
std::cout<<"Input value is not a number.\n";
}
return0;
}
Output:
The following output will appear if 78325 is taken as input after executing the code.
Using for loop:
The following example shows how to convert a string value into an integer value without using any built-in function. Create a C++ file with the following code to convert a string value into an integer number using the ‘for‘ loop. A string value of the number has been assigned into a variable that has been used in the ‘for‘ loop to convert the string into an integer.
#include <string>
intmain()
{
//Declare a string variable
std::string strData = "6000";
//Declare an integer variable
intnumber;
//Convert the string into integer
for (charchr: strData)
{
if (chr>= '0'&&chr<= '9') {
number = number * 10 + (chr - '0');
}
//Print the converted number
std::cout<<"The converted number is = "<< number <<'\n';
return0;
}
Output:
The following output will appear if 6000 is taken as input after executing the code.
Conclusion:
Five different ways to convert a string into a number have been described in this tutorial by using simple examples for helping the readers to know the way to convert any string to an integer in C++ programming.
from Linux Hint https://ift.tt/2TBD0x6
0 Comments