The program is suppose to write and then read data from the binary file and use seekg to display specific data depending on what is entered by the user. It displays the correct information when 1 is entered but not 0. Any help would be greatly appreciated.
#include <iostream>
#include <fstream>
using namespace std;
class employee {
char name[20];
double salary;
public:
employee (char *i, double s) { strcpy(name, i); salary = s;}
friend ostream &operator<<(ostream &out_data, employee emp); //inserter
void store (fstream &stream);
void read_in (fstream &stream);
};
void employee::store(fstream &stream) {
stream.write(name,20) ;
stream.write((char *) &salary, sizeof(double)); }
void employee::read_in (fstream &stream) {
stream.read(name,20);
name[20] = '\0';
stream.read((char *) &salary, sizeof(double)); }
ostream &operator<<(ostream &stream, employee emp) {
stream << emp.name << emp.salary;
return stream; }
int main() {
employee emp1("Nathan", 200.45);
employee emp2("Zac", 100.45);
employee temp(" ",0);
fstream emp("G:/employee", ios::out | ios::binary);
emp1.store(emp);
emp2.store(emp);
emp.close();
fstream in("G:/employee", ios::in | ios::binary);
int recNo;
do{
cout << "Which Record? "; cin >> recNo;
in.seekg(recNo*20 + sizeof(double), ios::beg);
temp.read_in(in);
cout << temp;
}while(in.good());
in.close();
return 0;
}