/***
//// Programmer : Mohammad Rastkar
//// Capability : Implementing a variable sized record.
//// Function : User -> can enter new records ( hourly or salaried ) and see them
(in separated sections).
Program -> saves recoeds in a file and show them later, in separate sections.
Last Build : APR_3_08
***/
///////////// Includes /////////////
#include <cstdlib> // system()
#include <conio.h> // getch()
#include <fstream>
#include <iostream>
using namespace std;
///////////// Data types /////////////
enum TYPE { SALARIED = '0', HOURLY = '1' };
struct Employee_Record // Employee record
{
char PAY_TYPE; // Tag_Field : SALARIED = '0', HOURLY = '1'
union
{
struct // SALARIED
{
double MONTHLY_RATE;
int START_DATE;
};
struct // HOURLY
{
double RATE_PER_HOUR;
int REG_HOURS;
int OVERTIME_HOURS;
};
};
int ID;
int AGE;
char DEPT;
} Employee;
///////////// Variable Declarations /////////////
char choice; // for choice in menu
fstream *fs; // file to save employee records
///////////// Main /////////////
int main()
{
while (1)
{
do // menu
{
system( "cls" ); // clear screen
cout << "(1) Enter a new Record \n";
cout << "(2) Display Records \n";
cout << "(3) Exit \n\n";
cout << " Enter a choice (1-3) : ";
cin >> choice;
} while ( choice < '1' || choice > '3');
switch ( choice )
{
case '1' : // Enter Records
system( "cls" );
cout << "\n\t\t < Entering a new record > ";
cout << "\n Enter the following informations for the new record : ";
cout << "\n\n PAY_TYPE ( SALARIED : 0, HOURLY : 1 ) : ";
cin >> Employee.PAY_TYPE;
cout << "\n ID : ";
cin >> Employee.ID;
cout << "\n AGE : ";
cin >> Employee.AGE;
cout << "\n DEPT (one character) : ";
cin >> Employee.DEPT;
if ( Employee.PAY_TYPE == SALARIED )
{
cout << "\n MONTHLY_RATE : ";
cin >> Employee.MONTHLY_RATE;
cout << "\n START_DATE : ";
cin >> Employee.START_DATE;
}
if ( Employee.PAY_TYPE == HOURLY )
{
cout << "\n RATE_PER_HOUR : ";
cin >> Employee.RATE_PER_HOUR;
cout << "\n REG_HOURS : ";
cin >> Employee.REG_HOURS;
cout << "\n OVERTIME_HOURS : ";
cin >> Employee.OVERTIME_HOURS;
}
fs = new fstream( "Employees.txt", ios::out | ios::app | ios::binary );
if (fs->fail())
{
cout << "\n Cann't open or create \"Employees\" file\n" << flush;
system( "pause" );
return 1;
}
fs->write( (char *) &Employee, sizeof(Employee) );
fs->close();
delete fs;
fs = 0;
break;
case '2' : // Display Records
system( "cls" );
// Print Salaried records...
fs = new fstream( "Employees.txt", ios::in | ios::binary );
cout << "\n\t\t < Salaried >\n\n";
cout << "ID\tAGE\tDEPT\tMONTHLY_RATE\tSTART_DATE\n"
<< "-------------------------------------------------- \n";
while (fs->read( (char *) &Employee, sizeof(Employee) ))
{
if ( Employee.PAY_TYPE == SALARIED )
{
cout << Employee.ID << '\t';
cout << Employee.AGE << '\t';
cout << Employee.DEPT << '\t';
cout << Employee.MONTHLY_RATE << "\t\t";
cout << Employee.START_DATE << '\n';
}
}
cout << "\n To see the Hourly records, " << flush;
system( "pause" );
// Print Hourly records...
system( "cls" );
fs->close();
delete fs;
fs = 0;
fs = new fstream( "Employees.txt", ios::in | ios::binary );
cout << "\n\t\t\t < Hourly > \n\n";
cout << "ID\tAGE\tDEPT\tRATE_PER_HOUR\tREG_HOURS\tOVERTIME_HOURS\n"
<< "---------------------------------------------------------------------- \n";
while ( fs->read( (char *) &Employee, sizeof(Employee) ) )
{
if ( Employee.PAY_TYPE == HOURLY )
{
cout << Employee.ID << '\t';
cout << Employee.AGE << '\t';
cout << Employee.DEPT << '\t';
cout << Employee.RATE_PER_HOUR << "\t\t";
cout << Employee.REG_HOURS << "\t\t";
cout << Employee.OVERTIME_HOURS << '\n';
}
}
cout << "\nTo see Menu, " << flush;
system( "pause" );
fs->close();
delete fs;
fs = 0;
break;
case '3' :
return 0;
break;
}
}
}