C++

Convert Int to String in C++

In C++, we sometimes need the data type conversion like “int to string” or “string to int” and much more. When we talk about converting the integer value from “int” to “string” data type, we have multiple options in C++ programming. We may utilize the “to_string()” method or “string stream” or “sprint()” function in C++ that aids in converting the desired “int” to the “string” data type. In this tutorial, we will study all these methods by which we can convert the “int” to “string” in C++ programming.

Example 1:

The two header files that we include in this code are the “iostream” and the “string” header files. The “iostream” is included here as some functions which we need in this code that are declared in this header file like “cout” or “cin”. The “string” header file is utilized to easily work with the “string” data type as the string functions are declared here.

The “std” namespace is then added and the driver code is now placed here which invokes the “main()” function. The “intValue” is initialized with the value of “45” of the “int” data type. We also initialize the “intToString” variable of the “string” data type and use the “to_string()” method. The number that we want to convert here is the “intValue” number, so we place “intValue” as the parameter of the “to_string()” method. Below this, we use the “cout” and print a line. Then, we add the “intToString” variable in the “cout” which contains the converted “int” to “string” data type’s value.

Code 1:

#include <iostream>

#include <string>

using namespace std;

int main()

{

  int intValue = 45;

  string intToString = to_string(intValue);

  cout << "The converted value from int to string is: ";

  cout << intToString << endl;

  return 0;

}

Output:

The string value is now created from the integer value and is shown here. This results from the “to_string()” method that is being used in our code.

Example 2:

The “iostream” and “string” header files are the two that we included in this code. After that, the “std” namespace is added and the driver code, which we call the “main()” function, is inserted here. The “int” data type initializes the “newIntValue”. Then, “cout” is here which helps in displaying the message in which we type “Please Enter Integer Value here”. When this message renders, the user will enter the “int” value that we get using “cin”.

Here, we use the “newIntValue” with “cin”. So, when we enter the data, it is saved in this “newIntValue”. The “to_string()” method is used in this instance and the “newIntToString” variable of the “string” data type is declared. Since the “newIntValue” is the integer that we wish to convert in this case, we pass it as an argument to the “to_string()” function. Below that, we print a line using “cout” and add the “newIntToString” variable which holds the value of the converted “int” to “string” data type in “cout”.

Code 2:

#include <iostream>

#include <string>

using namespace std;

int main()

{

  int newIntValue;

  cout << "Please Enter Integer Value here: ";

  cin >> newIntValue;

  string newIntToString = to_string(newIntValue);

  cout << "\nThe entered int value to string is: ";

  cout << newIntToString << endl;

  return 0;

}

Output:

We entered “19” as the input integer number and then hit “Enter”. After this, the converted “int” to “string” is displayed in the following:

Example 3:

Here, one more header file, “sstream”, is included along with the “iostream” and “string” header files because “sstream” defines a stream object that employs “str()” to track the internal conversion of an integer to a string after inserting an integer as a stream into an object. Then, inside the driver code, we initialize the “intNum” with the “1234” value of the “int” type. After this, we declare “numToStr” with the “ostringstream”.

To work with strings, we use the output stream class here. After this, we send the “intNum” to the “numToStr” as the stream of the output string. Then, we render “s1” using “cout”.

Code 3:

#include<iostream>

#include <sstream>

#include <string>

using namespace std;

int main()

{

  int intNum = 1234;

  ostringstream numToSTtr;

  numToSTtr << intNum;

  string s1 = numToSTtr.str();

  cout << "The converted int to string is : ";

  cout << s1 << endl;

  return 0;

}

Output:

The converted string, which we get from the “string streams” method in the C++ code, is shown in the following:

Example 4:

Here, we use the “string stream” method again. But this time, we get the integer value from the user and then convert it to a string. So, the “NumByUser ” is initialized using the “int” data type. We also declare “stringNum” with the “ostringstream”. Then, “cout” is here to assist in displaying the message in which we type “Please Enter Integer Value here”.

Below this, the user enters the “int” value that we obtain using “cin” when this message renders. Here, we place the “NumByUser” with “cin” so that whatever data that is entered will be saved in “NumByUser”. Subsequently, we submit the output string stream, “NumByUser”, to “stringNum”. The “.str()” method is used in the following to transform our integer data into a string which is then stored in the “s” variable. Then, we use “cout” to render “s”.

Code 4:

#include<iostream>

#include <sstream>

#include <string>

using namespace std;

int main()

{

    int NumByUser;

    ostringstream stringNum;

cout << "Please Enter the int number: " ;

cin >> NumByUser;

    stringNum << NumByUser;

    string s = stringNum.str();

    cout << "The int number converted to string is : ";

    cout << s << endl;

    return 0;

}

Output:

Here, “44” is the number that we insert as the user’s input. The transformed string, which we obtained using the C++ “string streams” function, is rendered in the following:

Example 5:

The “int a” is initialized here and assigned “475” to this. Then, we declare a string buffer which is “char mystr[1000]”. Below this, we utilize the “strintf()” method which takes three parameters and add “mystr”, “%d”, and “a” as the parameters of this function. This converts the “int” to “string” and prints the value of “a” to the “mystr” string buffer. After this, we render the converted “int” to “string” result by utilizing the “cout”.

Code 5:

#include <iostream>

using namespace std;

int main()

{

  int a = 475;

  char mystr[1000];

  sprintf(mystr, "%d", a);

  cout << "The converted string is: " << mystr;

  return 0;

}

Output:

Now, we get this output using the “strintf()” method in C++ and display the outcome of using the “strintf()” method as follows:

Example 6:

We get the integer input and then apply the “strinft()” method here. The “int_a” integer variable and the “convertedStr” string buffer are declared here. Then, we obtain the user’s input with “cin” and save it in the “int_a” variable.

After this, we pass the “convertedStr, “%d”, int_a” as the parameter of the “strintf()” function. This changes the type of input from “int” to “string”, printing the value of “int_a” to the “convertedStr” string buffer. Next, we use the “cout” function to render the converted “int” to the “string” result.

Code 6:

#include <iostream>

using namespace std;

int main()

{

  int int_a;

  char convertedStr[1000];

  cout << "Please Enter a number: ";

  cin >> int_a;

  sprintf(convertedStr, "%d", int_a);

  cout << "The int to string is: " << convertedStr;

  return 0;

}

Output:

The number that we entered here is “512”. Then, the output of utilizing the C++ “strintf()” technique is shown in the following. This is achieved by utilizing the method in the C++ code:

Conclusion

The concept of converting the integer data type into the string data type is thoroughly explained in this tutorial. We explored three distinct techniques that aid in converting “int” to “string” in C++. These are the “to_string”, “string stream”, and the “strintf()” methods which we studied here. We thoroughly explored all three techniques here along with their C++ codes.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.