An extract from C++ Programming in easy steps, 5th edition
A problem arises with string variables when you need to convert them to a different data type, perhaps to perform arithmetical operations with those values. As the string object is not a native C++ data type a string variable value cannot be converted to an int or any other regular data type by casting.
The solution is provided by the C++ <sstream> library that allows a stringstream object to act as an intermediary, through which string values can be converted to a numeric data type, and numeric values can be converted to a string data type. To make this ability available to a program, the library must be added with an #include <sstream> directive at the start of the program.
Values can be loaded into a stringstream object with the familiar output stream << operator that is used with cout statements. Contents can then be extracted from a stringstream object with the >> input stream operator that is used with cin statements.
In order to re-use a stringstream object, it must first be returned to its original state. This requires its contents to be set as an empty string and its status bit flags to be cleared.
Start a new program by specifying the C++ library classes to include, and a namespace prefix to use
#include <string> // Include string support. #include <sstream> // Include stringstream support.
using namespace std ;
Add a main function containing a final return statement and declaring two initialized variables to be converted
string term = “100” ;
int number = 100 ;
// Add more statements here.
return 0 ;
In the main function, insert statements to declare an integer variable, string variable, and a stringstream object
int num ; // To store a converted string.
string text ; // To store a converted integer.
stringstream stream ; // To perform conversions.
Next, use the stream output operator to load the initialized string value into the stringstream object
stream << term ; // Load the string.
Notice how the stringstream object’s str() function is used here to reset its contents to an empty string.
Use the stream input operator to extract content from the stringstream object into the uninitialized integer variable
stream >> num ; // Extract the integer.
Perform arithmetic on the integer and output the result num /= 4 ;
cout << “Integer value: ” << num << endl ;
Reset the stringstream object ready for re-use
stream.str(“”) ; // Empty the contents.
stream.clear() ; // Empty the bit flags.
Now, use the stream output operator to load the initialized integer value into the stringstream object
stream << number ; // Load the integer.
Use the stream input operator to extract content from the stringstream object into the uninitialized string variable
stream >> text ; // Extract the string.
Perform concatenation on the string and output the result
text += “ Per Cent” ;
cout << “String value: ” << text << endl ;
Save, compile, and run the program to see the converted output values
Want to know more?
For the complete C++ Programming guide, all in the trusted In Easy Steps style, click here. In full-colour and straightforward, jargon-free language, C++ Programming in Easy Steps will teach you how to install a free C++ compiler so you can quickly begin to create your own executable programs by copying the book’s examples. It demonstrates all the C++ language basics before moving on to provide examples of Object Oriented Programming (OOP).