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();

}

Recommended Answers

All 3 Replies

It is very unlikely that someone is going to look through your 300 lines of code. Please whittle the code down to a ~20 line compilable example of the problem, along with sample input, expected output, and current (incorrect) output.

Dave

It is very unlikely that someone is going to look through your 300 lines of code. Please whittle the code down to a ~20 line compilable example of the problem, along with sample input, expected output, and current (incorrect) output.

Dave

Sorry. I really dont know how i can split it up with it still compiling, due to the fact that i need all the loops to be closed. I realise it is a bit long. How about if i indicate the area that i know is going wrong.

If you run this, only enter p for physics when prompted and e for Em when prompted as i have only set up these instances in the switches.

If you run this next bit of code you will see the desired output come up before the question is asked "How many students do you wish to enter?" This output is what i would like to be sent to the output file. i will indicate the area i know is going wrong with <-----HERE

sorry i know this is a nuisance, but im very new to this and dont fully know what im doing. Your help would be much appreciated.

Thanks

#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);
	physstd a("terry",7036616,"EM","A",100);
	cout<<a;
	
	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);
					out<<store[j];   //<---------HERE     
					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);
					out<<store[j];
					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);
					out<<store[j];
					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;
				
	}
	delete[] stds;
	out.close();

}

You need to track down where your error occurs. It can be in one or more of at least three places:

  1. Bad parameters passed to the constructor on line 196. Display the parameters before the call. If they are incorrect, debug prior to line 196.
  2. Good parameters passed to the constructor, but the constructor doesn't initialize them correctly. Display the values att he end of the constructor code. Make sure they are right.
  3. Problem in the << operator code. Display the values to the screen in the << function. Make sure they are correct.

Those would be the big three categories. Figure out which of the three is the problem, then debug from there. If it's a problem in the constructor, place some more debugging statements in the constructor until you see precisely where it fails.

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.