Need Help!!! The instructor said the program below when he tried to end the prog
ID: 3708795 • Letter: N
Question
Need Help!!!
The instructor said the program below when he tried to end the program it crashed and did not create a file. Can you help me fix the program and try to stop making it crash after user adds the information and modify any changes if needed and creates a file at the end. Thanks!
#include<fstream>
#include<string>
#include <iostream>
using namespace std;
class Employee
{
string NAME[10];
float SALARY[10];
int YEAR_HIRED[10];
int RECORD_NUMBER[10];
public:
void readData(int x)
{
cout<<"Enter the Name: ";
cin>>NAME[x];
cout<<"Enter the salary: ";
cin>>SALARY[x];
cout<<"Enter the year hired: ";
cin>>YEAR_HIRED[x];
cout<<"Enter the record number: ";
cin>>RECORD_NUMBER[x];
cout<<endl;
}
void writeData(int x)
{
cout<<RECORD_NUMBER[x]<<" "<<NAME[x]<<" "<<SALARY<<" "<<YEAR_HIRED<<endl;
}
int getRecordNum(int x)
{
return RECORD_NUMBER[x];
}
void modify(int x)
{
cout<<"RecNO Name Salary Year_Hired "<<endl;
writeData(x);
char ans='n';
cout<<endl<<"Do you want to modify the name: ";
cin>>ans;
if(ans=='y')
{
cout<<"enter the name: ";
cin>>NAME[x];
ans='n';
}
cout<<endl<<"Do you want to modify the salary: ";
cin>>ans;
if(ans=='y')
{
cout<<"enter the salary: ";
cin>>SALARY[x];
ans='n';
}
cout<<endl<<"Do you want to modify the year hired: ";
cin>>ans;
if(ans=='y')
{
cout<<"enter the year_hired: ";
cin>>YEAR_HIRED[x];
ans='n';
}
}
}emp1, emp;
int main()
{
int n=0;
fstream file("employee.dat", ios::in | ios::out);
for(int i=0;i<10;i++)
{
cout<<"Enter employee "<<i+1<<" details" <<endl;
emp1.readData(i);
file.write((char *)&emp1, sizeof(emp1));
}
int recordNum;
long position;
char found='f';
char ans='y';
while(ans=='y'|| ans=='Y')
{
cout<<"Enter record Number of employee to modify: ";
cin>>recordNum;
file.seekg(0);
int i=0;
while(!file.eof())
{
position = file.tellg();
file.read((char *)&emp1, sizeof(emp1));
if(emp1.getRecordNum(i) == recordNum)
{
emp1.modify(i);
file.seekg(position);
file.write((char *)&emp1, sizeof(emp1));
found = 't';
break;
}
i++;
}
if(found=='f')
{
cout<<" Employee record is not found "<<endl;
}
file.seekg(0);
cout<<endl<<"Do you want to modify another record (y / n): "<<endl;
cin>>ans;
}
int i=0;
file.seekg(0);
cout<<endl<<"Employee records: ";
cout<<endl<<"RecNO Name Salary Year_Hired "<<endl;
while(!file.eof())
{
file.read((char *)&emp, sizeof(emp));
emp.writeData(i);
i++;
}
system("pause");
return 0;
}
Explanation / Answer
I would like use alternative, such object arrays instead of arrays... if you allow it...
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
class Employee
{
string NAME[10];
float SALARY[10];
int YEAR_HIRED[10];
int RECORD_NUMBER[10];
public:
void readData(int x)
{
cout << "Enter the Name: ";
cin >> NAME[x];
cout << "Enter the salary: ";
cin >> SALARY[x];
cout << "Enter the year hired: ";
cin >> YEAR_HIRED[x];
cout << "Enter the record number: ";
cin >> RECORD_NUMBER[x];
cout << endl;
}
void writeData(int x)
{
cout << RECORD_NUMBER[x] << " " << NAME[x] << " " << SALARY << " " << YEAR_HIRED << endl;
}
int getRecordNum(int x)
{
return RECORD_NUMBER[x];
}
void modify(int x)
{
cout << "RecNO Name Salary Year_Hired " << endl;
writeData(x);
char ans = 'n';
cout << endl
<< "Do you want to modify the name: ";
cin >> ans;
if (ans == 'y')
{
cout << "enter the name: ";
cin >> NAME[x];
ans = 'n';
}
cout << endl<< "Do you want to modify the salary: ";
cin >> ans;
if (ans == 'y')
{
cout << "enter the salary: ";
cin >> SALARY[x];
ans = 'n';
}
cout << endl
<< "Do you want to modify the year hired: ";
cin >> ans;
if (ans == 'y')
{
cout << "enter the year_hired: ";
cin >> YEAR_HIRED[x];
ans = 'n';
}
}
} emp1, emp;
int main()
{
int n = 0;
fstream file("employee.dat", ios::in | ios::out);
for (int i = 0; i < 10; i++)
{
cout << "Enter employee " << i + 1 << " details" << endl;
emp1.readData(i);
file.write((char *)&emp1, sizeof(emp1));
}
int recordNum;
long position;
char found = 'f';
char ans = 'y';
while (ans == 'y' || ans == 'Y')
{
cout << "Enter record Number of employee to modify: ";
cin >> recordNum;
file.seekg(0);
int i = 0;
while (!file.eof())
{
position = file.tellg();
file.read((char *)&emp1, sizeof(emp1));
if (emp1.getRecordNum(i) == recordNum)
{
emp1.modify(i);
file.seekg(position);
file.write((char *)&emp1, sizeof(emp1));
found = 't';
break;
}
i++;
}
if (found == 'f')
{
cout << " Employee record is not found " << endl;
}
file.seekg(0);
cout << endl
<< "Do you want to modify another record (y / n): " << endl;
cin >> ans;
}
int i = 0;
file.seekg(0);
cout << endl
<< "Employee records: ";
cout << endl
<< "RecNO Name Salary Year_Hired " << endl;
while (!file.eof())
{
file.read((char *)&emp, sizeof(emp));
emp.writeData(i);
i++;
}
// system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.