Question
C++ I am using a binary file to read in and update the salary for certain id's. I cannot seem to get the applyRaise function
C++
I am using a binary file to read in and update the salary for certain id's. I cannot seem to get the applyRaise function to correctly write over the position where the id was found. If you could help me fix the functions I would greatly appreciate it.
#include #include #include
using namespace std;
void printFile(fstream &fin); bool applyBonus(fstream &fin, int id, int raise);
int main(int argc, char * argv[]) { fstream fin;
int id = 0; int raise = 0; int length; int correct_id = 0; int n = 0; bool updated = false; if (argc != 4) { cout
fin.open(argv[1], ios::in|ios::out | ios::app | ios::binary) ;
if (!fin.is_open()) { cout
id = atoi((argv[2])); raise = atoi(argv[3]);
printFile(fin);
updated = applyBonus(fin, id, raise); cout
printFile(fin);
fin.close();
return 0; }
void printFile(fstream &fin) {
struct empData { int id; char firstName[20]; char lastName[40]; double salary; double bonus; }; empData test = { 0, "First Blank", "Last Blank", 0, 0 }; fin.clear(); fin.seekg(0, ios::beg); fin.read((char*)&test, sizeof(test));
while (!fin.eof()) { cout
bool applyBonus(fstream &fin, int id, int raise) { int position = 0; struct empData { int id; char firstName[20]; char lastName[40]; double salary; double bonus; }; empData test2 = { 0, "First Blank", "Last Blank", 0, 0 };
fin.clear(); fin.seekg(0, ios::beg); fin.read((char*)&test2, sizeof(empData));
while (!fin.eof() && test2.id != id) { fin.read((char*)&test2, sizeof(empData)); position += 1; }
test2.salary = test2.salary + raise;
fin.clear(); fin.seekp(position*sizeof(empData), ios::beg); fin.write((char*)&test2, sizeof(empData));
return true; }
I would like the applyRaise function to properly update the salary for the ID 56351 at the correct position and not to write it at the end of the file like it is doing now.
13889 Beverlee 19210 Filmore 23786 Giff 27417 Emmalee 32037 Claretta 37165 Rahal 42024 Ashlin 46836 Willa 51702 Kelley 56351 Renie 61138 Randa 65692 Genny 70195 Amalie 74728 Benson 78663 Cynthea 82807 Johnnie 86925 Ara 90386 Jere 94623 Emeline 99993 Cyndia 56351 Renie 56351 Renie 56351 Renie Agirre Heavy Larwood Dofty Riddali Steers Crinson Gilliatt Houlson Doe Espinha Pirazzi Balwin Maly Scholling Skelcher Beadon Delacoste Rudgerd Dance Doe Doe Doe Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: 78164.89 Bonus: 90597.50 Bonus: 76565.67 Bonus: 89918.66 Bonus: 75860.87 Bonus: 88589.03 Bonus: 89383.44 Bonus: 66728.80 Bonus: 73639.49 Bonus: 77109.93 Bonus: 75359.77 Bonus: 76931.36 Bonus: 87205.86 Bonus: 64654.75 Bonus: 70951.71 Bonus: 90752.44 Bonus: 63846.55 Bonus: 73906.01 Bonus: 77041.40 Bonus: 71660.70 Bonus: 79489.93 Bonus: 79489.93 Bonus: 79489.93 Bonus: 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 13889 Beverlee 19210 Filmore 23786 Giff 27417 Emmalee 32037 Claretta 37165 Rahal 42024 Ashlin 46836 Willa 51702 Kelley 56351 Renie 61138 Randa 65692 Genny 70195 Amalie 74728 Benson 78663 Cynthea 82807 Johnnie 86925 Ara 90386 Jere 94623 Emeline 99993 Cyndia 56351 Renie 56351 Renie 56351 Renie Agirre Heavy Larwood Dofty Riddali Steers Crinson Gilliatt Houlson Doe Espinha Pirazzi Balwin Maly Scholling Skelcher Beadon Delacoste Rudgerd Dance Doe Doe Doe Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: Salary: 78164.89 Bonus: 90597.50 Bonus: 76565.67 Bonus: 89918.66 Bonus: 75860.87 Bonus: 88589.03 Bonus: 89383.44 Bonus: 66728.80 Bonus: 73639.49 Bonus: 77109.93 Bonus: 75359.77 Bonus: 76931.36 Bonus: 87205.86 Bonus: 64654.75 Bonus: 70951.71 Bonus: 90752.44 Bonus: 63846.55 Bonus: 73906.01 Bonus: 77041.40 Bonus: 71660.70 Bonus: 79489.93 Bonus: 79489.93 Bonus: 79489.93 Bonus: 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started