Hi, I've nearly finished the program but there are some errors saying there are missing semi colons. However when I put them in I get so many errors :(
Here is the code. I know that just because it says there are missing ; does not ALWAYS mean they should be there. Any help? Much appreciated :)
There are a lot of errors in your program. I would suggest you start small, with just constructor and destructor, see if it compiles. Then add functions one at a time. Here are some examples of what is wrong:
1. In the destructor you did not have a closing }
2. need to include <string>
3. The definition of nStudent is string nStudent();, but the implementation is course::nStudent(). If your definition is correct, the implementation should be string course::nStudent()
4. In the same function, you need to return something.
5. In the same function, student in the string student or the class student?
6. Try to explain what is the meaning of student(); statement.
There might be more, but that's how far I checked. Like I mentioned, start with the minimal implementation and see if you have any errors.
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <iostream>
usingnamespace std;
class student
{
protected:
string surname;
int enNo;
float nMark;
public:
student();
~student();
string getSurname();
int getEnNo();
float tMark;
float getNMark();
float getTMark();
};
student::student()
{
cout << "A new student is enrolling. Please enter the new student's surname: ";
getline(cin, surname);
cout << "Please enter their enrolment number: ";
cin >> enNo;
tMark = 0;
}
student::~student()
{
cout << "The student ";
cout << surname;
cout << "is leaving and their total mark was: ";
cout << tMark;
getchar();
}
string student::getSurname()
{
return(surname);
}
int student::getEnNo()
{
return (enNo);
}
float student::getTMark()
{
return (tMark);
}
float student::getNMark()
{
cin >> nMark;
tMark += nMark;
return (tMark);
}
int main()
{
system("cls");
student studScore;
cout << studScore.student::getSurname() << " has enrolled and their enroll number is ";
cout << studScore.student::getEnNo();
char again;
do
{
cout << ". Please enter their mark from their latest test: ";
cout << studScore.student::getNMark();
cout << "Their total score is now ";
cout << studScore.student::getTMark();
cout << ". Do you want to enter another mark? (Type 'Y' or 'N')";
cin >> again;
} while (again != 'N' && again != 'n');
cin.get();
}
But I'm having trouble modifying it so I can allow a student to enrol on the course one at a time. I want to record the course name, use a flag to say if a student is enrolled or not, and the course constructor asks for the course name. The destructor is supposed to show the course name etc. as well as a student is enrolled/shows their enroll number.
And I think like with student the main part should have an auto object of class course with a name.
I think I need a do loop as well to be able to let the user say when a student is recruited, left or finished a test. Only one student enrols at a time. And no test can be taken if no student is enrolled.
I'm not sure what you mean with the closing brace for the destructor. They all seem there to me. Which line is it missing?
I've corrected 2 and 3. Have I returned correctly? And have I also done 4 and 5 corrections correctly? I've removed student(); also.
are old. I don't even know why you've included stdio.h and conio.h?
you need to include:
#include <string>
edit: one more comment: I wouldn't do any coding (as such) in class constructors or destructing (with the exception of your trace statements I guess. Ctor's and dtor's are there so you can initialise and clear up the object correctly.
i'd move this:
1 2 3 4
cout << "A new student is enrolling. Please enter the new student's surname: ";
getline(cin, surname);
cout << "Please enter their enrolment number: ";
cin >> enNo;
into a method called getStudentDetails, or something like that, and i'd move all of the code currently in your destructor into another method, especially your call to getchar(). this is very bad in a destructor.
Right I have changed a few bits but don't know if I've done anything wrong because I am stuck with the semicolon missing thing and can't see if any more errors are found.
#include <iostream> // declaring header files so I can use the code
#include <string>
usingnamespace std;
class student //declaring class student
{
protected: //this means these variables are used in the functions
string surname; //variables where data is entered and stored
int enNo;
float nMark;
public: //alowed to use these functions in main program
student(); //constructor
~student(); //destructor
string getSurname();
int getEnNo();
float tMark;
float getNMark();
float getTMark();
};
class course //declaring class course. Similar contents to class student
{
protected:
string student;
int enNo;
float tMark;
string cName;
int *CoursePtr;
bool enrollYN;
public:
course();
~course();
string nStudent();
string lStudent();
string getCourse();
bool getEnrollYN();
string getStudent();
float getNMark();
float getTMark();
};
course::course() //course constructor, in which course name is entered.
{
cout << "A new course has been created. Please enter the title of the course: ";
getline(cin, cName);;
}
course::~course() //destructor when the program ends.
{
cout << "The course ";
cout << cName;
cout << " has been deleted.";
if (enrollYN == 1)
{
cout << "The student ";
cout << student;
cout << " with the enrolment number ";
cout << enNo;
cout << " is still enrolled with a total mark of ";
cout << tMark;
}
course::nStudent()
{
enrollYN = 1;
return(student)
}
course::lStudent()
{
cout << "The student ";
cout << surname;
cout << "is leaving and their total mark was: ";
cout << getTMark();
getchar();
enrollYN = 0;
}
course::getCourse()
{
return(cName);
}
course::getEnrollYN()
{
if (enrollYN = 0)
{
return(0)
elseif (enrollYN = 1)
return (1)
}
}
student::student()
{
cout << "A new student is enrolling. Please enter the new student's surname: ";
getline(cin, surname);
cout << "Please enter their enrolment number: ";
cin >> enNo;
tMark = 0;
}
student::~student()
{
cout << "The student ";
cout << surname;
cout << "is leaving and their total mark was: ";
cout << student::getTMark();
getchar();
}
string student::getSurname()
{
return(surname);
}
int student::getEnNo()
{
return (enNo);
}
float student::getTMark()
{
return (tMark);
}
float student::getNMark()
{
cin >> nMark;
tMark += nMark;
return (tMark);
}
void main(void)
{
clrscr();
student studScore;
course studCourse;
cout << studScore.student::getSurname() << " has enrolled and their enroll number is ";
cout << studScore.student::getEnNo();
cout << ". "
cout << studCourse.course()::getCourse() << " is the title of the course they are enrolling on.";
char again, rlc;
do
{
cout << "Type 'r' to recruit a new student, 'l' if a student has left and 'c' if a student has completed a test. "
cin >> rlc;
} while (rlc != 'n' && rlc != 'l' && rlc != 'c')
if (rlc == 'r')
{
if (enrollYN == 1)
{
cout << "There is already a student enrolled. Please type 'l' if they have left.";
}
cout << "Please enter the new student: ";
cin << student.course::nStudent();
elseif (rlc == 'l')
{
cout << student.course::lStudent();
elseif (rlc == 'c')
{
do
{
if (enrollYN == 0);
{
"There is no student enrolled. Please type 'r' to recruit a student";
}
cout << ". Please enter their mark from their latest test: ";
cout << studScore.student::getNMark();
cout << "Their total score is now ";
cout << studScore.student::getTMark();
cout << ". Do you want to enter another mark? (Type 'Y' or 'N')";
cin >> again;
} while (again != 'N' && again != 'n');
}
}
}
if (enrollYN == 1);
{
course::~course()
}
cin.get();
}
}
}}
They are from the working program without the course.
well it didnt work for me because my compiler (quite rightly) didn't like #include<string.h>
and wanted #include<string>
how old are you course notes?
i can see you don't want to listen to my advice about having code in your constructor and destructor... Fair enough.
and there's now a load of syntax errors. what IDE/compiler are you using? Are they telling you line numbers of where the errors are? one example, when i try and compile your code, one error is:
course::nStudent()
{
enrollYN = 1;
return(student) // <-- compiler told me you are missing a semi-colon here
}
most of your errors are similar to that.
one major thing: you seem to be trying to define methods inside your destructor.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
course::~course()
{
cout << "The course ";
cout << cName;
cout << " has been deleted.";
if (enrollYN == 1)
{
cout << "The student ";
cout << student;
cout << " with the enrolment number ";
cout << enNo;
cout << " is still enrolled with a total mark of ";
cout << tMark;
} // <- this is the closing brace for you 'if', not your destructor.
Thanks for replying. I'm using Dev C++ and tells me the line numbers. I transitioned from Borland code. Perhaps I didn't understand what I needed to do for cons/destructors?
I've added ; after all return statements and added extra } after the if.
How do I avoid " trying to define methods inside your destructor."?
I get LOADS of errors after changing the code :( is this mostly due to the above?
#include <iostream> // declaring header files so I can use the code
#include <string>
usingnamespace std;
class student //declaring class student
{
protected: //this means these variables are used in the functions
string surname; //variables where data is entered and stored
int enNo;
float nMark;
public: //alowed to use these functions in main program
student(); //constructor
~student(); //destructor
string getSurname();
int getEnNo();
float tMark;
float getNMark();
float getTMark();
};
class course //declaring class course. Similar contents to class student
{
protected:
string student;
int enNo;
float tMark;
string cName;
int *CoursePtr;
bool enrollYN;
public:
course();
~course();
string nStudent();
string lStudent();
string getCourse();
bool getEnrollYN();
string getStudent();
float getNMark();
float getTMark();
};
course::course() //course constructor, in which course name is entered.
{
cout << "A new course has been created. Please enter the title of the course: ";
getline(cin, cName);;
}
course::~course() //destructor when the program ends.
{
cout << "The course ";
cout << cName;
cout << " has been deleted.";
if (enrollYN == 1) //if a student is enrolled
{
cout << "The student ";
cout << student;
cout << " with the enrolment number ";
cout << enNo;
cout << " is still enrolled with a total mark of ";
cout << tMark;
}
}
course::nStudent() //function for if student is enrolled
{
enrollYN = 1;
return(student); //return student surname
}
course::lStudent() //student leaving function
{
cout << "The student ";
cout << surname;
cout << "is leaving and their total mark was: ";
cout << getTMark();
getchar();
enrollYN = 0; //now left there is no student enrolled
}
course::getCourse() //get course name function
{
return(cName);
}
course::getEnrollYN() //function for if student is enrolled or not
{
if (enrollYN = 0) //if not
{
return(0); //0 means they are not enrolled.
elseif (enrollYN = 1) //else they are
return (1); //1 means they are enrolled
}
}
student::student()//student constructor
{
cout << "A new student is enrolling. Please enter the new student's surname: ";
getline(cin, surname);
cout << "Please enter their enrolment number: ";
cin >> enNo;
tMark = 0; //starts with no mark
}
student::~student() //destructor when program ends
{
cout << "The student ";
cout << surname;
cout << "is leaving and their total mark was: ";
cout << student::getTMark();
getchar();
}
string student::getSurname() //return surname function
{
return(surname);
}
int student::getEnNo() //return their enrol number function
{
return (enNo);
}
float student::getTMark() //total mark function
{
return (tMark);
}
float student::getNMark() //new mark function
{
cin >> nMark;
tMark += nMark; //short way of putting tMark = tMark + nMark;
return (tMark);
}
void main(void) //main program
{
clrscr();
student studScore; //total mark for student
course studCourse; //course name
cout << studScore.student::getSurname() << " has enrolled and their enroll number is ";
cout << studScore.student::getEnNo();
cout << ". "
cout << studCourse.course()::getCourse() << " is the title of the course they are enrolling on.";
char again, rlc; //again for when entering another mark, rlc for when a student is recruited, left or completed test
do
{
cout << "Type 'r' to recruit a new student, 'l' if a student has left and 'c' if a student has completed a test. "
cin >> rlc;
} while (rlc != 'n' && rlc != 'l' && rlc != 'c') //happens if any above is desired
if (rlc == 'r') //if recruit a new student
{
if (enrollYN == 1) //error if statement if already enrolled
{
cout << "There is already a student enrolled. Please type 'l' if they have left.";
}
cout << "Please enter the new student: ";
cin << student.course::nStudent();
elseif (rlc == 'l') //to leave a student
{
cout << student.course::lStudent();
elseif (rlc == 'c') //announce test completion
{
do
{
if (enrollYN == 0);
{
"There is no student enrolled. Please type 'r' to recruit a student";
}
cout << ". Please enter their mark from their latest test: ";
cout << studScore.student::getNMark();
cout << "Their total score is now ";
cout << studScore.student::getTMark();
cout << ". Do you want to enter another mark? (Type 'Y' or 'N')";
cin >> again;
} while (again != 'N' && again != 'n'); //when answer is 'n' the program ends
}
}
}
if (enrollYN == 1);
{
course::~course()
}
cin.get();
}
}
}}
The program now runs (compiles) but the only two things problem wise I have is:
The course constructor runs twice at the beginning but not like the student constructor which correctly runs once,
When the if statements for entering l and c (r works fine) at the end are 'completed', the program activates the course destructor twice as well. How do I change when the destructor activates? Or is the loop wrong?
Also, the student destructor doesn't seem to happen.
#include <iostream> // declaring header files so I can use the code
#include <string>
usingnamespace std;
class student //declaring class student
{
public: //alowed to use these functions in main program
int enNo;
float nMark;
string surname;
student(); //constructor
~student(); //destructor
string getSurname();
int getEnNo();
float tMark;
float getNMark();
float getTMark();
};
class course //declaring class course. Similar contents to class student
{
public:
string student;
int enNo;
float tMark;
string cName;
int *CoursePtr;
bool enrollYN;
course();
~course();
string nStudent();
string lStudent();
string getCourse();
bool getEnrollYN();
string getStudent();
float getNMark();
float getTMark();
};
course::course() //course constructor, in which course name is entered.
{
cout << "A new course has been created. Please enter the title of the course: ";
cin >> cName;
}
course::~course() //destructor when the program ends.
{
cout << "The course ";
cout << cName;
cout << " has been deleted.";
if (enrollYN == 1) //if a student is enrolled
{
cout << " The student ";
cout << student;
cout << " with the enrolment number ";
cout << enNo;
cout << " is still enrolled with a total mark of ";
cout << tMark;
cout << ".";
}
}
string course::nStudent() //function for if student is enrolled
{
enrollYN = 1; //return student surname
}
string course::lStudent() //student leaving function
{
cout << "The student ";
cout << student;
cout << "is leaving and their total mark was: ";
cout << tMark;
getchar();
enrollYN = 0; //now left there is no student enrolled
}
string course::getCourse() //get course name function
{
return(cName);
}
bool course::getEnrollYN() //function for if student is enrolled or not
{
if (enrollYN = 0) //if not
{
return(0); //0 means they are not enrolled.
}
elseif (enrollYN = 1)
{ //else they are
return (1); //1 means they are enrolled
}
}
student::student()//student constructor
{
cout << "A new student is enrolling. Please enter the new student's surname: ";
getline(cin, surname);
cout << "Please enter their enrolment number: ";
cin >> enNo;
tMark = 0; //starts with no mark
}
student::~student() //destructor when program ends
{
cout << "The student ";
cout << surname;
cout << " is leaving and their total mark was ";
cout << student::getTMark();
getchar();
}
string student::getSurname() //return surname function
{
return(surname);
}
int student::getEnNo() //return their enrol number function
{
return (enNo);
}
float student::getTMark() //total mark function
{
return (tMark);
}
float student::getNMark() //new mark function
{
cin >> nMark;
tMark += nMark; //short way of putting tMark = tMark + nMark;
return (tMark);
}
int main(void) //main program
{
student studScore; //total mark for student
course studCourse; //course name
course enNo;
string sur;
int enr;
sur = studScore.student::getSurname();
enr = studScore.student::getEnNo();
cout << studScore.student::getSurname() << " has enrolled and their enroll number is ";
cout << studScore.student::getEnNo();
cout << ". ";
cout << studCourse.course::getCourse() << " is the title of the course they are enrolling on.";
char again;
char rlc; //again for when entering another mark, rlc for when a student is recruited, left or completed test
do
{
cout << " Type 'r' to recruit a new student, 'l' if a student has left and 'c' if a student has completed a test. Press any other key to quit the program.";
cin >> rlc;
if (rlc == 'r') //if recruit a new student
{
if (studCourse.course::getEnrollYN() == 1)
{
cout << "There is already a student enrolled.";
}
else
{
cout << "Please enter the surname of the new student: ";
cin >> sur;
cout << ". Please enter their enrollment number: ";
cin >> enr;
}
}
elseif (rlc == 'l') //to leave a student
{
studCourse.course::getEnrollYN() == 0;
cout << "The student is no longer enrolled.";
}
elseif (rlc == 'c') //announce test completion
{
do
{
if (studScore.student::getEnNo() == 0)
{
"There is no student enrolled. Please type 'r' to recruit a student";
}
cout << ". Please enter their mark from their latest test: ";
cout << studScore.student::getNMark();
cout << "Their total score is now ";
cout << studScore.student::getTMark();
cout << ". Do you want to enter another mark? (Type 'Y' or 'N')";
cin >> again;
} while (again != 'N' && again != 'n'); //when answer is 'n' the program ends
}
}
while (rlc != 'n' && rlc != 'l' && rlc != 'c'); //happens if any above is desired
}
Thanks for the help everyone has offered so far :)
It seems as though there are two different instances of the course con/destructor. The second destructor for course does not have a value for what is supposed to be in cName.
edit: one more comment: I wouldn't do any coding (as such) in class constructors or destructing (with the exception of your trace statements I guess. Ctor's and dtor's are there so you can initialise and clear up the object correctly.
i'd move this:
cout << "A new student is enrolling. Please enter the new student's surname: ";
getline(cin, surname);
cout << "Please enter their enrolment number: ";
cin >> enNo;
into a method called getStudentDetails, or something like that, and i'd move all of the code currently in your destructor into another method, especially your call to getchar(). this is very bad in a destructor.
also, what are you trying to do in these kind of situations:
cout << studScore.student::getNMark();
calling convention is: object.method()
why do you have a scope resolution operator in there?
I would not class a project as "nearly solved" just because it compiles.
edit: one more thing (but not the last..):
1 2 3 4
string course::nStudent() //function for if student is enrolled
{
enrollYN = 1; //return student surname
}
This does not compile for me. You even say "return student surname" but you don't return anything from this.
I seriously recommend reading up on both functions and classes and objects.