// Matt Kaliszewski
// Dormitory Assignment Program
// Kaliszewski M Dormitoty Program.cpp
// Libraries
#include <iostream>
using namespace std;
#include <conio.h>
#include <string.h>
#include "dorm.h"
// Function Prototypes
void display_room(dormitory d[7], int c_d, int c_r);
void set_dorm_n_room(int *c_d, int *c_r);
void search_dorms(dormitory d[7], char name[40], char type);
int
main(void)
{
dormitory dorms[7];
int choice, curr_dorm, curr_room, s, remove, search;
char first[40], last[40];
do{
// Display the menu
cout << "############## DORMITORY MANAGER ##############" << endl << endl
<< "Please select an option:" << endl << endl
<< "1) Assign a student to a room" << endl
<< "2) Remove a student from a room" << endl
<< "3) Display the occupants of a room" << endl
<< "4) Find a specific student" << endl
<< "5) Quit" << endl << endl
<< "Your choice: ";
cin >> choice;
cout << endl;
switch(choice)
{
case 1: // Assign a student to a room
// Set the dorm and room
set_dorm_n_room(&curr_dorm, &curr_room);
// Make sure there is an open bed
s=0;
while(dorms[curr_dorm-1].rooms[curr_room-1].open[s]==0 && s<4)
s++;
if(s<3) // If there is, assign the student
{
cout << endl
<< "Please enter the information for the student:" << endl
<< "First Name: ";
cin >> dorms[curr_dorm-1].rooms[curr_room-1].students[s].first_name;
cout << "Last Name: ";
cin >> dorms[curr_dorm-1].rooms[curr_room-1].students[s].last_name;
cout << endl
<< "======================================================" << endl
<< dorms[curr_dorm-1].rooms[curr_room-1].students[s].first_name
<< " " << dorms[curr_dorm-1].rooms[curr_room-1].students[s].last_name
<< " was assigned to Dorm: " << curr_dorm << " Room: "
<< curr_room << endl
<< "======================================================" << endl;
// Mark this bed as taken
dorms[curr_dorm-1].rooms[curr_room-1].open[s]=0;
}
else // If all beds are taken
{
cout << endl
<< "***********************" << endl
<< "Room is already full!!!" << endl
<< "***********************" << endl;
}
// Return to the main menu
cout << "Press any key to return to the menu..." << endl << endl;
getch();
break;
case 2: // Remove a student from a room
// Set the dorm and room
set_dorm_n_room(&curr_dorm, &curr_room);
// Display the contents of the room
display_room(dorms, curr_dorm, curr_room);
// Get the student to remove
do{
cout << "Which student would you like to remove? ";
cin >> remove;
} while(remove<1 && remove>3);
// Remove the student
strcpy(dorms[curr_dorm-1].rooms[curr_room-1].students[remove-1].first_name, "");
strcpy(dorms[curr_dorm-1].rooms[curr_room-1].students[remove-1].last_name, "");
// Mark the bed as available
dorms[curr_dorm-1].rooms[curr_room-1].open[remove-1]=1;
cout << endl
<< "======================================" << endl
<< "The student was succesfully removed!!!" << endl
<< "======================================" << endl;
// Return to the main menu
cout << "Press any key to return to the menu..." << endl << endl;
getch();
break;
case 3: // Display the occupants of a room
// Set the dorm and room
set_dorm_n_room(&curr_dorm, &curr_room);
// Display the contents of the room
display_room(dorms, curr_dorm, curr_room);
// Return to the main menu
cout << "Press any key to return to the menu..." << endl << endl;
getch();
break;
case 4: // Find a specific student
// Ask the user to search by first name or last name
cout << "===============================================" << endl
<< "Search for a student:" << endl << endl
<< "1) By First Name" << endl
<< "2) By Last Name" << endl
<< "Choice - ";
cin >> search;
// Validate the entry, invalid entries to 1
search = (search>=1 && search<=2) ? search : 1;
if(search==1) // Search by first name
{
// Get the first name
cout << endl << "Please enter the first name to search for: ";
cin >> first;
// Search through the dorms and print out any matches
search_dorms(dorms, first, 'f');
}
else // Search by last name
{
// Get the last name
cout << endl << "Please enter the last name to search for: ";
cin >> last;
// Search through the dorms and print out any matches
search_dorms(dorms, last, 'l');
}
// Return to the main menu
cout << "Press any key to return to the menu..." << endl << endl;
getch();
break;
case 5: // Quit
break;
default: // Handle an invalid entry
cout << "******************" << endl
<< "Invalid option...." << endl
<< "******************" << endl << endl
<< "Press any key to return to the menu..." << endl << endl;
getch();
break;
}
} while(choice!=5);
return 0;
}