Please help me debug this program! #include <iostream> #include <stdlib.h> using
ID: 3819004 • Letter: P
Question
Please help me debug this program!
#include <iostream>
#include <stdlib.h>
using namespace std;
class Student
{
private:
int id;
string firstName;
string lastName;
int phone;
string emailID;
float GPA;
int classRank, exit;
public:
Student(int sID, string fname, string lname, int number, string email, float gpa, int rank)
{
id = sID;
firstName = fname;
lastName = lname;
phone = number;
emailID = email;
GPA = gpa;
classRank = rank;
}
int getClassRank() {
return classRank;
}
void setClassRank(int classRank) {
this->classRank = classRank;
}
string getEmailId() {
return emailID;
}
void setEmailId( string emailId) {
emailID = emailId;
}
string getFirstName() {
return firstName;
}
void setFirstName( string firstName) {
this->firstName = firstName;
}
float getGpa() const {
return GPA;
}
void setGpa(float gpa) {
GPA = gpa;
}
int getId() const {
return id;
}
void setId(int id) {
this->id = id;
}
string getLastName() {
return lastName;
}
void setLastName( string lastName) {
this->lastName = lastName;
}
int getPhone() {
return phone;
}
void setPhone(int phone) {
this->phone = phone;
}
void printStudent()
{
cout<<"Student ID: "<<id<<endl;
cout<<"Student Name: "<<firstName<<" "<<lastName<<endl;
cout<<fixed<<"Student Phone Number: "<<phone<<endl;
cout<<"Student email ID: "<<emailID<<endl;
cout<<"Student GPA: "<<GPA<<endl;
cout<<"Student Rank: "<<classRank<<endl;
}
};
class Schedule
{
private:
int StudentID;
int classNumber;
int creditHours;
public:
Schedule(int id, int number, int hours)
{
StudentID = id;
classNumber = number;
creditHours = hours;
}
int getClassNumber() {
return classNumber;
}
void setClassNumber(int classNumber) {
this->classNumber = classNumber;
}
int getCreditHours() {
return creditHours;
}
void setCreditHours(int creditHours) {
this->creditHours = creditHours;
}
int getStudentId() {
return StudentID;
}
void setStudentId(int studentId) {
StudentID = studentId;
}
void printSchdule()
{
cout<<"Student ID: "<<StudentID<<endl;
cout<<"Class Number: "<<classNumber<<endl;
cout<<"Credit Hours:"<<creditHours<<endl<<endl;
}
};
Student ** students = new Student*[20];
Schedule ** schedules = new Schedule*[20];
int numberOfStudents = 0;
int numberOfSchedules = 0;
void addStudent()
{
int id, number, rank;
float gpa;
string fname, lname, email;
cout<<" Enter student ID: ";
cin>>id;
cout<<" Enter student first name: ";
cin>>fname;
cout<<" Enter student last name: ";
cin>>lname;
cout<<" Enter student phone number: ";
cin>>number;
cout<<" Enter student email ID: ";
cin>>email;
cout<<" Enter student GPA: ";
cin>>gpa;
cout<<" Enter student rank: ";
cin>>rank;
students[numberOfStudents] = new Student(id, fname, lname, number, email, gpa, rank);
numberOfStudents++;
}
void deleteStudent()
{
int studentID;
cout<<" Enter the student id to delete: ";
cin>>studentID;
for(int i=0; i<numberOfStudents; i++)
{
if(students[i]->getId() == studentID)
{
if(i+1 == numberOfStudents)
{
delete students[i];
numberOfStudents--;
}
else
{
for(int j=i; j<numberOfStudents; j++)
{
students[i] = students[i+1];
}
numberOfStudents--;
break;
}
}
}
}
void printStudentInfo(int id)
{
bool isStudentAvailable = false;
for(int i=0; i<numberOfStudents; i++)
{
if(students[i]->getId() == id)
{
students[i]->printStudent();
isStudentAvailable = true;
}
}
if(isStudentAvailable == false)
{
cout<<" No such student available";
}
}
void printScheduleforStudentID()
{
int studentID;
cout<<" Enter the student ID to get the schedule: ";
cin>>studentID;
bool isSchudulePrinted = false;
for(int i=0; i<numberOfSchedules; i++)
{
if(schedules[i]->getStudentId() == studentID)
{
schedules[i]->printSchdule();
printStudentInfo(studentID);
isSchudulePrinted = true;
}
}
if(!isSchudulePrinted)
{
cout<<" No Schedule found for the student";
}
void addStudentToSchedule()
{
int studentID, classNumber, creditHours;
cout<<" Enter the Student id to add to the Schedule: ";
cin>>studentID;
cout<<" Enter the class number: ";
cin>>classNumber;
cout<<" Enter the credit hours for the student: ";
cin>>creditHours;
schedules[numberOfSchedules] = new Schedule(studentID, classNumber, creditHours);
numberOfSchedules++;
}
int main()
{
students[0] = new Student(1001, "Erik", "Hnida", 98764321, "BigDaddyG@hmail.com", 2.6, 20);
numberOfStudents++;
char choice = 'Y';
while(choice == 'y' || choice == 'Y')
{
int option;
cout<<" Choose an option. 1. Add a student 2. Add a student to schedule 3. Display a schedule for student 4. Display a student 5. Delete a student 6. Exit";
cout<<" Enter your choice: ";
cin>>option;
switch(option)
{
case 1: addStudent();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 2: addStudentToSchedule();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 3: printScheduleforStudentID();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 4: int id;
cout<<" Enter student id: ";
cin>>id;
printStudentInfo(id);
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 5: deleteStudent();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 6: exit(0);
}
}
return 0;
}
}
247:
#include <iostream>
#include <stdlib.h>
using namespace std;
class Student
{
private:
int id;
string firstName;
string lastName;
int phone;
string emailID;
float GPA;
int classRank, exit;
public:
Student(int sID, string fname, string lname, int number, string email, float gpa, int rank)
{
id = sID;
firstName = fname;
lastName = lname;
phone = number;
emailID = email;
GPA = gpa;
classRank = rank;
}
int getClassRank() {
return classRank;
}
void setClassRank(int classRank) {
this->classRank = classRank;
}
string getEmailId() {
return emailID;
}
void setEmailId( string emailId) {
emailID = emailId;
}
string getFirstName() {
return firstName;
}
void setFirstName( string firstName) {
this->firstName = firstName;
}
float getGpa() const {
return GPA;
}
void setGpa(float gpa) {
GPA = gpa;
}
int getId() const {
return id;
}
void setId(int id) {
this->id = id;
}
string getLastName() {
return lastName;
}
void setLastName( string lastName) {
this->lastName = lastName;
}
int getPhone() {
return phone;
}
void setPhone(int phone) {
this->phone = phone;
}
void printStudent()
{
cout<<"Student ID: "<<id<<endl;
cout<<"Student Name: "<<firstName<<" "<<lastName<<endl;
cout<<fixed<<"Student Phone Number: "<<phone<<endl;
cout<<"Student email ID: "<<emailID<<endl;
cout<<"Student GPA: "<<GPA<<endl;
cout<<"Student Rank: "<<classRank<<endl;
}
};
class Schedule
{
private:
int StudentID;
int classNumber;
int creditHours;
public:
Schedule(int id, int number, int hours)
{
StudentID = id;
classNumber = number;
creditHours = hours;
}
int getClassNumber() {
return classNumber;
}
void setClassNumber(int classNumber) {
this->classNumber = classNumber;
}
int getCreditHours() {
return creditHours;
}
void setCreditHours(int creditHours) {
this->creditHours = creditHours;
}
int getStudentId() {
return StudentID;
}
void setStudentId(int studentId) {
StudentID = studentId;
}
void printSchdule()
{
cout<<"Student ID: "<<StudentID<<endl;
cout<<"Class Number: "<<classNumber<<endl;
cout<<"Credit Hours:"<<creditHours<<endl<<endl;
}
};
Student ** students = new Student*[20];
Schedule ** schedules = new Schedule*[20];
int numberOfStudents = 0;
int numberOfSchedules = 0;
void addStudent()
{
int id, number, rank;
float gpa;
string fname, lname, email;
cout<<" Enter student ID: ";
cin>>id;
cout<<" Enter student first name: ";
cin>>fname;
cout<<" Enter student last name: ";
cin>>lname;
cout<<" Enter student phone number: ";
cin>>number;
cout<<" Enter student email ID: ";
cin>>email;
cout<<" Enter student GPA: ";
cin>>gpa;
cout<<" Enter student rank: ";
cin>>rank;
students[numberOfStudents] = new Student(id, fname, lname, number, email, gpa, rank);
numberOfStudents++;
}
void deleteStudent()
{
int studentID;
cout<<" Enter the student id to delete: ";
cin>>studentID;
for(int i=0; i<numberOfStudents; i++)
{
if(students[i]->getId() == studentID)
{
if(i+1 == numberOfStudents)
{
delete students[i];
numberOfStudents--;
}
else
{
for(int j=i; j<numberOfStudents; j++)
{
students[i] = students[i+1];
}
numberOfStudents--;
break;
}
}
}
}
void printStudentInfo(int id)
{
bool isStudentAvailable = false;
for(int i=0; i<numberOfStudents; i++)
{
if(students[i]->getId() == id)
{
students[i]->printStudent();
isStudentAvailable = true;
}
}
if(isStudentAvailable == false)
{
cout<<" No such student available";
}
}
void printScheduleforStudentID()
{
int studentID;
cout<<" Enter the student ID to get the schedule: ";
cin>>studentID;
bool isSchudulePrinted = false;
for(int i=0; i<numberOfSchedules; i++)
{
if(schedules[i]->getStudentId() == studentID)
{
schedules[i]->printSchdule();
printStudentInfo(studentID);
isSchudulePrinted = true;
}
}
if(!isSchudulePrinted)
{
cout<<" No Schedule found for the student";
}
void addStudentToSchedule()
{
int studentID, classNumber, creditHours;
cout<<" Enter the Student id to add to the Schedule: ";
cin>>studentID;
cout<<" Enter the class number: ";
cin>>classNumber;
cout<<" Enter the credit hours for the student: ";
cin>>creditHours;
schedules[numberOfSchedules] = new Schedule(studentID, classNumber, creditHours);
numberOfSchedules++;
}
int main()
{
students[0] = new Student(1001, "Erik", "Hnida", 98764321, "BigDaddyG@hmail.com", 2.6, 20);
numberOfStudents++;
char choice = 'Y';
while(choice == 'y' || choice == 'Y')
{
int option;
cout<<" Choose an option. 1. Add a student 2. Add a student to schedule 3. Display a schedule for student 4. Display a student 5. Delete a student 6. Exit";
cout<<" Enter your choice: ";
cin>>option;
switch(option)
{
case 1: addStudent();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 2: addStudentToSchedule();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 3: printScheduleforStudentID();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 4: int id;
cout<<" Enter student id: ";
cin>>id;
printStudentInfo(id);
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 5: deleteStudent();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 6: exit(0);
}
}
return 0;
}
}
247, 260: A function definition is not allowed here before the "}" token
302: Expected "}" at end of input
Explanation / Answer
Hi
I have fixed the issue and it is working file now
#include <iostream>
#include <stdlib.h>
using namespace std;
class Student
{
private:
int id;
string firstName;
string lastName;
int phone;
string emailID;
float GPA;
int classRank, exit;
public:
Student(int sID, string fname, string lname, int number, string email, float gpa, int rank)
{
id = sID;
firstName = fname;
lastName = lname;
phone = number;
emailID = email;
GPA = gpa;
classRank = rank;
}
int getClassRank() {
return classRank;
}
void setClassRank(int classRank) {
this->classRank = classRank;
}
string getEmailId() {
return emailID;
}
void setEmailId( string emailId) {
emailID = emailId;
}
string getFirstName() {
return firstName;
}
void setFirstName( string firstName) {
this->firstName = firstName;
}
float getGpa() const {
return GPA;
}
void setGpa(float gpa) {
GPA = gpa;
}
int getId() const {
return id;
}
void setId(int id) {
this->id = id;
}
string getLastName() {
return lastName;
}
void setLastName( string lastName) {
this->lastName = lastName;
}
int getPhone() {
return phone;
}
void setPhone(int phone) {
this->phone = phone;
}
void printStudent()
{
cout<<"Student ID: "<<id<<endl;
cout<<"Student Name: "<<firstName<<" "<<lastName<<endl;
cout<<fixed<<"Student Phone Number: "<<phone<<endl;
cout<<"Student email ID: "<<emailID<<endl;
cout<<"Student GPA: "<<GPA<<endl;
cout<<"Student Rank: "<<classRank<<endl;
}
};
class Schedule
{
private:
int StudentID;
int classNumber;
int creditHours;
public:
Schedule(int id, int number, int hours)
{
StudentID = id;
classNumber = number;
creditHours = hours;
}
int getClassNumber() {
return classNumber;
}
void setClassNumber(int classNumber) {
this->classNumber = classNumber;
}
int getCreditHours() {
return creditHours;
}
void setCreditHours(int creditHours) {
this->creditHours = creditHours;
}
int getStudentId() {
return StudentID;
}
void setStudentId(int studentId) {
StudentID = studentId;
}
void printSchdule()
{
cout<<"Student ID: "<<StudentID<<endl;
cout<<"Class Number: "<<classNumber<<endl;
cout<<"Credit Hours:"<<creditHours<<endl<<endl;
}
};
Student ** students = new Student*[20];
Schedule ** schedules = new Schedule*[20];
int numberOfStudents = 0;
int numberOfSchedules = 0;
void addStudent()
{
int id, number, rank;
float gpa;
string fname, lname, email;
cout<<" Enter student ID: ";
cin>>id;
cout<<" Enter student first name: ";
cin>>fname;
cout<<" Enter student last name: ";
cin>>lname;
cout<<" Enter student phone number: ";
cin>>number;
cout<<" Enter student email ID: ";
cin>>email;
cout<<" Enter student GPA: ";
cin>>gpa;
cout<<" Enter student rank: ";
cin>>rank;
students[numberOfStudents] = new Student(id, fname, lname, number, email, gpa, rank);
numberOfStudents++;
}
void deleteStudent()
{
int studentID;
cout<<" Enter the student id to delete: ";
cin>>studentID;
for(int i=0; i<numberOfStudents; i++)
{
if(students[i]->getId() == studentID)
{
if(i+1 == numberOfStudents)
{
delete students[i];
numberOfStudents--;
}
else
{
for(int j=i; j<numberOfStudents; j++)
{
students[i] = students[i+1];
}
numberOfStudents--;
break;
}
}
}
}
void printStudentInfo(int id)
{
bool isStudentAvailable = false;
for(int i=0; i<numberOfStudents; i++)
{
if(students[i]->getId() == id)
{
students[i]->printStudent();
isStudentAvailable = true;
}
}
if(isStudentAvailable == false)
{
cout<<" No such student available";
}
}
void printScheduleforStudentID()
{
int studentID;
cout<<" Enter the student ID to get the schedule: ";
cin>>studentID;
bool isSchudulePrinted = false;
for(int i=0; i<numberOfSchedules; i++)
{
if(schedules[i]->getStudentId() == studentID)
{
schedules[i]->printSchdule();
printStudentInfo(studentID);
isSchudulePrinted = true;
}
}
if(!isSchudulePrinted)
{
cout<<" No Schedule found for the student";
}
}
void addStudentToSchedule()
{
int studentID, classNumber, creditHours;
cout<<" Enter the Student id to add to the Schedule: ";
cin>>studentID;
cout<<" Enter the class number: ";
cin>>classNumber;
cout<<" Enter the credit hours for the student: ";
cin>>creditHours;
schedules[numberOfSchedules] = new Schedule(studentID, classNumber, creditHours);
numberOfSchedules++;
}
int main()
{
students[0] = new Student(1001, "Erik", "Hnida", 98764321, "BigDaddyG@hmail.com", 2.6, 20);
numberOfStudents++;
char choice = 'Y';
while(choice == 'y' || choice == 'Y')
{
int option;
cout<<" Choose an option. 1. Add a student 2. Add a student to schedule 3. Display a schedule for student 4. Display a student 5. Delete a student 6. Exit";
cout<<" Enter your choice: ";
cin>>option;
switch(option)
{
case 1: addStudent();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 2: addStudentToSchedule();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 3: printScheduleforStudentID();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 4: int id;
cout<<" Enter student id: ";
cin>>id;
printStudentInfo(id);
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 5: deleteStudent();
cout<<" Do you want to continue: Y or N? ";
cin>>choice;
break;
case 6: exit(0);
}
}
return 0;
}
Output:
sh-4.2$ g++ -std=c++11 -o main *.cpp
sh-4.2$ main
Choose an option.
1. Add a student
2. Add a student to schedule
3. Display a schedule for student
4. Display a student
5. Delete a student
6. Exit
Enter your choice: 1
Enter student ID: 111
Enter student first name: sss
Enter student last name: dd
Enter student phone number: 222
Enter student email ID: asdas
Enter student GPA: 23
Enter student rank: 2
Do you want to continue: Y or N? n
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.