CFD Online Discussion Forums

CFD Online Discussion Forums (
-   OpenFOAM Programming & Development (
-   -   how to convert string type to scalar type (

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:

int main(int argc, char *argv[])
#  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.

zhajingjing October 16, 2009 07:23

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

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

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