This is the file I will be calling from, basically it stores something into a file called "temp.txt, so let's assume that it works and the problem is not here. This is the "file.h" file.

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

ofstream output_file("temp.txt", ios::out);

class NUM_STUD:
{

   public:      	
                NUM_STUD (char *, int);
	
                NUM_STUD (int);

	float * ADD_STUD();
	
   private:
	int n;  // number of students
                float A[10];//grade
	char ifile[15];

};

NUM_STUD::NUM_STUD(int x)
{
	n=x;
}

NUM_STUD::NUM_STUD(char * if_name, int x)
{
	int i,j;
	strcpy(ifile, if_name);
	ifstream input_file(ifile, ios::in);
	n = x;
	for(i=0; i<n; i++)
	{
		input_file >> A[i];
	}
}

float * NUM_STUD::ADD_STUD()
{
	int i, j;
	float X[10];
                float num;
                num=0;
	for (i=0; i<n;i++)
	{
	        num+= 1;
	        X[i] = num;
	}
	output_file<<"Answer:" << endl;
	for (i=0; i<n; i++)
	{
	                output_file<< A[i]<<endl;
	}
	return X;
}

This is the second header file which is being used in the main file. fgrades.h

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

#include "/usr/file.h"

class NUM_TEST: public NUM_STUD
{
   public:
	NUM_TEST(int); // constructor; 

	void SCORES (char *); 
		
   private:
	int numb; // number of test
};
 
// constructor code 
NUM_TEST::NUM_TEST(int x)
	:NUM_STUD(6) 
{
        numb = x;
}

void NUM_TEST::SCORES(char * data_file)
{
                int i, j;
                float S[10];
                for(i=0;i<6;i++)
                {
                               S[i]=0.00;
                }
	float X[15], Y[15];//TEST 1 AND TEST 2
	float g;	
	ifstream input_file(data_file, ios::in);   
	
	for(i=0; i<numb; i++)
    	{
		input_file >> X[i]>> Y[i];
    	}
	
	// calculate the average scores
	for(i=0;i<numb;i++)
	{
		S[i]=(X[i]+Y[i])/2;
	}

	// put them into a temp file in the format for NUM_STUD class can read

	ofstream out_file("temp_avg.txt", ios::out); 
                for(i=0;i<6;i++)
                {
	       out_s_file<<S[i]<<endl;
                }

	[B]NUM_STUD temp_m("temp_avg.txt",6); //problem
	temp_m.ADD_STUD();[/B]


	ifstream in_stud("temp.txt",ios::in);
                for (i=0;i<6;i++)
                {
	     in_stud>>m[i];
                }
	
	ofstream output("final_file.text", ios::out);
	output<<"The final grade for students are:"<<endl;
	output<<"USING INHERITANCE:"<<endl;
	output<<setprecision(2)<<setiosflags(ios::fixed|ios::showpoint)<<m<<endl;
}

Main File

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

int main()
{
	NUM_TEST C(5);
	C.SCORES("data");
	return 0;
}

The format of the data file is as follows:

X_0 Y_0
X_1 Y_1
...
X_n-1 Y_n-1

when ran "temp_avg.txt" produces

S_0
S_1
..
S_5

every thing works perfectly up until I use inherentence, the "temp.txt" is empty and i think thats my problem , i may be calling the class wrong ( in bold above) any help would be greatful. heres my errors when ran:

/usr/file.h: In member function âfloat* NUM_STUD ::ADD_STUD()â:
/usr/file.h:line#: warning: address of local variable X returned

out put in "final_file.text" is
The final grade for students are
USING INHERITANCE:

Data from final "temp.txt' is not there. any help with this, don't worry about the inhereted class "ADD_STUD()" it works , but the temp.txt file genreating.

It is complaining because you are trying to return an array that will disappear the instant the function terminates.

I'd swear I just read/responded to this very issue not too long ago, but I can't find it.

Since you aren't using the result of ADD_STUD, just make it void and don't return anything.

If you really do want to return an array, you have to allocate it first:

int *make_ints( int count )
  {
  int *result = new int[ count ];
  for (int i = 0; i < count; i++)
    result[ i ] = count -i;  // whatever
  return result;
  }

But you must also remember to deallocate it once you are done with it.

int *foo = make_ints( 3 );
  for (int i = 0; i < 3; i++)
    cout << foo[i] << endl;
  delete [] foo;

Finally, you can avoid the grief and just use a std::vector or std::deque to do the dirty work:

std::vector<int> make_ints( int count )
  {
  std::vector<int> result;
  for (int i = 0; i < count; i++)
    result.push_back( count -i );  // the same whatever
  return result;
  }

Not only do you not have to clean up after yourself, you get other benefits:

std::vector<int> foo = make_ints( 3 );
  for (int i = 0; i < foo.size(); i++)
    cout << foo[i] << endl;
  // Hey, I don't have to delete/free/whatever 'foo'!

Hope this helps.

I think my real problem is that when I use this code

NUM_STUD temp_m("temp_avg.txt",6); 
temp_m.ADD_STUD();

it generates a empty "temp.txt" file that I'm looking for, I don't if the code above is the proper way to inherit the "NUM_STUD" class. The file.h file can't be changed and is an example of how my real project on matrixes look like. I just want to use inherentence to call a base class function to generate a file so that I can use that file in my new function to output into a new file. Does that make more sense.

At least one note, somewhere along the code you have: output_file<<"Answer:" << endl; and later on you are attempting to read in values from the very same file in_stud>>m[i]; That just silently fails because of the string you have written there (I'm assuming here that m[] holds floats <-> I did not find declaration of 'm' anywhere in the code you've posted).

I found out what my problem was, I posted this sample code to show you my overall srucure not for it to be evaluated. The problem was that the file.h was suppose to generate a temp file I was wrong. I was suppose to use inheretence to populate the members in the file that.h file so that I can use a pointer to get the values that was being return by the float interger X. thanks for your help though.

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.