0

the text file named "Ch3_Ex5Data.txt" contains this

Miller Andrew 65789.87 5
Green Sheila 75892.56 6
Sethi Amit 74900.50 6.1

my code is suppose to rearrange the contents of the text file. for example "Miller Andrew" is suppose to be "Andrew Miller" I should also include their salary, salary increase, and their updated salary. Everything is going smooth except for the fact that the last line of the file duplicates its output. What can I do?

Here is the C++ code:

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

int main() 
{
  ifstream inFile;
  ofstream outFile;
  inFile.open("Ch3_Ex5Data.txt");
  outFile.open("Ch3_Ex5Output.dat");
  string firstName, lastName;
  double updateSalary, salary, salaryIncrease;
  outFile<<fixed<<setprecision(2);

  while(!inFile.eof())
  {
      inFile >> lastName >> firstName >> salary >> salaryIncrease;
      updateSalary = salary + (salary*(salaryIncrease/100));
      outFile << "Name: " << firstName <<" "<< lastName << endl 
            << "Salary Increase: "<< salaryIncrease <<"%" << endl
            <<"Salary: $"<< salary << endl
            << "Updated Salary: $" << updateSalary << endl << endl;
  }
  return 0;
}

The output file shows this

Name: Andrew Miller
Salary Increase: 5.00%
Salary: $65789.87
Updated Salary: $69079.36

Name: Sheila Green
Salary Increase: 6.00%
Salary: $75892.56
Updated Salary: $80446.11

Name: Amit Sethi
Salary Increase: 6.10%
Salary: $74900.50
Updated Salary: $79469.43

Name: Amit Sethi
Salary Increase: 6.10%
Salary: $74900.50
Updated Salary: $79469.43

how do I stop the last output from duplicating?

Kino
  • 1
  • 1
  • This doesn't address the question, but get in the habit of initializing objects with meaningful values rather than default-initializing them and immediately overwriting the default values. In this case, that means changing `ifstream inFile; inFile.open("Ch3_Ex5Data.txt");` to `ifstream inFile("Ch3_Ex5Data.txt")`;` and similarly for `outFile`. – Pete Becker Jun 21 '21 at 13:20

0 Answers0