Section 6 - String Conversions

3A.6.1 Converting From Strings to Numbers

There is a vast difference between:

string s;
s = "-3.4";

and

double c;
c = -3.4;

You tell me what it is.

What you hopefully said is that, while s will look the same as c when printed, s is incapable of being used in any computation. s is not a number, it is a string! You cannot compute directly using strings. Even if a string happens to hold a numeric set of characters, this is unknowable to C++.

In beginning C++ classes, students often use the cin statement to read numbers directly from the user console into their int, float or double variables.  That is, you often read numbers directly from the user into numeric variables.  However in real applications, this is rarely the case.  Normally programs read numbers  from the user in the form of a string, so if we asked the user for a float or double, and they typed in "-3.4", we would receive that value as a string, and we would be unable to compute with it.

To convert it to a number that we can use for computation, use the simple notation:

istringstream(s) >> c; // convert String s to double c

We would do this, for instance, if we read a user response  into the string object s and needed to convert it to a number in order  to compute with it.  This can be done if c is a double, float, int, long, etc.

3A.6.2 Converting From Numbers to Strings

The other direction requires three statements:

ostringstream cnvrt;
cnvrt << c;
s = cnvrt.str();

This results in any numeric variable c having its value turned into a string and stored in s.  Also, if you have to effect a conversion from number to string twice or more in a program, you must   use a separate ostringstream object in place of cnvrt. For instance you could use the name cnvrt2 for the second conversion, cnvrt3 for the third, etc.  We will see this below.

3A.6.3 An Example

Here is an example to demonstrate the conversion.  Note that, in addition to <iostream> and <string> we have to #include the file <sstream>.  This is used for the conversions to work.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int main()
{
string strMyNum = "123.456";
double numMyNum;

// used to convert a string to any numeric type
istringstream(strMyNum) >> numMyNum;

// these should be the same if conversion worked
cout << "Original value as string: " << strMyNum << endl
<< "Original value as number: " << numMyNum << endl;

// compute using the number
numMyNum = (numMyNum + 1) / (numMyNum - 1);

// used to convert any numeric type to a string
ostringstream cnvrt;
cnvrt << numMyNum;
strMyNum = cnvrt.str();

// these should be the same if conversion worked
cout << "Answer as string: " << strMyNum << endl
<< "Answer as number: " << numMyNum << endl;

numMyNum = (numMyNum + 1) / (numMyNum - 1);

// clearing stream for reuse
cnvrt.str("");
cnvrt.clear();

cnvrt << numMyNum;
strMyNum = cnvrt.str();   // convert from double c to string s

// these should be the same if conversion worked
cout << "Answer as string: " << strMyNum << endl
<< "Answer as number: " << numMyNum << endl;
}

Here is your output:

console output 1

This is an interesting mathematical result, by the way (unrelated to our course).  We used the formula (x+1)/(x-1) to get a second number.  Then we applied it again, and we got the original number back!  Does anyone know anything about this?

Reusing the ostringstream Object

In order to use the same object, cnvrt, for a second or third conversion down the road, you have to reset it using the following two statements between conversions:

// clearing stream for reuse
cnvrt.str("");
cnvrt.clear();

This was demonstrated in the above program.

3A.6.4 Converting Between Strings and Chars

Going between strings and chars is very easy.  Since strings are made from chars, all we have to do is specify the position in the string that we want to access.  To grab a single character from a string we can retrieve that char from the string and store it into a separate char variable.  In the other direction, if we want to put a single character into a string we can assign a new value into that position of the string.  Either way, we have to be able to specify the position of a particular character in a long string. We use bracket notation str[p] to access the pth position in string str.  Counting always begins with 0, so that str[0] is the first letter in str, and if string has length n, then the last character in the string is str[n-1].

Here is a simple example:

#include <iostream>
#include <string>
using namespace std;

int main()
{
string myString = "30 Year Adjustable";
char let1, let2;

let1 = myString[0];
let2 = myString[3];

cout << "Original string: \n"
<< myString << endl;
cout << "Letters in position 0 and 3: \n"
<< let1 << " " << let2 << " " << endl;

// now make changes to some characters in the string
myString[0] = '1';
myString[1] = '5';

cout << "Modified string: \n"
<< myString << endl;
}

The output is:

console output 2

Again, let me emphasize, the numbering of the positions begins with 0.  This will be true for arrays when we get to them.