Solving the String Problem in C++ Programming!

Written by In Easy Steps Team on . Posted in News

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.

convert.cpp

Step 1
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.
#include <iostream>
using namespace std ;

Step 2
Add a main function containing a final return statement and declaring two initialized variables to be converted
int main()
{
string term = “100” ;
int number = 100 ;
// Add more statements here.
return 0 ;
}

Step 3
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.

Step 4
Next, use the stream output operator to load the initialized string value into the stringstream object
stream << term ; // Load the string.

Hot-Tip-New_cropped

 

Notice how the stringstream object’s str() function is used here to reset its contents to an empty string.

 

 

Step 5
Use the stream input operator to extract content from the stringstream object into the uninitialized integer variable
stream >> num ; // Extract the integer.

A non-empty stringstream object has bit flags indicating its status as good, bad, eof, or fail – these should be cleared before re-use by the stringstream object’s clear() function, as demonstrated here.

Step 6
Perform arithmetic on the integer and output the result num /= 4 ;
cout << “Integer value: ” << num << endl ;

Step 7
Reset the stringstream object ready for re-use
stream.str(“”) ; // Empty the contents.
stream.clear() ; // Empty the bit flags.

Step 8
Now, use the stream output operator to load the initialized integer value into the stringstream object
stream << number ; // Load the integer.

Step 9
Use the stream input operator to extract content from the stringstream object into the uninitialized string variable
stream >> text ; // Extract the string.

Step 10
Perform concatenation on the string and output the result
text += “ Per Cent” ;
cout << “String value: ” << text << endl ;

Step 11
Save, compile, and run the program to see the converted output values

 
067-1[convert]

 

 

9781840787573

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).