#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include <sstream>
#include <windows.h>
using namespace std;
class Subject
{
protected:
string sub_code;
string sub_fac;
public:
Subject(string sub_code="", string sub_fac="")
{
this->sub_code = sub_code;
this->sub_fac = sub_fac;
}
string getSubCode() {return sub_code;}
string getSubFac() {return sub_fac;}
};
class Student
{
protected:
string stud_name;
string stud_fac;
public:
Student(string stud_name="", string stud_fac="")
{
this->stud_name = stud_name;
this->stud_fac = stud_fac;
}
string getStudName() {return stud_name;}
string getStudFac() {return stud_fac;}
};
vector <Subject> subject;
vector <Student> student;
string temp1, temp2;
bool stud_exist=0, sub_exist=0, duplicate=0;
int position;
class Registration : public Subject, public Student
{
public:
Registration(string stud_name, string stud_fac, string sub_code, string sub_fac) : Student(stud_name,stud_fac), Subject(sub_code,sub_fac) {}
};
vector <Registration> registration;
void Import(char filename[]);
void Combine(string student_name, string subject_name);
void ListAll(string input);
void ListOne(string input);
void Add(string input);
void Delete(string input);
void RegistrationFunction(string input);
void Check();
string Upper(string input);
void End()
{
cout << endl << endl;
system("pause");
}
void ShowMessage(int msgcode=0)
{
if (msgcode == 0)
cout << "\nNULL value is found";
else if (msgcode == 1)
cout << endl << "Student " << temp1 << " cannot be found";
else if (msgcode == 2)
cout << endl << "Subject " << temp2 << " cannot be found";
else if (msgcode == 3)
cout << endl << "Subject " << temp1 << " cannot be found";
else if (msgcode == 4)
cout << endl << temp1 << " has been added previously";
else if (msgcode == 5)
cout << endl << temp1 << " has registered " << temp2 << " previously";
else if (msgcode == 6)
cout << endl << temp1 << " has not register the subject " << temp2 << " previously";
else if (msgcode == 96)
cout << endl << temp1 << " has dropped " << temp2 << " successfully";
else if (msgcode == 97)
cout << endl << temp1 << " has registered " << temp2 << " successfully";
else if (msgcode == 98)
cout << endl << temp1 << " has been removed permanently";
else if (msgcode == 99)
cout << endl << temp1 << " has been registered under the faculty " << temp2 << " successfully";
if (msgcode<50)
system("color 04");
else
system("color 02");
}
int main()
{
int x;
Import("subjects.txt");
Import("students.txt");
Import("registration.txt");
while(1)
{
temp1 = " "; // Clear data
temp2 = " "; // Clear data
system("color 07");
system("cls");
cout << "Subject Registration System\n\n"
"1.\tCreate a student\n"
"2.\tCreate a subject\n"
"3.\tDelete a student\n"
"4.\tDelete a subject\n"
"5.\tRegister a student to/from a subject\n"
"6.\tDrop a student to/from a subject\n"
"7.\tList all students\n"
"8.\tList all subjects\n"
"9.\tList subjects of a specified student\n"
"10.\tList students of a specified subject\n\n"
"Choose one: ";
cin >> x;
cin.clear();
cin.ignore(255,'\n');
switch(x)
{
case 1: {Add("Student"); break;}
case 2: {Add("Subject"); break;}
case 3: {Delete("Student"); break;}
case 4: {Delete("Subject"); break;}
case 5: {RegistrationFunction("Add"); break;}
case 6: {RegistrationFunction("Drop"); break;}
case 7: {ListAll("Student"); break;}
case 8: {ListAll("Subject"); break;}
case 9: {ListOne("Student"); break;}
case 10: {ListOne("Subject"); break;}
}
}
}
void Import(char filename[])
{
vector<string>temp;
ifstream in; string data;
in.open(filename);
while (getline(in,data,'\n')) // Store the data line by line
temp.push_back(data);
for (int i=0; i<temp.size(); i++)
{
int count=0; string str = temp[i];
stringstream stream(str);
while(getline(stream, data, ',')) // Seperate data by comma
{
if(count==0)
temp1 = data;
else if (count==1)
temp2 = data;
count++;
}
if(filename=="subjects.txt")
{
Subject x(temp1, temp2);
subject.push_back(x);
Combine("",temp1);
}
else if(filename=="students.txt")
{
Student x(temp1, temp2);
student.push_back(x);
Combine(temp1,"");
}
else if(filename=="registration.txt")
Combine(temp1,temp2);
}
temp.clear();
in.clear();
in.close();
}
void Combine(string x, string y)
{
int flag=0; string a,b,c,d;
for(int i=0; i<student.size();i++) // Fetch student's faculty
if(Upper(student[i].getStudName())==Upper(x))
{
a = student[i].getStudName();
b = student[i].getStudFac();
}
for(int i=0; i<subject.size();i++) // Fetch subject's faculty
if(Upper(subject[i].getSubCode())==Upper(y))
{
c = subject[i].getSubCode();
d = subject[i].getSubFac();
}
Registration temp(a,b,c,d); // Confirm registration
registration.push_back(temp);
}
void ListOne(string x)
{
system("cls");
int count = 1;
if (x=="Student")
cout << "You are about to list all subjects from a Student\n\nStudent Name\t: ";
else if (x=="Subject")
cout << "You are about to list all students from a Subject\n\nSubject Code\t: ";
getline(cin,temp1);
Check();
if(temp1=="")
ShowMessage(0); // Display Message
else if(stud_exist == 0 && x=="Student")
ShowMessage(1); // Display Message
else if(sub_exist == 0 && x=="Subject")
ShowMessage(3); // Display Message
else
{
for (int i=0; i<registration.size(); i++)
if(Upper(registration[i].getStudName()) == Upper(temp1) && registration[i].getSubCode()!= "" && x=="Student")
{
if (count==1)
cout << "\nNo.\tSubjects\tFaculty\n";
cout << count << "\t" << registration[i].getSubCode() << "\t\t" << registration[i].getSubFac() << "\n";
count++;
}
else if(Upper(registration[i].getSubCode()) == Upper(temp1) && registration[i].getStudName()!= "" && x=="Subject")
{
if (count==1)
cout << "\nNo.\tStudents\tFaculty\n";
cout << count << "\t" << registration[i].getStudName() << "\t\t" << registration[i].getStudFac() << "\n";
count++;
}
}
End();
}
void ListAll(string x)
{
system("cls");
vector<Registration>temp = registration;
if(x=="Subject")
cout << "All subjects\n\nNo.\tSubjects\tFaculty\t\tStudents\n";
else if(x=="Student")
cout << "All students\n\nNo.\tStudents\tFaculty\t\tSubjects\n";
for (int i=0; i<temp.size(); i++)
{
bool comma=0;
if(temp[i].getSubCode()=="" && x=="Subject") // Make sure subject is not empty
{temp.erase(temp.begin()+i); i--;}
else if(temp[i].getStudName()=="" && x=="Student") // Make sure subject is not empty
{temp.erase(temp.begin()+i); i--;}
else
{
if(x=="Subject") // Publish all subjects
{
cout << i+1 << "\t" << temp[i].getSubCode() << "\t\t" << temp[i].getSubFac() << "\t\t";
for (int j=temp.size()-1; j>=0; j--)
if(Upper(temp[j].getSubCode())==Upper(temp[i].getSubCode()) && temp[j].getStudName() != "")
{
if(comma)
cout << ", ";
cout << temp[j].getStudName();
if(i!=j) {temp.erase(temp.begin()+j);}
comma = 1;
}
cout << endl;
}
else if(x=="Student") // Publish all students
{
cout << i+1 << "\t" << temp[i].getStudName() << "\t\t" << temp[i].getStudFac() << "\t\t";
for (int j=temp.size()-1; j>=0; j--)
if(Upper(temp[j].getStudName())==Upper(temp[i].getStudName()) && temp[j].getSubCode() != "")
{
if(comma)
cout << ", ";
cout << temp[j].getSubCode();
if(i!=j) {temp.erase(temp.begin()+j);}
comma = 1;
}
cout << endl;
}
}
}
End();
}
void Add(string x)
{
system("cls");
if(x=="Student")
cout << "You are about to add a new student\n\nStudent Name\t: ";
else if(x=="Subject")
cout << "You are about to add a new subject\n\nSubject Code\t: ";
getline(cin,temp1);
cout << "Faculty Name\t: ";
getline(cin,temp2);
Check();
if(temp1=="" || temp2=="")
ShowMessage(0); // Display Message
else if(stud_exist || sub_exist)
ShowMessage(4); // Display Message
else if(x=="Student")
{
Student temp(temp1, temp2);
student.push_back(temp);
Combine(temp1,"");
ShowMessage(99); // Display Message
}
else if(x=="Subject")
{
Subject temp(temp1, temp2);
subject.push_back(temp);
Combine("",temp1);
ShowMessage(99); // Display Message
}
End();
}
void Delete(string x)
{
system("cls");
bool flag=0;
cout << "You are about to delete a " << x << "\n\n" << x << " Name\t: ";
getline(cin,temp1);
for (int i=0; i<registration.size(); i++)
if ((Upper(registration[i].getStudName())==Upper(temp1) && x=="Student") || (Upper(registration[i].getSubCode())==Upper(temp1) && x=="Subject"))
{
registration.erase(registration.begin()+i);
flag = 1;
i--;
}
if(temp1=="")
ShowMessage(0); // Display Message
else if (flag == 1)
ShowMessage(98); // Display Message
else
ShowMessage(1); // Display Message
End();
}
void RegistrationFunction(string x)
{
system("cls");
if (x=="Add")
cout << "You are about to register a subject to a student\n\nStudent Name\t: ";
else if (x=="Drop")
cout << "You are about to drop a subject from a student\n\nStudent Name\t: ";
getline(cin,temp1);
cout << "Subject Code\t: ";
getline(cin,temp2);
Check();
if(temp1=="" || temp2=="")
ShowMessage(0); // Display Message
else if (duplicate==1 && x=="Add")
ShowMessage(5); // Display Message
else if(stud_exist==1 && sub_exist==1 && x=="Add")
{
Combine(temp1,temp2); // Perform registration
ShowMessage(97); // Display Message
}
else if (duplicate==0)
ShowMessage(6); // Display Message
else if(duplicate==1 && x=="Drop")
{
registration.erase(registration.begin()+position); // Perform deletion
ShowMessage(96); // Display Message
}
if (stud_exist==0)
ShowMessage(1); // Display Message
if (sub_exist==0)
ShowMessage(2); // Display Message
End();
}
void Check()
{
stud_exist = 0; sub_exist = 0; duplicate = 0;
for (int i=0; i<registration.size(); i++)
{
if (Upper(registration[i].getStudName())==Upper(temp1)) // Check if student exist
stud_exist = 1;
else if(Upper(registration[i].getSubCode())==Upper(temp1) || Upper(registration[i].getSubCode())==Upper(temp2)) // Check if subject exist
sub_exist = 1;
if (Upper(registration[i].getStudName())==Upper(temp1) && Upper(registration[i].getSubCode())==Upper(temp2)) // Check for duplication
{duplicate = 1; position = i;}
}
}
string Upper(string x)
{
for (int i=0; i<x.size(); i++)
x[i] = toupper(x[i]);
return x;
}
im having trouble with too few argument at the void combine... and also at the part void add which precisely at combine(temp,""). Please help me....this is the final i need to wrap my project