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.