CFD Online Discussion Forums

CFD Online Discussion Forums (http://www.cfd-online.com/Forums/)
-   OpenFOAM Programming & Development (http://www.cfd-online.com/Forums/openfoam-programming-development/)
-   -   how to convert string type to scalar type (http://www.cfd-online.com/Forums/openfoam-programming-development/69225-how-convert-string-type-scalar-type.html)

zhajingjing October 15, 2009 22:48

how to convert string type to scalar type
 
Hello everyone,

In a small post-processing program, I have to convert a string into a scalar:
Code:

int main(int argc, char *argv[])
{
    timeSelector::addOptions();
    argList::validArgs.append("fieldName");
    argList::validArgs.append("xValue");
#  include "setRootCase.H"
#  include "createTime.H"
    instantList timeDirs = timeSelector::select0(runTime, args);
#  include "createMesh.H"

    word fieldName(args.additionalArgs()[0]);
  word xValue(args.additionalArgs()[1]);
   
    Info <<xValue<<endl;
   
    ......    // ?
  scalar  x=      // 
get the value from xValue, but xValue is a string---How???

Thanks for any help!

l_r_mcglashan October 16, 2009 04:20

You could use 'strtod' from the cstdlib.

http://www.cplusplus.com/reference/c...stdlib/strtod/

zhajingjing October 16, 2009 07:23

I add the following,It worked.Thank you very much.
Code:

#include <cstdlib>
...
  char* pEnd;
  scalar x=strtod(xValue.c_str(),&pEnd);



All times are GMT -4. The time now is 22:17.