PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, October 30, 2022

[FIXED] Why is the last line of my input file running twice?

 October 30, 2022     c++, eof, file-io, while-loop     No comments   

Issue

INPUT FILE

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

ifstream inFile;
ofstream outFile;
string laastName;
string firstName;
double salary;
double percent;
double new Salary;
double increase;

inFile.open("Ch3_Ex6Data.txt");
outFile.open("Ch3_Ex6Output.dat");

while(!inFile.eof()) {

    inFile >> lastName;
    inFile >> firstName;
    inFile >> salary;
    inFile >> percent;

    percent /= 100;
    increase = salary * percent;
    newSalary = increase + salary;

    outFile << firstName << " " << lastName << " ";
    outFile << setprecision(2) << fixed << newSalary << endl;

}

inFile.close();
outFile.close();

return 0
}

Output File

Andrew Miller 69079.36
Sheila Green 82722.89
Amit Sethi 79469.43
Amit Sethi 74946.19

My question is why is the last line getting output twice and why is it different than the first one? I don't understand why the loop continues on. Is the end of file marker not hitting? I was able to hard code it by putting in an index variable and putting a second condition into the while loop by saying && less than index but i feel as if i shouldn't have to do that whatsoever.


Solution

The problem is that eof does not do what you think it does.

Imagine you are walking on a floor, tile after tile, picking up what's on the floor, putting it in your pockets and show up (print) your pockets content.

When you put you feet on the last tile the floor is not yet "ended" and your nose is still safe. You have not (yet) smashed the wall. You fill-up the pockets and print them.

eof,then, tells you when your nose is broken, not when the tile is the last.

So, you are on the last tile, check your nose, find it ok, and go one step forward. Your nose is now bleeding, nothing is there to peek up to put in our pockets, and your pockets still contain ... what they had before.

You print the content of your pocket (once again) and than check your nose. It's broken: you exit.

The idiomatic way to solve that problem is this one:

while(inFile >> lastName
             >> firstName
             >> salary
             >> percent)
{
   //all your computation here
}

I think you should understand by yourself why.



Answered By - Emilio Garavaglia
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing