Hi everyone, I am doing a C++ project for school in the UNIX lab that requires inheritance. My main program is calculating linear regression for some plotted points and I'm inheriting a linear system solver from another program. The linear system solver is suppose to print the solved values to a text file and my main program is to read that file and use that data for the regression but for some reason the solver doesn't write anything to the file. The solver code was given by my professor so I don't think anythings wrong with it so maybe its something I'm not doing.

Here is a sample input file for the main program:

0.0  1.0
1.0  3.1
2.1  5.2
3.1  6.9
4.2  9.1

Here is the main code:

#include "p7.h"
#include <fstream.h>
#include <iostream.h>

int main()
{
	EZ_CURVE_FIT C(5);
	C.LS_FIT_BY_ME("in_7_1");
	return 0;
}

Here is my header p7.h

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>

#include "/usr/tools/EE259/IO/base_p6.h"

class EZ_CURVE_FIT: public EZ_MATH
{
   public:
	EZ_CURVE_FIT(int); // constructor; 
		// example: s.EZ_CURVE_FIT(5);
		// there are 5 pairs of data points;

	void LS_FIT_BY_ME(char *); 
		// example: s.LS_FIT_BY_ME("data_file");
		// perform least squares fit using inheritance 
		// data is in "data_file";

   private:
	int np; // number of data pairs
};
 
// constructor code 
EZ_CURVE_FIT::EZ_CURVE_FIT(int x)
	:EZ_MATH(2) // call base class constructor, (we need to solve 
	   // a linear equation system of AX=B, Dimension of A is 2X2) ;
{
        np = x;
}

void EZ_CURVE_FIT::LS_FIT_BY_ME(char * data_file)
{
	ifstream input_file(data_file,ios::in);

	float X[50],Y[50];
	int i,j;
   
   	for (i=0;i<np;i++)
	{
 		input_file >> X[i] >>Y[i];
	}
    
	float s1=0, s2=0, s3=0, s4=0, s5=0, s6=0;

	for(i=0;i<np;i++)
	{
		s1 += (X[i]*X[i]);
		s2 += X[i];
		s3 += (X[i]*Y[i]);
		s4 += X[i];
		s5 += 1;
		s6 += Y[i];
	}

	ofstream out_in6("in_6",ios::out); 
	
	out_in6 << s1 << " " << s2 << endl;
	out_in6 << s4 << " " << s5 << endl;
	out_in6 << s3 << endl;
	out_in6 << s6 << endl;

	EZ_MATH temp_m("in_6",np);
	temp_m.EZ_MATH::EZ_GE_BY_ME();

}

Here is the inherited header called base_p6:

#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#include "/usr/tools/EE259/IO/sample_p5.h"

ofstream the_output_file_16("out_61", ios::out);


class EZ_MATH: public MATRIX
{

   public:      // public interfaces for this class
	
	EZ_MATH (char *, int); // example: EZ_MATH s("i_file", 3);
		   // creates an object s from i_file with 3 equations

	EZ_MATH (int);

	float * EZ_GE_BY_ME();
	

   private:
	int n;  // 
	float AA[10][10];
	float B[10];
	char in_file[15];   //array to hold the input file name
};
	
EZ_MATH::EZ_MATH( int x)
	:MATRIX(x, x) // initialize base class MATRIX with dim1=x, dim2=x;
{

	n=x;
}

EZ_MATH::EZ_MATH(char * if_name, int x)
	:MATRIX(x, x) // initialize base class MATRIX with dim1=x, dim2=x;
{
	// your code for EZ_MATH constructor goes here
	int i,j;
	strcpy(in_file, if_name);
	ifstream input_file(in_file, ios::in);
	
	n = x;
	//read elements of AA and B from if_name
	
	for(i=0; i<n; i++)
	{
		for(j=0; j<n; j++)
		{
			input_file >> AA[i][j];
		}
	}

	for(i = 0; i < n; i++)
	{
		input_file >> B[i];
	}
	//the_output_file_16<<"+++ START OUTPUT FROM SAMPLE_P6.H:" << endl;
	//the_output_file_16<<"+++ MATRIX AA and B HAVE BEEN CREATED"<<endl;
	//the_output_file_16<<"+++ END OUTPUT FROM SAMPLE_P6.H:" << endl;
}

float * EZ_MATH::EZ_GE_BY_ME()
{
	int column, row, i, j, var;
	int end = 1;
	float temp;
	int z;

	for (column=0; column<n; column++)
	{
		if (AA[column][column] == 0)  //pivot in case diagonal element=0
		{
			int k;
			end = 0;
			for (k = column+1; (k<n && end==0); k++)
			{
				if (AA[k][column] != 0) //found non-zero diagonal element; swap lines
				{
					temp = B[column];
					B[column] = B[k];
					B[k] = temp;
					for (z=0; z<n; z++)
					{
						temp = AA[column][z];
						AA[column][z] = AA[k][z];
						AA[k][z] = temp;
					}
					end = 1;
				}
				else
				{}
			}
			if (end == 0) //singularity
			{
				//the_output_file_16<<"+++ START OUTPUT FROM SAMPLE_P6.H:" << endl;
				//the_output_file_16 <<"+++ MY GAUSSIAN ELIMINATION SOLUTION:" <<endl;
				//the_output_file_16 <<"+++ SYSTEM IS SINGULAR" <<endl;
				//the_output_file_16<<"+++ END OUTPUT FROM SAMPLE_P6.H:" << endl;
			}
			else
			{}
		}
		else
		{}

		for (row=column+1; row<n && end!=0; row++)
		{
			float multiplier;
			multiplier = (-AA[row][column])/(AA[column][column]);
			for (i=column; i<n; i++)
			{
				AA[row][i] += multiplier * AA[column][i];
			}
			B[row] += multiplier * B[column];
		}
	}
	float X[10];
	for (row=n-1; row>=0 && end!=0; row--)
	{
		float accumulation=0;
		for (var=n-1; var>row; var--)
		{
			accumulation += AA[row][var] * X[var];
		}
		X[row] = (B[row] - accumulation)/(AA[row][row]);
	}

	if (end != 0)
	{
		the_output_file_16<<"+++ START OUTPUT FROM SAMPLE_P6.H:" << endl;
		the_output_file_16 <<"+++ MY GAUSSIAN ELIMINATION SOLUTION:" <<endl;
		for (i=0; i<n; i++)
		{
		the_output_file_16 <<"+++ X["<<i<<"]="<<setprecision(2) <<setiosflags(ios::fixed | ios::showpoint) <<X[i] <<endl;
		}
		the_output_file_16<<"+++ END OUTPUT FROM SAMPLE_P6.H:" << endl;
	}
	return X;
}

