Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Passing values using class notation (http://www.go4expert.com/forums/passing-values-using-class-notation-t1099/)

can342man 31Jul2006 18:42

Passing values using class notation
 
Hello,

I am trying to input a parameter from an external file into this program. The subroutine does import the integer parameter 50 in the program and prints its result on the screen. My problem is trying to pass the value of the parameter out of the subroutine and into the main function using the class notation and reference method. Can anyone tell me if I am using the class and reference notation properly?

Thanks really appreciate it.
:)

Code:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

class mdParam {
public:
  int nMol;
};

void mdPar(mdParam &Param);

int main ()
{
mdParam Param;
   
mdPar(Param);
cout << "This is returned = " << Param.nMol << endl;

}


/* PARAMETERS FROM EXTERNAL */
void mdPar(mdParam &Param)
{
ifstream mdIn("MDPARAMTEST.log");

  char line[129], line2[129];
  int i, j, len;
  double dt, dtemp;

  int iLines = 0;
  do{
    iLines ++;
    while (mdIn.peek() == '#'){
      mdIn.getline(line,128);
    }
    if(mdIn.peek() == EOF){
      cerr << "EOF" << "nLines = " << iLines << endl;
      break;
    }
    // read text up to =
    mdIn.getline(line,128,'=');

    len = strlen(line);
    for(i=0, j=0; j<=len; j++){
      if( line[j] == '\n' ){
        i = 0;
        continue;
      }
      if( line[j] != ' ' ){
        line2[i] = line[j];
        i++ ;
      }
    }

    cerr << line << endl;
    cerr << line2 << endl;

    if (strcasecmp("nMol",line2) == 0){
      mdIn >> Param.nMol;
      mdIn.getline(line,128);
      if(Param.nMol < 0 || Param.nMol > 800){
        cerr << "Number of molecules = "  << endl;
        exit(1);
      }
      cout << "nMol = " << Param.nMol << endl;
    }
   
  }while(mdIn.peek() != EOF);
 
  cerr << "EOF2" << "nLines2 = " << iLines << endl;
mdIn.close();
return; 
}


shabbir 31Jul2006 21:21

Re: Passing values using class notation
 
Check if you are using the exit(1) function correctly. It does mean you are exiting the program and not the function. Use break/return statement if appropriate.

Offtopic comment:
This is my 1,000th post by the way


All times are GMT +5.5. The time now is 05:40.