Question: Below is my code for a C++ assignment when I run it I am getting a run time error and it aborts. The assignment is
Below is my code for a C++ assignment when I run it I am getting a run time error and it aborts. The assignment is to write code that will read in information from an employee.dat file with first name last name and salary seperated by commas and print them to a new file adding 50 dollars to the salar:
EX: Doe, John, 2000 would become Doe, John, 2050.
Any help would be great!!
#include
#include
#include
#include
#include
#include
using namespace std;
class employee {
private: string first_name;
string last_name;
float monthly_pay;
public:
employee()
{
first_name = "";
last_name = "";
monthly_pay = 0.0;
}
void set_fname(string fname)
{
first_name = fname;
}
void set_lname(string lname)
{
last_name = lname;
}
void set_pay(float pay)
{
monthly_pay = pay;
}
string get_fname()
{
return first_name;
}
string get_lname()
{
return last_name;
}
float get_pay()
{
return monthly_pay;
}
void setemployee() {
cout << last_name;
cout << first_name;
cout << monthly_pay;
}
};
int main() {
string name, fname, lname;
float pay;
int i = 0;
int count = 0;
std::string::size_type sz;
ifstream myfile("employee.dat");
ofstream openfile("update.dat");
employee emp[100];
if (myfile.is_open())
{
while (!myfile.eof()) {
if (getline(myfile, name, ','))
{
if (count == 0)
{
emp[i].set_lname(name);
count = count + 1;
}
else if (count == 1)
{
emp[i].set_fname(name);
count = count + 1;
}
else
{
pay = std::stof(name, &sz);
emp[i].set_pay(pay);
count = count + 1;
}
}
if (count >= 3)
{
emp[i].setemployee();
openfile << emp[i].get_lname() << ',' << emp[i].get_fname() << ',' << emp[i].get_pay() << '.' << endl;
i++;
count = 0;
}
}
}
else cout << "Unable to open file";
system("pause");
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
