0

In the following code:

  atnames = new char[natoms];
  xyzs = new double*[natoms];
  for(int iat=0;iat<natoms;iat++)
  {
    int idum1;
    xyzs[iat] = new double[3];
    for(int ii = 0; ii<3; ii++)
    {
      xyzs[iat][ii] = 0.0;
    }
    getline(finp, tline);
    sscanf(tline.c_str(), "%c(%i) %f %f %f ATOM", &atnames[iat], &idum1, &xyzs[iat][0], &xyzs[iat][1], &xyzs[iat][2]);
  }

  for(int iat=0; iat<natoms; iat++)
  {
    cout << atnames[iat] << iat+1 << ":" << xyzs[iat][0] <<  " " << xyzs[iat][1] << " " << xyzs[iat][2] << endl;
  }

I am trying to read the lines:

O(1)       1.23799   0.00000   0.00000 ATOM
N(2)      -0.75911   1.08623   0.00000 ATOM
C(3)       0.00000  -0.00000   0.00000 ATOM
H(4)      -1.75297   0.97208   0.03384 ATOM
H(5)      -0.30916   1.98585  -0.01442 ATOM
H(6)      -0.52646  -0.95018   0.03999 ATOM

However, when I print out the values, I get:

O1:5.27341e-315 0 0
N2:1.58536e-314 5.26712e-315 0
C3:0 0 0
H4:1.59047e-314 5.26123e-315 5.05975e-315
H5:1.58004e-314 5.3044e-315 1.56185e-314
H6:1.58343e-314 1.58694e-314 5.06791e-315

So I am obviously reading the first char correctly, but am not storing the values of the floating point numbers correctly into the array xyz. I feel like this is a referencing/dereferencing problem, but I'm unclear as to what I'm doing wrong.

If I try to read the values directly to xyzs[iat][0] instead of &xyzs[iat][0], I get a seg-fault.

1 Answer 1

2

Use %f for float variables.

Use %lf for double variables.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.