0

I am having errors running this code. While executing it gives out an error like-

terminate called after throwing an instance of 'std::out_of_range' what(): basic_string::substr

Any ideas on what I am doing wrong here?

#include <string.h>
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>
#include "fix_nvt.h"
#include "group.h"
#include "modify.h"
#include "error.h"

using namespace LAMMPS_NS;
using namespace FixConst;
using namespace std;

 std::ifstream inFile;

double x,y,z;
string str_val;

/* ---------------------------------------------------------------------- */

FixNVT::FixNVT(LAMMPS *lmp, int narg, char **arg) :
  FixNH(lmp, narg, arg)
{
  if (!tstat_flag)
    error->all(FLERR,"Temperature control must be used with fix nvt");
  if (pstat_flag)
    error->all(FLERR,"Pressure control can not be used with fix nvt");

  // create a new compute temp style
  // id = fix-ID + temp

  int n = strlen(id) + 6;
  id_temp = new char[n];
  strcpy(id_temp,id);
  strcat(id_temp,"_temp");

  char **newarg = new char*[3];
  newarg[0] = id_temp;
  newarg[1] = group->names[igroup];
  newarg[2] = (char *) "temp";

  modify->add_compute(3,newarg);
  delete [] newarg;
  tflag = 1;
  printf("syracuse rocks\n\n\n");
  inFile.open("matrix.txt", ios::in);
  while(getline(inFile,str_val));
    {
  x = atof(str_val.substr(1,4).c_str());
  y = atof(str_val.substr(17,4).c_str());
  z = atof(str_val.substr(33,4).c_str());
  printf ("x = %f, y =%f, z=%f\n",x,y,z);
 }
  inFile.close();



}
derekerdmann
  • 17,696
  • 11
  • 76
  • 110
Peter
  • 17
  • 3
  • 2
    Are you sure 1,17 and 33 are valid indexes for `str_val`? – NathanOliver Apr 13 '16 at 18:56
  • Using a debugger, you could step into and see the values stored into str_val and verify what @NathanOliver just said above – Ceros Apr 13 '16 at 19:00
  • The reason I wrote it is because it reads from a file. matrix.txt and the file has values which start from 1, 17 and 33 numbered columns. I believe that is format right? substr(where it begins,how much space to leave before the next). – Peter Apr 13 '16 at 19:00
  • @Peter look at this http://en.cppreference.com/w/cpp/string/basic_string/substr – Ceros Apr 13 '16 at 19:01
  • Here is a suggestion. In your while loop put `if (str_val.size() < 37) std::cout << "string to small!!!\n";` as the first line. Recompile and let me know if that outputs or not. – NathanOliver Apr 13 '16 at 19:02
  • @NathanOliver it does says string too small. Can you please help me address the problem? – Peter Apr 13 '16 at 19:31
  • @NathanOliver I am reading the contents of a file that I print already and its named as matrix.txt. The file shows content, I am not sure what this error means – Peter Apr 13 '16 at 19:32
  • It means you have a line in your file that is not as bid as you expect. Next step is to print out that line and see what it is. Then you have to figure out how to parse it and all the other data with the same process. – NathanOliver Apr 13 '16 at 19:34

0 Answers0