|
-
May 1st, 2009, 04:13 AM
#1
input from a file and storing in array of objects
hi..i m trying to solve an assignment for my own practice with FILEing in c++.
i have found the assignment here (due date was 27th march, so its not my school assignment, only practice)
http://www.cs.fsu.edu/~gaitrosd/clas...ent5/hw05.html
1-i have to make a class student with three derived classes english, history, math with a function that calculates avg.
2-read the data from an input file and store it using an array of appropriate type.(polymorphism)
first line of input file has first and last name.
2nd line of input file has subject name and marks.
3-then output file...
how can i read input file to store data in appropriate object??
this is what i made..i m confused and need help.
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
////////////////////////////////////////////////////////////
class student
{
protected:
string fname,lname,course;
public:
student (string f,string l,string c): fname(f),lname(l),course(c)
{}
};
////////////////////////////////////////////////////////////////
class english : public student
{
private:
int atten,project,midterm,fexam;
public:
english(string n1,string n2, string s, int a, int p, int m, int f):
atten(a),project(p),midterm(m),fexam(f),student(n1,n2,s)
{}
string get_fname() const
{return fname;}
string get_lname() const
{return lname;}
string get_course() const
{return course;}
float avg ()
{return (atten*10/100+project*30/100+midterm*30/100+fexam*30/100);}
};
/////////////////////////////////////////////////////////////////
class history : public student
{
private:
int term,mid,final;
public:
history (string n1,string n2, string s, int t, int m, int f):
term(t),mid(m),final(f),student(n1,n2,s)
{}
string get_fname() const
{return fname;}
string get_lname() const
{return lname;}
string get_course() const
{return course;}
float avg ()
{return (term*25/100+mid*35/100+final*40/100);}
};
/////////////////////////////////////////////////////////////////
class math : public student
{
private:
int quiz1,quiz2,quiz3,quiz4,quiz5,t1,t2,final;
public:
math (string n1,string n2, string s, int q1, int q2, int q3, int q4, int q5, int t1, int t2, int f):
quiz1(q1),quiz2(q2), quiz3(q3), quiz4(q4), quiz5(q5),student(n1,n2,s)
{}
string get_fname() const
{return fname;}
string get_lname() const
{return lname;}
string get_course() const
{return course;}
float avg ()
{return ((quiz1+quiz2+quiz3+quiz4+quiz5)*15/100 + t1*25/100 + t2*25/100+final*35/100);}
};
///////////////////////////////////////////////////////////////////
int main ()
{
char fileName[30];
cout<<"Enter File Name: ";
cin>>fileName;
string name1,name2,sub;
int num,a,p,m,f;
ifstream fin(fileName);
if (fin.fail())
{cout<<"Sorry, Input File not found.."<<endl;exit (1);}
fin.get(num);
fin.get(
fin.close();
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|