When i try to display the information its the same rubbish coming on the screen.
Code:
#include <iostream>
#include <fstream>
using namespace std;
// Declaration of OK_Photo structure
struct OK_Photo
{
char name[20]; //Employee Full Name
char dept[20]; //Department Employee works in
float overtime; //Overtime worked
float hrs; //Hours worked
float deductions; // Salary Deductions
int EmpNum; // Employee Number
};
//Function Prototypes
int menu();
void AddRecord(fstream &);
void DisplayRecord(fstream &);
void ChangeRecord(fstream &);
/*
void Pay(fstream &);
*/
int main()
{
int choice;
fstream payroll ("payroll.txt", ios::app | ios::binary);
OK_Photo record = {" ", 0, 0.0};
//writing the blank records
for (int count = 0; count < 5; count++)
{
payroll.write(reinterpret_cast<char *>(&record), sizeof(record));
}
//inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
payroll.close();
payroll.open("payroll.txt", ios::app | ios::binary);
do
{
choice = menu();
switch (choice)
{
case 1: AddRecord(payroll);
break;
case 2: DisplayRecord(payroll);
break;
case 3: ChangeRecord(payroll);
break;
/*
case 4: Pay(payroll);
break;
*/
case 5: cout << "Exiting Program...\n\n";
}
}while (choice != 5);
payroll.close();
return 0;
}
int menu ()
{
int selection;
cout<<"\n\n===== Main Menu ====="<<endl;
cout << "1) Add Record \n";
cout << "2) Display Record \n";
cout << "3) Update Record \n";
cout << "4) Pay \n";
cout << "5) Exit Program \n";
cout << "Please enter[1, 2, 3, 4 or 5]: ";
cin >> selection;
while (selection< 1 || selection > 5)
{
cout << "Invalid Choice!!\n";
cin >> selection;
}
return selection;
}
void ChangeRecord(fstream &file)
{
fstream payroll("payroll.txt", ios::in);
OK_Photo record;
// Get the record number of the desired record.
cout << "\n\nEnter Employee Number for update: \n";
cin >> record.EmpNum;
// Move to the record and read it.
payroll.seekg(record.EmpNum * sizeof(record), ios::beg);
payroll.read(reinterpret_cast<char *>(&record),
sizeof(record));
// Display the record contents
cout << "\n\nEmployee Name: ";
cout << record.name << endl;
cout << "Department: ";
cout << record.dept << endl;
cout << "Hours Worked: ";
cout << record.hrs << endl;
cout << "Overtime: ";
cout << record.overtime << endl;
cout << "Deductions :";
cout << record.deductions <<endl;
cout << "Total Salary :";
cout << record.hrs + record.overtime - record.deductions;
// Get the new record data.
cout << "\n\nEmployee Number: ";
cin >> record.EmpNum;
cout << "Employee Name: ";
cin.ignore();
cin.getline(record.name, 20);
cout << "Department: ";
cin >> record.dept;
cout << "Hours Worked: ";
cin.ignore();
cin >> record.hrs;
cout << "Overtime: ";
cin.ignore();
cin >> record.overtime;
cout << "Total Deductions: ";
cin.ignore();
cin >> record.deductions;
// Move back to the beginning of this record's position.
payroll.seekp(record.EmpNum * sizeof(record), ios::beg);
// Write the new record over the current record.
payroll.write(reinterpret_cast< char *>(&record),
sizeof(record));
// Close the file.
file.close();
}
void DisplayRecord(fstream &file)
{
fstream payroll;
OK_Photo record;
payroll.open("payroll.txt", ios::in);
// Get the record number of the desired record.
cout << "\n\nEmployee Number: ";
cin >> record.EmpNum;
// Move to the record and read it.
payroll.seekg(record.EmpNum * sizeof(record), ios::beg);
payroll.read(reinterpret_cast<char *>(&record),
sizeof(record));
cout<<"\n\n-----Displaying record for Employee " <<record.name<<endl;
// Display the record contents
cout << "\n\nEmployee Name: ";
cout << record.name << endl;
cout << "Department: ";
cout << record.dept << endl;
cout << "Hours Worked: ";
cout << record.hrs << endl;
cout << "Overtime: ";
cout << record.overtime << endl;
cout << "Deductions :";
cout << record.deductions <<endl;
//closing the file
file.close();
}
void AddRecord(fstream &file)
{
cout << "\n\nPlease enter the information for the new data: \n";
ofstream payroll ("payroll.txt", ios::app);
OK_Photo record;
//Info of the new data
cout << "\n\nEmployee Number: ";
cin >> record.EmpNum;
cout << "Employee Name: ";
cin.ignore();
cin.getline(record.name, 31);
cout << "Department: ";
cin >> record.dept;
cout << "Hours Worked: ";
cin.ignore();
cin >> record.hrs;
cout << "Overtime: ";
cin.ignore();
cin >> record.overtime;
cout << "Deductions: ";
cin.ignore();
cin >> record.deductions;
payroll.write(reinterpret_cast< const char *>(&record),
sizeof(record));
//closing the file
file.close();
}

