i`ve done a few codes and i compile it so that i can review it
but it got stuck when i want to assign a room.
i dont know why..
can someone please help me to fix this please...
#include <iostream>
#include <vector>
#include <cstdlib>
#include <set>
#include <algorithm>
#include <string>
using namespace std;
class student
{
protected:
string name, dummy,status;
char gender;
int ID;
public:
student() : name("UNKNOWN"), ID(0), gender('-'), status("unassigned"){}
student (string Name, int ID, char Gender)
{
this->name = Name;
this->ID = ID;
this->gender = Gender;
}
student(const student& s): name(s.name), ID(s.ID), gender(s.gender){}
void setstudent();
void setstatus(string status) {this->status = status;}
void print(int i) const;
int getid(){return ID;}
};
void student::setstudent()
{
cout << "Enter student's ID: ";
cin >> ID;
cout << "Enter student's gender (M/F): ";
cin >> gender;
gender = toupper(gender);
getline(cin, dummy);
cout << "Enter student's name: ";
getline(cin, name);
}
void student::print(int i) const
{
cout << i+1 << ". ID: " << ID << " || NAME: " << name <<
" || SEX: " << gender << " || ROOM: " << status << endl;
}
class room
{
protected:
vector<student*> students;
vector<student*>::iterator it;
public:
void addstudent(student* s);
void delstudent (int target);
void printALL();
void availroom(); //print available room
int studentsize(){return students.size();}
void assignMA(char MA[8][2]);
void assignMB(char MB[12][3]);
void assignFA(char FA[6][2]);
void assignFB(char FB[14][3]);
void unAssignMA(char MA[8][2]);
void unAssignMB(char MB[12][3]);
void unAssignFA(char FA[6][2]);
void unAssignFB(char FB[14][3]);
void availPlaceMA (char MA[8][2]);
void availPlaceMB (char MB[12][3]);
void availPlaceFA (char FA[6][2]);
void availPlaceFB (char FB[14][3]);
void addRoom(int target);
void delRoom(int target);
};
void room::addstudent(student* s)
{
students.push_back(s);
}
void room::delstudent(int target)
{
students.erase(students.begin()+target);
}
void room::printALL()
{
for(int i = 0; i < students.size(); i++)
students[i]->print(i);
}
void room:: assignMA(char MA[8][2])
{
int i ; // row
int j; // column
int c =0;
char more='Y';
do
{
cout << endl;
cout<<" Please enter room desired 1-8 :";
cin>>i;
cout<<endl;
while(i>8||i<1)
{
cout << i << endl;
cout <<" invalid room" << endl;
cout << " Enter Room desired 1-8 ";
cin >> i;
}
i--;
for(j=0;j<2;j++){
if( MA[i][j]=='X'){
c++;
if (c = 2){cout << "Room is full..select other Room";}
}
}
}while (MA[i][j]=='X');
MA[i][j]='X';
i++;
cout << " Success assigning room ."<< endl;
cout << "room no = MA" << i<<endl;
return;
}
void room:: assignMB(char MB[12][3])
{
int i ; // row
int j; // column
int c =0;
char more='Y';
do
{
cout << endl;
cout<<" Please enter room desired 1-12 :";
cin>>i;
cout<<endl;
while(i>12||i<1)
{
cout << i << endl;
cout <<" invalid room" << endl;
cout << " Enter Room desired 1-12 ";
cin >> i;
i--;
}
for(j=0;j<2;j++){
if( MB[i][j]=='X'){
c++;
if (c = 3){cout << "Room is full..select other Room";}
}
}
}while (MB[i][j]=='X');
MB[i][j]='X';
cout << " Success assigning room ."<<endl;
i++;
cout << "room no = MB" << i<<endl;
return;
}
void room:: assignFA(char FA[6][2])
{
int i ; // row
int j; // column
int c =0;
char more='Y';
do
{
cout << endl;
cout<<" Please enter room desired 1-12 :";
cin>>i;
cout<<endl;
while(i>6||i<1)
{
cout << i << endl;
cout <<" invalid room" << endl;
cout << " Enter Room desired 1-6 ";
cin >> i;
i--;
}
for(j=0;j<2;j++){
if( FA[i][j]=='X'){
c++;
if (c = 2){cout << "Room is full..select other Room";}
}
}
}while (FA[i][j]=='X');
FA[i][j]='X';
cout << " Success assigning room ."<<endl;
i++;
cout << "room no = FA" << i<<endl;
return;
};
void room:: assignFB(char FB[14][3])
{
int i ; // row
int j; // column
int c =0;
char more='Y';
do
{
cout << endl;
cout<<" Please enter room desired 1-14 :";
cin>>i;
cout<<endl;
while(i>14||i<1)
{
cout << i << endl;
cout <<" invalid room" << endl;
cout << " Enter Room desired 1-14 ";
cin >> i;
i--;
}
for(j=0;j<3;j++){
if( FB[i][j]=='X'){
c++;
if (c = 3){cout << "Room is full..select other Room";}
}
}
}while (FB[i][j]=='X');
FB[i][j]='X';
cout << " Success assigning room"<<endl;
i++;
cout << "room no = FB" << i<<endl;
return;
};
void room::addRoom(int target)
{
string roomno;
cout << "reinput the room no for student record= " ;
cin>>roomno;
students[target]->setstatus(roomno);
//students.erase(students.begin()+target);
}
void room::availPlaceMA (char MA[8][2])
{ //print available room
int category;
int row_counter, column_counter = 0;
int avail_counter = 0;
int counter = 0;
cout <<"This is list of available room" << endl;
for(row_counter = 0; row_counter <= 7 ; row_counter++)
{
counter = 0;
for( column_counter = 0; column_counter <=1; column_counter++ )
{
if (MA[row_counter][column_counter] == '*')
{
counter++;
}
}
if (counter >= 1)
{
cout << "MA" << row_counter+1<< " got " <<counter << "available place"<<endl;
}
cout << endl;
}
}
void room::availPlaceMB (char MB[12][3])
{ //print available room
int category;
int row_counter, column_counter = 0;
int avail_counter = 0;
int counter = 0;
cout <<"This is list of available room" << endl;
for(row_counter = 0; row_counter <= 11 ; row_counter++)
{
counter = 0;
for( column_counter = 0; column_counter <=2; column_counter++ )
{
if (MB[row_counter][column_counter] == '*')
{
counter++;
}
if (counter >= 1)
{
cout << "MB" << row_counter+1<< " got " <<counter << "available place"<<endl;
}
}
cout << endl;
}
}
void room::availPlaceFA (char FA[6][2])
{ //print available room
int category;
int row_counter, column_counter = 0;
int avail_counter = 0;
int counter = 0;
cout <<"This is list of available room" << endl;
for(row_counter = 0; row_counter <= 5 ; row_counter++)
{
counter =0;
for( column_counter = 0; column_counter <=1; column_counter++ )
{
if (FA[row_counter][column_counter] == '*')
{
counter++;
}
if (counter >= 1)
{
cout << "FA" << row_counter+1<< " got " <<counter << "available place"<<endl;
}
}
cout << endl;
}
}
void room::availPlaceFB (char FB[14][3])
{ //print available room
int category;
int row_counter, column_counter = 0;
int avail_counter = 0;
int counter = 0;
cout <<"This is list of available room" << endl;
for(row_counter = 0; row_counter <= 13 ; row_counter++)
{
counter =0;
for( column_counter = 0; column_counter <=2; column_counter++ )
{
if (FB[row_counter][column_counter] == '*')
{
counter++;
}
if (counter >= 1)
{
cout << "FB" << row_counter+1<< " got "<< counter << "available place"<<endl;
}
}
cout << endl;
}
}
void room:: unAssignMA(char MA[8][2])
{
int i ; // row
int j; // column
int c =0;
char more='Y';
do
{
cout << endl;
cout<<" Please enter room desired 1-8 :";
cin>>i;
cout<<endl;
while(i>8||i<1)
{
cout << i << endl;
cout <<" invalid room" << endl;
cout << " Enter Room desired 1-8 ";
cin >> i;
}
i--;
for(j=0;j<2;j++){
if( MA[i][j]=='*'){
c++;
if (c = 2){cout << "Room is already empty..select other Room";}
}
}
}while (MA[i][j]=='*');
MA[i][j]='*';
cout << " Success unassigning room ."<<endl;
i++;
cout << "room no = MA" << i<<endl;
return;
}
void room:: unAssignMB(char MB[12][3])
{
int i ; // row
int j; // column
int c =0;
char more='Y';
do
{
cout << endl;
cout<<" Please enter room desired 1-12 :";
cin>>i;
cout<<endl;
while(i>12||i<1)
{
cout << i << endl;
cout <<" invalid room" << endl;
cout << " Enter Room desired 1-12 ";
cin >> i;
i--;
}
for(j=0;j<2;j++){
if( MB[i][j]=='*'){
c++;
if (c = 3){cout << "Room is already empty..select other Room";}
}
}
}while (MB[i][j]=='*');
MB[i][j]='*';
cout << " Success unassigning room ."<<endl;
i++;
cout << "room no = MB" << i<<endl;
return;
}
void room:: unAssignFA(char FA[6][2])
{
int i ; // row
int j; // column
int c =0;
char more='Y';
do
{
cout << endl;
cout<<" Please enter room desired 1-12 :";
cin>>i;
cout<<endl;
while(i>6||i<1)
{
cout << i << endl;
cout <<" invalid room" << endl;
cout << " Enter Room desired 1-6 ";
cin >> i;
i--;
}
for(j=0;j<2;j++){
if( FA[i][j]=='*'){
c++;
if (c = 2){cout << "Room is already empty..select other Room";}
}
}
}while (FA[i][j]=='*');
FA[i][j]='*';
cout << " Success unassigning room ."<<endl;
i++;
cout << "room no = FA" << i<<endl;
return;
};
void room:: unAssignFB(char FB[14][3])
{
int i ; // row
int j; // column
int c =0;
char more='Y';
do
{
cout << endl;
cout<<" Please enter room desired 1-14 :";
cin>>i;
cout<<endl;
while(i>14||i<1)
{
cout << i << endl;
cout <<" invalid room" << endl;
cout << " Enter Room desired 1-14 ";
cin >> i;
i--;
}
for(j=0;j<3;j++){
if( FB[i][j]=='*'){
c++;
if (c = 3){cout << "Room is already..select other Room";}
}
}
}while (FB[i][j]=='*');
FB[i][j]='*';
cout << " Success unassigning room ."<<endl;
i++;
cout << "room no = FB" << i<<endl;
return;
};
void room::delRoom(int target)
{
students[target]->setstatus("unassigned");
//students.erase(students.begin()+target);
}
void mainmenu()
{
cout << "*===========================================*" << endl <<
"|[1] add students |" << endl <<
"|[2] delete student |" << endl <<
"|[3] assign rooms |" << endl <<
"|[4] empty rooms |" << endl <<
"|[5] List students |" << endl <<
"|[6] Exit |" << endl <<
"*===========================================+" << endl << endl << endl;
}
void roomMenu1()
{
cout << "*===========================================*" << endl <<
"|[1] ASSIGN ROOM TO A STUDENT |" << endl <<
"|[2] UNASSIGN A ROOM FROM A STUDENT |" << endl <<
"|[3] ASSIGN ROOM ACCORDING TO GENDER |" << endl <<
"|[4] back |" << endl <<
"*===========================================+" << endl << endl << endl;
}
room a;
room r1;
student* s1 = NULL;
int target;
int num;
bool done = false;
char choice,choice2, option;
char gender2;
char m,f,F,M;
char MAs[8][2];
char MBs[12][3];
char FAs[6][2];
char FBs[14][3];
int main()
{
for( int row = 0; row < 8; ++row ){
for( int column = 0; column < 2; ++column )
{
MAs[row][column] = '*';
}
}
for( int row = 0; row < 12; ++row ){
for( int column = 0; column < 3; ++column )
{
MBs[row][column] = '*';
}
}
for( int row = 0; row < 6; ++row ){
for( int column = 0; column < 2; ++column )
{
FAs[row][column] = '*';
}
}
for( int row = 0; row < 14; ++row ){
for( int column = 0; column < 3; ++column )
{
FBs[row][column] = '*';
}
}
do
{
mainmenu();
cout << "Select option (1-6): ";
cin >> option;
option = toupper(option);
switch (option)
{
case '1' : cout << "Add student? (Y/N):";
cin >> choice;
system ( "cls" );
while (toupper(choice) == 'Y')
{
cout << "existed student " <<endl;
r1.printALL();
s1 = new student;
s1->setstudent();
r1.addstudent(s1);
cout << "add more students?(Y/N): ";
cin >> choice;
}
r1.printALL();
system("pause");
system("cls");
break;
case '2' : cout << "Delete student? (Y/N):";
cin >> choice;
while (toupper(choice) == 'Y')
{
system("cls");
r1.printALL();
cout << "select a student(1-" << r1.studentsize() << "): ";
cin >> target;
cout << endl;
target=target-1;
r1.delstudent(target);
cout << "Delete more students?(Y/N): ";
cin >> choice;
};
system("pause");
system("cls");
break;
case '3' : cout << "Assign to room? (Y/N):";
cin >> choice;
num = 2;
while (toupper(choice) == 'Y' )
{
system("cls");
r1.printALL();
cout << "select a student(1-" << r1.studentsize() << "): ";
cin >> target;
target--;
cout << "input f for female/m for male :";
cin >> gender2;
if (gender2 =='m'||gender2=='M')
{
cout << "choose type A or B? :" ;
cin >> choice2;
cout <<endl;
if(choice2=='A'||choice2=='a')
{
a.availPlaceMA (MAs);
a.assignMA(MAs);
a.addRoom(target);
}
else if(choice2=='B'||choice2=='b')
{
a.availPlaceMB (MBs);
a.assignMB(MBs);
a.addRoom(target);
}
}
else if (gender2 =='f'||gender2=='F')
{
cout << "choose type A or B? :" ;
cin >> choice2;
cout << endl;
if(choice2=='A'||choice2=='a')
{
a.availPlaceFA (FAs);
a.assignFA(FAs);
a.addRoom(target);
}
else if(choice2=='B'||choice2=='b')
{
a.availPlaceFB(FBs);
a.assignFB(FBs);
a.addRoom(target);
}
}
}
cout << "Assign more?(Y/N): ";
cin >> choice;
system("pause");
system("cls");
break;
case '4' : cout << "Unassign room? (Y/N):";
cin >> choice;
while (toupper(choice) == 'Y' )
{
system("cls");
r1.printALL();
cout << "select a student(1-" << r1.studentsize() << "): ";
cin >> target;
target--;
cout << "input f for female/m for male :";
cin >> gender2;
if (gender2 =='m'||gender2=='M')
{
cout << "choose type A or B? :" ;
cin >> choice2;
cout<<endl;
if(choice2=='A'||choice2=='a')
{
a.unAssignMA(MAs);
a.delRoom(target);
}
else if(choice2=='B'||choice2=='b')
{
a.unAssignMB(MBs);
a.delRoom(target);
}
}
else if (gender2 =='f'||gender2=='F')
{
cout << "choose type A or B? :" ;
cin >> choice2;
cout<<endl;
if(choice2=='A'||choice2=='a')
{
a.unAssignFA(FAs);
a.delRoom(target);
}
else if(choice2=='B'||choice2=='b')
{
a.unAssignFB(FBs);
a.delRoom(target);
}
}
}
cout << "Assign more?(Y/N): ";
cin >> choice;
a.printALL();
system("pause");
system("cls");
break;
case '5' : r1.printALL();
system("pause");
system("cls");
break;
case '6' : done = true;
break;
}
}while(!done);
return 0;
}
really2 hope u guys can help me :)