我写了一个程序,为一个结构数组动态分配内存。它看起来工作正常,但是当我尝试删除阵列时,计算机会制作一个" bong"噪音和程序停止响应。它不会抛出错误或任何东西,只是停止。我尝试了delete
和delete[]
,结果相同。我已经尝试移动删除位置,发现我可以在创建后立即将其删除,但不能在将其传递给任何函数后删除。谁能告诉我为什么我不能删除记忆?
#include <iostream>
#include <iomanip>
#include <fstream>
using std::cin;
using std::cout;
using std::endl;
using std::ifstream;
using std::ofstream;
using std::setw;
using std::left;
using std::fixed;
using std::setprecision;
//structure of an employee
struct Employee
{
char first_name[32];
char last_name[32];
char SSN[11];
float wage;
int hours;
char status;
};
void ReadData(Employee work_force[]);
void PrintData(Employee work_force[], int number_entries);
float CalculateStarightPay(Employee worker);
float CalculateOvertimePay(Employee worker);
int FindNumberEntries();
const int SPACING = 15; //spacing used in formating
const char dataFile[] = "EmployeeData.txt"; //file to read data from
const int union_dues = 5; //amount deducted from pay for the union
int main()
{
int number_entries = FindNumberEntries();
//array of employees
Employee * work_force = new Employee[number_entries];
//read in data
ReadData(work_force);
//prints the data
PrintData(work_force, number_entries);
//clean up memory
delete[] work_force;
return 0;
}
//finds the number of entries in the file
int FindNumberEntries()
{
int counter = 0;
//worker to read through file entries
Employee worker_temp;
ifstream input;
input.open(dataFile);
if (input.is_open())
{
while (!input.eof())
{
input >>
worker_temp.first_name >>
worker_temp.last_name >>
worker_temp.SSN >>
worker_temp.wage >>
worker_temp.hours >>
worker_temp.status;
cin.clear();
counter++;
}
input.close();
}
else
{
cout << "File could not be opened!" << endl;
}
return counter - 1;
}
//reads employee data from file
void ReadData(Employee work_force[])
{
ifstream input;
input.open(dataFile);
//reads in entries
if (input.is_open())
{
for (int i = 0; !input.eof(); i++)
{
//reads in employee data
input >>
work_force[i].first_name >>
work_force[i].last_name >>
work_force[i].SSN >>
work_force[i].wage >>
work_force[i].hours >>
work_force[i].status;
cin.clear();
}
input.close();
}
else
{
//error that file could not open
cout << "File could not be opened!" << endl;
}
}
//calculates straight pay
float CalculateStarightPay(Employee worker)
{
//determines of worker is fulltime or not
if (worker.status == 'F')
{
if (worker.hours > 40)
{
return ((worker.wage * 40) - 5);
}
else
{
return (worker.wage * worker.hours) - union_dues;
}
}
else
{
if (worker.hours > 40)
{
return (worker.wage * 40);
}
else
{
return worker.wage * worker.hours;
}
}
}
//calculate overtime pay
float CalculateOvertimePay(Employee worker)
{
//deermines if there are any overtime hours
if (worker.hours <= 40)
{
return 0;
}
else
{
//calculates overtime pay
return ((worker.hours - 40) * (worker.wage * 1.5));
}
}
//prints employee data in a well formated manner
void PrintData(Employee work_force[], int number_entries)
{
delete work_force;
float straight_pay = 0.0F;
float Overtime_pay = 0.0F;
char name[32] = { '\0' };
//print headers
cout << left <<
setw(SPACING) << "Name" <<
setw(SPACING) << "SSN" <<
setw(SPACING) << "Hourly Wage" <<
setw(SPACING) << "Hours Worked" <<
setw(SPACING) << "Straight Pay" <<
setw(SPACING) << "Overtime Pay" <<
setw(SPACING) << "Status" <<
setw(SPACING) << "Net Pay" << endl;
//prints data for each EMPLOYEE
for (int i = 0; i < number_entries; i++)
{
straight_pay = CalculateStarightPay(work_force[i]);
Overtime_pay = CalculateOvertimePay(work_force[i]);
//adds a space after first name
work_force[i].first_name[strlen(work_force[i].first_name) + 1] = '\0';
work_force[i].first_name[strlen(work_force[i].first_name)] = ' ';
//puts last name and first name together
strcpy(name, strcat(work_force[i].first_name, work_force[i].last_name));
//prints out all the data in a nic eformat
cout << fixed << setprecision(2) <<
setw(SPACING ) << name <<
setw(SPACING) << work_force[i].SSN << '$' <<
setw(SPACING) << work_force[i].wage <<
setw(SPACING) << work_force[i].hours << '$' <<
setw(SPACING) << straight_pay << '$' <<
setw(SPACING) << Overtime_pay <<
setw(SPACING) << work_force[i].status << '$' <<
setw(SPACING) << (straight_pay + Overtime_pay) << endl;
}
}
答案 0 :(得分:1)
请delete work_force;
位于PrintData
的顶部。
使用std::string
s作为您的姓名和SSN。 (固定长度的字符串是等待发生的可用性事故)。
使用std::vector<Employee>
。重要的是,这意味着您不再需要使用new
(这是您应该总是尝试避免的事情)。这也意味着您只需要阅读一次文件 - 您只需阅读该条目,然后使用push_back
将其推送到向量。
从输入流中读取时,您需要尝试读取,然后测试流以查看是否已经点击了eof。所以read函数看起来像:
while (input >>
worker_temp.first_name >>
worker_temp.last_name >>
worker_temp.SSN >>
worker_temp.wage >>
worker_temp.hours >>
worker_temp.status)
{
workforce.push_back(worker_temp);
}