1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Passing values using class notation

Discussion in 'C++' started by can342man, Jul 31, 2006.

  1. can342man

    can342man New Member

    Joined:
    Jul 31, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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;  
    }
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,293
    Likes Received:
    365
    Trophy Points:
    83
    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.

    [comment]This is my 1,000th post by the way[/comment]
     

Share This Page