Hey guys, I am having a problem with a homework assignment which I was given.
I have created a vector of type Kitchen * like this
vector<Kitchen *>members(10);
the code works well enough without problems but when i go to insert a new member at an arbitrary location, it works fine but it appears to write over rather than shift the previous element to the right.
Just to simplify things i have included only the test file. The part in question is in the middle. Just in case you are wandering, this code works in every way, except for the above stated problem. No runtime or compile time errors. Further checking the element in question shows it to be empty. I have checked the copy constructor and it appears fine.
Thanks,
Greg
The necessary declarations have been included like <vector> etc
I have created a vector of type Kitchen * like this
vector<Kitchen *>members(10);
the code works well enough without problems but when i go to insert a new member at an arbitrary location, it works fine but it appears to write over rather than shift the previous element to the right.
Just to simplify things i have included only the test file. The part in question is in the middle. Just in case you are wandering, this code works in every way, except for the above stated problem. No runtime or compile time errors. Further checking the element in question shows it to be empty. I have checked the copy constructor and it appears fine.
Thanks,
Greg
The necessary declarations have been included like <vector> etc
Rich (BB code):
int main()
{
ifstream fin;
fin.open("input.txt");
if(fin.fail())
{
cout << "Could not open file \"input.txt\", exiting program.\n";
exit(1);
}
ofstream fout;
fout.open("output.txt");
if(fout.fail())
{
cout << "Could not open file \"output.txt\", exiting program.\n";
exit(1);
}
vector<Kitchen *>members(10);
for (unsigned int i = 0; i < members.size(); i++)
{
members = new Kitchen;
fin >> *members;
if(members->ret_name() != "Empty")
{
cout << *members << endl << endl;
fout << *members << endl << endl;
}
}
vector<Kitchen *>::iterator It;
It = members.begin();
It+=5;
members.erase(It);
Kitchen * const temp2 = new Kitchen("Greg","Bacon-Sandwich-chef",30000,23,2);
//cout << temp2->salary_details() << endl;
It = members.begin();
It+=5;
members.insert(It,temp2); //temp2 now invalidated. attempting to delete temp2 after this statement will result in a runtime error
cout << "output all elements after insertion.\n\n";
for (unsigned int j = 0; j < members.size(); j++)
{
if(members[j]->ret_name() != "Empty")
{
cout << *members[j] << endl << endl;
fout << *members[j] << endl;
}
}
int retire = 0,promo = 0,less5 = 0;
double tot_salaries = 0;
for(unsigned int k = 0; k < members.size(); k++)
{
if(members[k]->ret_name() != "Empty")
{
if(members[k]->retirement_check())
retire++;
if(members[k]->promotion_check())
promo++;
if(members[k]->service_check())
less5++;
tot_salaries += members[k]->salary_details();
}
}
cout << "Total chefs due for retirement:\t\t\t" << retire << endl
<< "Total chefs due for promotion:\t\t\t" << promo << endl
<< "Total chefs with service less than 5 years:\t" << less5 << endl
<< "Total cost of salaries:\t\t\t\t" << "Euro: " << tot_salaries << endl << endl;
fout << "Total chefs due for retirement:\t\t\t" << retire << endl
<< "Total chefs due for promotion:\t\t\t" << promo << endl
<< "Total chefs with service less than 5 years:\t" << less5 << endl
<< "Total cost of salaries:\t\t\t\t" << "Euro: " << tot_salaries << endl;
for (unsigned int j = 0; j < members.size(); j++)
{
cout << *members[j] << endl << endl;
fout << *members[j] << endl;
}
cout << "Total salaries for chef 1 and 2 is: " << (*members[0] + *members[1]).salary_details() << endl;
if(*members[1] >= *members[2])
cout << "Chef 1 has a larger or equal salary than Chef 2.\n\n";
if(*members[1] > *members[2])
cout << "Chef 1 has a larger salary than Chef 2.\n\n";
if(*members[1] == *members[1])
cout << "These two chefs have equal salaries.\n";
for(unsigned int i = 0; i < members.size(); i++)
{
delete members;
}
return 0;
}