Hi im very new to this but i am trying to write a progam that serves as a student database for a project at uni. Ive been stuck on this problem for ages now and can seem to spot the probably obvious mistake ive made. The code compiles but the problem is that the output file ive asked my data to be sent to is just a line of random letters and numbers. Ive only incorporated this output in the first 'case' of the second 'switch', but will put it in the rest of the cases ater after i can get this case to work.Here is the code, any help would be much appreciated ...
also if you run it, only select p for physics, and e for em because thats all im set up for, and you have to put a full stop after entering your name lol, i know its a bit doggy.
#include<iostream>
#include<string>
#include <fstream>
using namespace std;
class student
{
protected:
string name;
int ID;
static int nostds;
public:
student()
{
name = "no name";
ID = 0;
nostds = 0;
}
student (string pname,int pID)
{
name = pname;
ID = pID;
nostds++;
}
~student(){nostds--;}
string getname()const{return name;}
int getID()const{return ID;}
virtual void print()=0;
student(const student &v)
{
name = v.getname(); ID=v.getID();
}
friend ostream &operator<<(ostream &os, const student &ob);
};
ostream &operator<<(ostream &os, const student &ob)
{
os<<"| "<<ob.name<<" | "<<ob.ID<<"|"<<endl;
return os;
}
int student::nostds(0);
class physstd : public student
{
private:
string course;
string grade;
int percentage;
public:
physstd()
{
course = "Null";
grade = "Null";
percentage = 0;
}
physstd(string name,int ID,string pcourse, string pgrade, int ppercentage):student(name,ID)
{
course = pcourse;
grade = pgrade;
percentage = ppercentage;
}
~physstd(){}
//void add();
void print()
{
cout<<"PHYSICS:";
cout<<"| Entry number "<<nostds<<"| "<<name<<" | "<<ID<<" | "<<course<<" | "<<grade<<" | "<<percentage<<"% |"<<endl<<endl;
}
friend ostream &operator<<(ostream &os, const physstd &ob);
};
ostream &operator<<(ostream &os,const physstd &ob)
{
os<<"PHYSICS:";
os<<"| "<<ob.name<<" | "<<ob.ID<<" | "<<ob.course<<" | "<<ob.grade<<" | "<<ob.percentage<<"% |"<<endl<<endl;
return os;
}
class chemstd : public student
{
private:
string course;
string grade;
int percentage;
public:
chemstd()
{
course = "Null";
grade = "Null";
percentage = 0;
}
chemstd(string name,int ID,string pcourse, string pgrade, int ppercentage): student(name,ID)
{
course = pcourse;
grade = pgrade;
percentage = ppercentage;
}
~chemstd(){}
//void add();
void print()
{
cout<<"CHEMISTRY:";
cout<<"| Entry number "<<nostds<<"| "<<name<<" | "<<ID<<" | "<<course<<" | "<<grade<<" | "<<percentage<<"% |"<<endl<<endl;
}
};
class mathstd : public student
{
private:
string course;
string grade;
int percentage;
public:
mathstd()
{
course = "Null";
grade = "Null";
percentage = 0;
}
mathstd(string name,int ID,string pcourse, string pgrade, int ppercentage): student(name,ID)
{
course = pcourse;
grade = pgrade;
percentage = ppercentage;
}
~mathstd(){}
//void add();
void print()
{
cout<<"MATHS";
cout<<"| Entry number "<<nostds<<"| "<<name<<" | "<<ID<<" | "<<course<<" | "<<grade<<" | "<<percentage<<"% |"<<endl<<endl;
}
};
int main()
{
// Open Outputfile
const char outfile[] = "Output.txt";
ofstream out(outfile);
int nstds;
cout<<"How may students do you wish to enter?"<<endl<<endl;
cin>>nstds;
student **stds=new student*[nstds];
char choicesubject;
char choicecourse;
for(int i=0;i<nstds;i++)
{
int nocourses;
string pname;
int pID;
string pcourse;
string pgrade;
int ppercentage;
cout<<"Student "<<i+1<<", enter Physics, Chemistry or Maths [P,C,M]..."<<endl;
cin>>choicesubject;
cout<<"How many courses is this student taking?"<<endl;
cin>>nocourses;
physstd **store = new physstd*[nocourses];
switch (choicesubject)
{
case 'P':
case 'p':
cout<<"Enter name of student..."<<endl;
getline (cin,pname,'.');
cout<<"Enter student id number..."<<endl;
cin>>pID;
char choicecourse;
for(int j=0;j<nocourses;j++)
{
string pcourse;
string pgrade;
int ppercentage;
cout<<"Course "<<j+1<<" Is the student taking EM, Particle or C++ [E,P,C]?"<<endl;
cin>>choicecourse;
switch(choicecourse)
{
case 'E':
case 'e':
pcourse="EM";
cout<<"Enter the grade for the student taking EM..."<<endl;
cin>>pgrade;
cout<<"enter the percentage for the student taking EM..."<<endl;
cin>>ppercentage;
store[j]= new physstd(pname,pID,pcourse,pgrade,ppercentage);
physstd *temp;
temp=store[j];
out<<temp;
break;
case 'P':
case 'p':
pcourse="Particle";
cout<<"Enter the grade for the student taking Particle..."<<endl;
cin>>pgrade;
cout<<"enter the percentage for the student taking Particle..."<<endl;
cin>>ppercentage;
store[j]= new physstd(pname,pID,pcourse,pgrade,ppercentage);
physstd *temp2;
temp2=store[j];
out<<temp;
break;
case 'C':
case 'c':
pcourse="C++";
cout<<"Enter the grade for the student taking C++..."<<endl;
cin>>pgrade;
cout<<"enter the percentage for the student taking C++..."<<endl;
cin>>ppercentage;
store[j]= new physstd(pname,pID,pcourse,pgrade,ppercentage);
physstd *temp3;
temp=store[j];
out<<temp3;
break;
default:
cout<<"Incorrect course"<<endl;
break;
delete[] store;
}
}
//stds[i] = new physstd(pname,pID,pcourse,pgrade,ppercentage);
break;
case 'C':
case 'c':
cout<<"Enter name of student..."<<endl;
getline (cin,pname,'.');
cout<<"Enter student id number..."<<endl;
cin>>pID;
cout<<"Enter physics course "<<"..."<<endl;
cin>>pcourse;
cout<<"Enter the students grade in this course..."<<endl;
cin>>pgrade;
cout<<"Enter the students percentage in this course..."<<endl;
cin>>ppercentage;
stds[i] = new chemstd(pname,pID,pcourse,pgrade,ppercentage);
break;
case 'M':
case 'm':
cout<<"Enter name of student..."<<endl;
getline (cin,pname,'.');
cout<<"Enter student id number..."<<endl;
cin>>pID;
cout<<"Enter physics course "<<"..."<<endl;
cin>>pcourse;
cout<<"Enter the students grade in this course..."<<endl;
cin>>pgrade;
cout<<"Enter the students percentage in this course..."<<endl;
cin>>ppercentage;
stds[i] = new mathstd(pname,pID,pcourse,pgrade,ppercentage);
break;
default:
cout<<"Not a valid course"<<endl;
break;
}
cout<<endl<<endl<<endl;
}
/*for(int i=0;i<nstds;i++)
{
stds[i]->print();
delete stds[i];
stds[i]=NULL;
}*/
delete[] stds;
/*student *pp;
physstd s;
physstd s1("Terence Britton",70366161,"C++","A*",100);
pp=&s; pp->print();
pp=&s1; pp->print();*/
out.close();
}