my_library.h
Code:
char ints[6];
char floats[20];
void prompt(void);
class data
{
public:
char last[31], first[36];
float salary;
int yrhired;
void display(void);
void modify(void);
}employee[20];
int count = 0;
while (count < 20){
employee[count].last[0] = '\0';
employee[count].first[0] = '\0';
employee[count].salary = -1;
employee[count].yrhired = -1;
count++;
}
void display_choose(void){
int display_choice;
cout << "Which record? (1-20)" << endl;
cin >> display_choice;
if(display_choice > 20 || display_choice < 1){
cout << "Invalid choice" << endl;
display_choose();
}
else
employee[display_choice+1].display();
}
void data::display(void){
cout << endl << last << "," << first << endl << "Salary: " << salary << endl << "Year hired: " << yrhired << endl;
prompt();
}
void modify_choose(void){
int modify_choice;
cout << "Which record? (1-20)" << endl;
cin >> modify_choice;
if (modify_choice > 20 || modify_choice < 1){
cout << "Invalid choice" << endl;
modify_choose();
}
else
employee[modify_choice+1].modify();
}
void data::modify(void){
int modcount = 1;
cout << endl << "Last name: ";
last[0] = getchar();
while (modcount < 30 && last[modcount-1] != '\n'){
last[modcount] = getchar();
modcount++;
}
last[modcount] = '\0';
modcount = 1;
cout << endl << "First and Middle: ";
first[0] = getchar();
while (modcount < 35 && last[modcount-1] != '\n'){
first[modcount] = getchar();
modcount++;
}
first[modcount] = '\0';
cout << endl << "Salary: ";
cin >> salary;
cout << endl << "Year hired: ";
cin >> yrhired;
prompt();
}
void save(void){
int savecount = 0;
FILE *fp;
char endline[2];
endline[0] = '\n';
endline[1] = '\r';
fp = fopen("company.db", "wb");
if (fp = NULL){
cout << "Error opening 'company.db'";
prompt();
}
else{
while (savecount < 20){
fwrite("Last: ", 1, 6, fp);
fwrite(employee[savecount].last, 1, 30, fp);
fwrite(endline, 1, 2, fp);
fwrite("First and Middle: ", 1, 18, fp);
fwrite(employee[savecount].first, 1, 35, fp);
fwrite(endline, 1, 2, fp);
fwrite("Salary: ", 1, 8, fp);
sprintf(floats, "%f", employee[savecount].salary);
fwrite(floats, 1, 20, fp);
fwrite(endline, 1, 2, fp);
fwrite("Year hired: ", 1, 12, fp);
sprintf(ints, "%d", employee[savecount].yrhired);
fwrite(ints, 1, 6, fp);
fwrite(endline, 1, 2, fp);
savecount++;
}
}
fclose (fp);
prompt();
}
void retrieve(void){
char character;
FILE *fp;
fp = fopen("company.db", "r");
if (fp = NULL){
cout << "Error opening 'company.db'";
prompt();
}
else{
while ((character = fgetc(fp)) != EOF){
printf("%c", character);
}
}
fclose(fp);
prompt();
}
my_program.cpp
Code:
#include <iostream>
using namespace std;
#include "my_library.h"
void prompt(void){
char choice;
printf("(1) Display\n(2) Modify\n(3) Save\n(4) Retrieve\n(5) Exit\n");
cin >> choice;
switch(choice){
case 1:
display_choose();
break;
case 2:
modify_choose();
break;
case 3:
save();
break;
case 4:
retrieve();
break;
case 5:
exit(0);
break;
default:
printf("Invalid choice\n");
prompt();
break;
}
}
int main() {
prompt();
}
I don't quite see how the rest of the code is relevant to the problem, but beggars can't be choosers.