1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
#include <iostream> // for user I/O
#include <fstream> // for file streams
#include <iomanip> // for fixed, setw and setprecision
#include <cctype> // for toupper()
#include <string>
using namespace std;
// declare enumerated type globally
enum occupantType {EMPTY, LAWYER, PARALEGAL, ASSISTANT};
//global constants
const int FLOOR_ROW = 5;
const int OFFICE_COL = 8;
const string BUILDING_FILE = "BUILDING.BIN";
// typedef aliases
typedef int officeBuilding[FLOOR_ROW][OFFICE_COL];
//prototypes
bool readFile(officeBuilding);
void writeFile(officeBuilding);
void initializeArray(officeBuilding);
char mainMenu();
int calculateCurrentStat(officeBuilding);
char convertNumberToLetter(int);
int convertLetterToNumber(char);
int optionPrompts(int&, char&, char&);
int occupantType();
void displayEmptyOffices(officeBuilding);
int displayOccupiedOffices(officeBuilding);
string displayOccupantType(int);
void newOccupancyMessage(string, int, char, char, officeBuilding);
//**************************************************************************
int main()
{
//variables
officeBuilding buildingArray;
int officeCount = 0;
bool results;
bool exitLoop = false;
bool validatedRequest = true;
char choice;
int flrNum,
enumType,
officeNum,
total;
char officeLetter;
string occupantName;
results = readFile(buildingArray); //read BUILDING.BIN file
total = calculateCurrentStat(buildingArray); //calculates and returns the individual types
while(choice != 'D' && choice != 'd')
{
choice = mainMenu();
//if all offices are empty and choice is 1 or 2, display error and re-prompt menu
if (total == 0)
{
while (choice == '1' || choice == '2')
{
cout << endl;
cout << "*** ERROR! ALL OFFICES ARE EMPTY. TRY AGAIN ***" << endl << endl;
choice = mainMenu();
}
}
if (choice == 'D' || choice == 'd') //if choice is 'D' exit the loop
{
cout << "Saving data to file BUILDING.BIN" << endl;
writeFile(buildingArray); //function to save data to binary file
return 1;
}
cout << endl;
choice = static_cast<int>(choice); //change status of choice from char to int
if(choice == '3')
{
//validation functions
do
{
displayEmptyOffices(buildingArray); //displays all empty offices
//displays option 3 questions (floor number, column letter, and occupant type)
enumType = optionPrompts(flrNum, officeLetter, choice);
officeNum = convertLetterToNumber(officeLetter); //converts the office letter to a numeric value
}while (!validatedRequest); //loops until correct number and office letter are entered
//display functions
buildingArray[flrNum][officeNum] = enumType; //adds the occupant type to the 2D array (enumerated type)
occupantName = displayOccupantType(enumType); //occupantName becomes the string value of the enumerated type name
newOccupancyMessage(occupantName, flrNum, officeLetter, choice, buildingArray); //displays new occupant message
total =calculateCurrentStat(buildingArray); //re-calculates and displays all current status in the array
}
}
cin.ignore();
return 0;
}
//*****************************************************************************************************************
bool readFile(officeBuilding buildingArray)
{
//variables
string filename;
bool exitLoop = true;
int floor;
int office;
int numCount;
//reading from input binary file for inBuildingData
ifstream inBuildingData;
inBuildingData.open(BUILDING_FILE.c_str(), ios::binary | ios::in);
//prompt
cout << endl << "Enter data filename: ";
cin >> filename;
// verification that file exists
if (filename != BUILDING_FILE)
{
cout << "Data file: " << filename << ". Does not exist." << endl;
cout << "Program will continue to main menu." << endl << endl;
initializeArray(buildingArray);
}
else // read all data into 2D array
{
for (int floor= 0; floor < 5; floor++)
{
for (int office= 0; office < 8; office++)
inBuildingData.read( reinterpret_cast<char*>(&buildingArray[floor][office]), sizeof(officeBuilding) );
}
// inBuildingData.read( reinterpret_cast<char*>(&buildingArray), 40 * sizeof(officeBuilding) );
// if (inBuildingData) // no error state,// so 100 numbers successfully read
// numCount = 40;
// else // read error occurred
// {
// numCount = (inBuildingData.gcount() / sizeof(int));
// inBuildingData.clear(); // reset file stream
// }
}
inBuildingData.close();
return exitLoop;
}
//*****************************************************************************************************************
void writeFile(officeBuilding buildingArray)
{
//writing to the binary file OUT_BUILDING.BIN
ofstream outBuildingData;
outBuildingData.open(BUILDING_FILE.c_str(), ios::binary | ios::out);
//variables
int floor = 0,
office = 0;
if (outBuildingData.is_open())
{
for (int floor; floor < 5; floor++)
{
for (int office= 0; office < 8; office++)
outBuildingData.write( reinterpret_cast<char*>(&buildingArray[floor][office]), sizeof(officeBuilding) );
}
cout << "Data has been successfully saved. Goodbye!" << endl;
}
else
cout << "Unable to open file!" << endl;
outBuildingData.close();
return;
}
|