Convert String To Integer In C++ - C++ Programming Tutorial
C++ Course / Strings / Convert String To Integer In C++

Convert String To Integer In C++

BLUF: Mastering Convert String To Integer In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Convert String To Integer In C++

C++ is renowned for its efficiency. Learn how Convert String To Integer In C++ enables low-level control and high-performance computing in the tutorial below.

This part will cover various techniques for transforming the provided string information into an integer in C++ programming. There are specific scenarios where it becomes necessary to change one data type to another, with converting a string to an integer being a common requirement in programming tasks.

For instance, consider a numeric string like " 143 " that needs to be converted to a numeric data type. In this scenario, a specific function is required to transform the string into an integer, resulting in the numeric value 143. Moving forward, we will delve into various techniques within the C++ programming language that facilitate the conversion of string data into integers.

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 class is employed for transforming a string containing numerical values into an integer data type. This class defines a stream object for inserting a string and retrieving the integer data through stream operations. Within the stringstream class, "<<" and ">>" operators are utilized to extract data from the (<<) operator and insert data by passing stream to (>>) left operand.

Let's develop a program to showcase the stringstream class for converting 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 preceding code snippet, we employ the stringstream class to instantiate an obj object, facilitating the conversion of string data to an integer. Subsequently, we utilize the ">>" operator to retrieve the converted string from obj and convert it into numerical data.

Using the sscanf function

The sscanf function transforms a provided string into a specific data type such as an integer.

Syntax

Example

sccanf ( str, %d, &intvar);

The sscanf function requires three parameters - the character string (str), format specifier (%d), and the integer variable (&intvar) to hold the converted value.

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 explore an example where we can utilize the sscanf function to convert a string into a numerical value in the C++ programming language.

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 value into an integer data type by accepting the string as an argument and providing an integer output.

Syntax

Example

stoi(str);

The stoi function takes a str parameter, which is the string input provided to stoi for the conversion of string data to 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 develop a script that utilizes the stoi method for converting string values to integers within the C++ coding 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

A function atoi is employed to transform a character string into an integer value. It accepts a string of characters as input and outputs an integer value.

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 develop a program that utilizes the atoi function to transform a string value into an integer data type within 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:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience