I'm trying to figure out a way to read in two values- one is a char and one is a string. I would like to pull the real number from the string if it is a real number.
basically if the string was a real number it would output as double type.
here's an example of the basic input structure:
and then somehow would use a function of "strtod" or something similar to extract the real number if there is one.Code:#include <iostream> #include <string> using namespace std; char a; string b; cin >> a >> b;
STRTOD - convert string to double precision number.
here is an example of one that works but w/o the cin value. I have tried using this one below by adding a cin value, but can't get it to work.
Code:#include <iostream> #include <stdlib.h> #include <stdio.h> #include <string> using namespace std; /* CELEBS3 This example converts a string to a double value. It prints out the converted value and the substring that stopped the conversion. */ int main() { char *string, *stopstring; double x; string = "-5667.45&%$"; x = strtod(string, &stopstring); printf("string = \"%s\"\n", string); printf(" strtod = %f\n", x); printf(" Stopped scan at \"%s\"\n\n", stopstring); cout << x << endl; cout << stopstring << endl; }
and the output is:
string = "-5667.45&%$"
strtod = -5667.450000
Stopped scan at "&%$"
-5667.45
&%$
Press any key to continue . . .
part of that reason is I don't know how to make that strtod function work with a cin command line is I don't know what the asterisks are for on this line: char *string, *stopstring;
I thought if the data type was char, it would be just one letter long. according to the line:
string = "-5667.45&%$";" can have more than one letter...



LinkBack URL
About LinkBacks



