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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
#include <cstdlib>
#include <iostream>
#include <string>
#include <iomanip>
#include <istream>
#include <fstream>
using namespace std;
const int SIZE_A = 25;
const int SIZE_B = 10;
typedef char String_A[SIZE_A];
typedef char String_B[SIZE_B];
struct EmpInfo{
String_A fullname;
String_B id;
};
struct PayInfo{
EmpInfo nameId;
double hourlyRate;
};
//prototypes below here
void Create(fstream &);
int Menu();
void Input(fstream &, PayInfo &);
void Read(fstream &, PayInfo &);
void Append(fstream &, PayInfo &);
void Search(fstream &, PayInfo &);
void Delete(fstream &,fstream &, PayInfo &);
int main()
{
char filename[]= "employees.dat";
const int HOURS = 80;
PayInfo employee;
int choice, answer;
fstream EmployeeFile, EmployeeFile2;
do
{
choice = Menu();
switch(choice)
{
case 1: Create(EmployeeFile);
break;
case 2: Input(EmployeeFile,employee);
break;
case 3: Read(EmployeeFile,employee);
break;
case 4: Append(EmployeeFile,employee);
break;
case 5: Delete(EmployeeFile,EmployeeFile2,employee);
break;
case 6: Search(EmployeeFile,employee);
break;
case 7:
case 8:
case 9: cout << "\n GOOD BYE." << endl << endl;
break;
}
cout << endl;
}while (choice != 9);
cout << endl << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
void Create(fstream &myFile)
{
myFile.open("employees.dat", ios::out | ios::binary);
if(myFile.good())
{
cout << "File was created successfully.\n"<<endl;
}
else
{
cout << "File did not open successfully.\n" << endl << endl;
}
myFile.clear();
myFile.close();
}
int Menu()
{
int mychoice;
cout << "Choose a menu option: " << endl;
cout << "1- Create a Binary File\n";
cout << "2- Populate with a New Record\n";
cout << "3- Read and Display the Contents of the File\n";
cout << "4- Append a Record\n";
cout << "5- Delete a Record\n";
cout << "6- Search For a Record\n";
cout << "7- Modify a Record\n";
cout << "8- Wipe out all records\n";
cout << "9- Exit\n\n";
cout << "Enter your choice: ";
cin >> mychoice;
return mychoice;
}
void Input(fstream &file, PayInfo &employee)
{
file.open("employees.dat", ios::out | ios::binary);
if(file.good())
{
cout << "File was created successfully.\n"<<endl;
}
else
{
cout << "File did not open successfully.\n" << endl << endl;
}
char answer;
cin.ignore();
do
{
cout << "\nEnter the following employee info." << endl << endl;
cout << "Full name: ";
cin.getline(employee.nameId.fullname,sizeof(employee.nameId.fullname));
cout << "Employee ID#: ";
cin.getline(employee.nameId.id,sizeof(employee.nameId.id));
do
{
cout << "Hourly Rate: $";
cin >> employee.hourlyRate;
if (employee.hourlyRate > 0)
break;
else
cout << "Invalid pay rate. Must be > $0.00. Try again." << endl;
} while (1);
file.write((char *)&employee, sizeof(employee));
cout << "\nDo you have another employee to enter? (Y or N) ";
cin >> answer;
cin.ignore();
}while (answer == 'Y' || answer == 'y');
cout << endl << endl;
file.close();
}
void Read(fstream &file, PayInfo &employee)
{
char answer;
file.open("employee.dat", ios::in | ios::binary);
if (!file)
{
cout << "Error opening file. Please try again.\n";
}
cout << "Here is the list of employees:\n\n";
file.read(reinterpret_cast<char*>(&employee),sizeof(employee));
while (!file.eof())
{
cout << "\nFull name: ";
cout << employee.nameId.fullname;
cout << "\nEmployee ID#: ";
cout << employee.nameId.id;
cout << "\nHourly Rate: $";
cout << employee.hourlyRate;
file.read(reinterpret_cast<char*>(&employee),sizeof(employee));
}
cout << "That's the last record in the file!\n";
file.clear();
file.close();
}
|