And here is the inherited header for the inherited header called sample_p5.h:

#include<iostream.h>
#include<fstream.h>
#include<stdlib.h>
#include<string.h>


ofstream output_file("out.5", ios::out);

class MATRIX: public PROGRAM_BANK{
public:	// public interfaces for this class
	
	  MATRIX (int, int); 	// constructor;
				// example: MATRIX X(d1,d2);
			   	// creates a matrix object with
			   	// private variables dim1 = d1 and dim2 = d2
			  	// and initializes each element to 0;
  
	  MATRIX(int, int, char *); 
	  			// constructor;
				// example: MATRIX X(d1,d2,"in_name");
				// read the elements of the matrix
				// from a given file in_name; d1 and d2 are 
				// the two dimensions of the matrix;
				
private:

  	  int dim1, dim2; 	// dimensions of the matrix;
				// if dim2 is 1, a vector (no need for special
				// handling of vectors; everything should work
				// for both matrices and vectors)

  	  double A[20][20]; 	// 2 dimensional array for the matrix elements;
	  char in_file[11]; 	// hold the input file name
 };
// constructor
MATRIX::MATRIX(int d1, int d2)
        :PROGRAM_BANK(d1) // use the inheritance
{
     int i, j;
     dim1 = d1;
     dim2 = d2;
     
     if(d1 < 0 || d1 > 20 || d2< 0 || d2 >20)
     {
          exit(1);
     }
     
     for (i= 0; i<dim1; i++)
     {
         for (j= 0; j<dim2; j++)
         {
             A[i][j] = 0;
         }
     }

}

// constructor
MATRIX::MATRIX(int d1, int d2, char * file_name)
        :PROGRAM_BANK(d1, file_name) // use the inheritance
{
     int i, j;
     output_file << "+++ P5 START +++++++++++++++++++++++++++++++++++++++++" << endl;
           
     if(d1 < 0 || d1 > 20 || d2< 0 || d2 >20)
     {
           output_file <<"+++ P5_OUTPUT >>> INPUT ERROR" 
           	       << endl
           	       <<"+++ P5_OUTPUT >>> UNABLE TO CREATE MATRIX" 
           	       << endl
           	       <<"+++ P5_OUTPUT >>> EXITING PROGRAM" 
           	       << endl
          	       << "+++ P5 END +++++++++++++++++++++++++++++++++++++++++++" 
          	       << endl;
           exit(1);
     }    
     else
     {
          dim1 = d1;
          dim2 = d2;
          
          // copy the input file name to private variable in_file
	  strcpy(in_file, file_name);

	  //define the input_filein this current block
 	  ifstream input_file(in_file, ios::in);
         
          
          for (i =0; i<dim1; i++)
          {
              for(j = 0; j<dim2; j++)
              {
                 input_file >> A[i][j];
              }
          }
          
          output_file <<"+++ P5_OUTPUT >>> CREATED A " << dim1 <<" X " << dim2 
                      <<" MATRIX FROM " << in_file << endl;

     } 
	
     output_file << "+++ P5 END +++++++++++++++++++++++++++++++++++++++++++" << endl;

}

From where my main code ends, I'm expecting a file called out_61 with the solved system but I get a blank file. I can cout before the ofstream write of these headers and find that the correct data was calculated but it will not print it to the file and all the streams are declared in all headers. So if anyone can help, I would be thankful.

Why not make the ofstream elements members of their respective classes? (and instantiate them in the class constructor) I'm not sure if that will solve the problem, but it will make your code neater at a minimum, and global variables are never a good idea.

Also, with the standards, the old iostream.h was replaced by iostream, etc. and string.h by cstring.

Why not make the ofstream elements members of their respective classes? (and instantiate them in the class constructor) I'm not sure if that will solve the problem, but it will make your code neater at a minimum, and global variables are never a good idea.

Also, with the standards, the old iostream.h was replaced by iostream, etc. and string.h by cstring.

The professor created and locked all of the headers except the header I have to edit also he gave us the shell for that header.So I can't really modify anything dealing with ofstream.

Hmmm. I'm not sure then. You don't actually close the files anywhere, though. Is that outside of your control, too? Otherwise I'm at a loss.

Hmmm. I'm not sure then. You don't actually close the files anywhere, though. Is that outside of your control, too? Otherwise I'm at a loss.

I also saw that he didn't close those files either and I can't do nothing to close the files in those inherited headers because they are locked. I'll try and copy those headers and make those modifications to see what happened. Thanks for the help though.

I also saw that he didn't close those files either and I can't do nothing to close the files in those inherited headers because they are locked. I'll try and copy those headers and make those modifications to see what happened. Thanks for the help though.

Yeah it didn't work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.