Convert String To Integer In C++

This section will discuss the different methods to convert the given string data into an integer using the C++ programming language. There are some situations or instances where we need to convert a certain data to another type, and one such situation is to convert string to int data in programming.

For example, we have a numeric string as " 143 ", and we want to convert it to a numeric type. We need to use a function that converts a string to an integer and returns the numeric data as 143. Now, we will learn each method that helps convert string data into integers in the C++ programming language.

Different methods to convert the string data into integers in the C++ programming language.

  • Using the stringstream class
  • Using the stoi function
  • Using the atoi function
  • Using the sscanf function
  • Using the stringstream class

The stringstream is a class used to convert a numeric string to an int type. The stringstream class declares a stream object to insert a string as a stream object and then extracts the converted integer data based on the streams. The stringstream class has "<<" and ">>" operators, which are used to fetch data from (<<) operator and insert data by passing stream to (>>) left operator.

Let's create a program to demonstrate the stringstream class for converting the string data into an integer in the C++ programming language.

Program1.cpp

Example

#include <iostream>

#include <sstream> // use stringstream class

using namespace std;

int main()

{

	string str1 = "143"; // declare a string

	

	int intdata; // declare integer variable

	

	/* use stringstream class to declare a stream object to insert a string and then fetch as integer type data. */

	 

	 stringstream obj;

	 obj << str1; // insert data into obj

	 obj >> intdata; // fetch integer type data

	 

	 cout << " The string value is: " << str1 << endl;

	 cout << " The representation of the string to integer type data is: " << intdata << endl;

	 return 0;

}

Output

Output

The string value is: 143

 The representation of the string to integer type data is: 143

In the above program, we use the stringstream class to create an obj object, and it helps to convert string data into an integer. We then use the "<<" operator to insert string characters into the obj object, and then we use the ">>" operator to extract the converted string from obj into numeric data.

Using the sscanf function

A sscanf function converts a given string into a specified data type like an integer.

Syntax

Example

sccanf ( str, %d, &intvar);

The sscanf function has three arguments to specify the char string (str), data specifier (%d), and the integer variable (&intvar) to store the converted string.

Algorithm of the sscanf function

  • The sscanf function belongs to the stringstream class, so we need to import the class into our program.
  • Initialize a constant character string str.
  • Create an integer variable to hold the converted string to integer values.
  • Pass the string variable into the sscanf function, and assign the sscanf function to the integer variable to store the integer value generated by the function.
  • Print the integer values.

Let's consider an example to use the sscanf function to convert the string into numeric number in C++.

Program2.cpp

Example

#include <iostream>

#include <sstream> // use stringstream class

using namespace std;

int main ()

{

	// declare the character strings

	const char *str1 = "555"; 

	const char *str2 = "143"; 

	const char *str3 = "101"; 

	

	// declare the integer variables 

	int numdata1; 

	int numdata2; 

	int numdata3; 

	

		

	/* use sscanf() function and to pass the character string str1,

	 and an integer variable to hold the string. */

	 sscanf (str1, "%d", &numdata1);

	 cout <<" The value of the character string is: " << str1;

	 cout << "\n The representation of string to int value of numdata1 is: " << numdata1 <<endl;

	 sscanf (str2, "%d", &numdata2);

	 cout <<"\n The value of the character string is: " << str2;

	 cout << "\n The representation of string to int value of numdata2 is: " << numdata2 <<endl;

	 

	 sscanf (str3, "%d", &numdata3);

	 cout <<"\n The value of the character string is: " << str3;

	 cout << "\n The representation of string to int value of numdata3 is: " << numdata3 <<endl;

	 return 0;

}

Output

Output

The value of the character string is: 555

 The representation of string to int value of numdata is: 555

 The value of the character string is: 143

 The representation of string to int value of numdata is: 143

 The value of the character string is: 101

 The representation of string to int value of numdata is: 101

Using the stoi function

The stoi function converts a string data to an integer type by passing the string as a parameter to return an integer value.

Syntax

Example

stoi(str);

The stoi function contains an str argument. The str string is passed inside the stoi function to convert string data into an integer value.

Algorithm of the stoi function

  • Initialize the string variable to store string values.
  • After that, it creates an integer type variable that stores the conversion of string into integer type data using the stoi function.
  • Print the integer variable value.

Let's create a program to use the stoi function to convert the string value into the integer type in the C++ programming language.

Program3.cpp

Example

#include <iostream>

#include <string>

using namespace std;

int main ()

{

	string strdata1 = "108";

	string strdata2 = "56.78";

	string strdata3 = "578 Welcome";

	

	// use stoi() function to pass string as arguments

	int intdata1 = stoi (strdata1);

	int intdata2 = stoi (strdata2);

	int intdata3 = stoi (strdata3);

	

	// print the converted values

	cout << " The conversion of string to an integer using stoi(\"" << strdata1 << "\") is " << intdata1 <<endl;

	cout << " The conversion of string to an integer using stoi(\"" << strdata2 << "\") is " << intdata2 <<endl;

	cout << " The conversion of string to an integer using stoi(\"" << strdata3 << "\") is " << intdata3 <<endl;

	return 0;

}

Output

Output

The conversion of string to an integer using stoi("108") is 108

 The conversion of string to an integer using stoi("56.78") is 56

 The conversion of string to an integer using stoi("578 Welcome") is 578

Using the atoi function

An atoi function is used to convert the character string into an integer value. An atoi function passes the character type string to return the integer data.

Syntax

Example

atoi (const char *str);

Algorithm of the atoi function

  • Initialize a pointer-type character array to store a string.
  • After that, it creates an integer type variable that stores the conversion of string into integer type data using the atoi function.
  • Print the integer variable value.

Let's create a program to use the atoi function to convert the string value into the integer type in the C++ programming language.

Program4.cpp

Example

#include <iostream>

#include <string>

using namespace std;

int main ()

{

	// declare a constant character array of string 

	const char *strdata1 = "256";

	const char *strdata2 = "16.18";

	const char *strdata3 = "1088 Good Bye";

	

	// use atoi() function to pass character type array as arguments

	int intdata1 = atoi (strdata1);

	int intdata2 = atoi (strdata2);

	int intdata3 = atoi (strdata3);

	

	// print the converted values

	cout << " The conversion of string to an integer value using atoi(\"" << strdata1 << "\") is " << intdata1 <<endl;

	cout << " The conversion of string to an integer value using atoi(\"" << strdata2 << "\") is " << intdata2 <<endl;

	cout << " The conversion of string to an integer value using atoi(\"" << strdata3 << "\") is " << intdata3 <<endl;

	return 0;

}

Output

Output

The conversion of string to an integer value using atoi("256") is 256

 The conversion of string to an integer value using atoi("16.18") is 16

 The conversion of string to an integer value using atoi("1088 Good Bye") is 1088

Input Required

This code uses input(). Please provide values below: