I am new to C++ and am having issues with a class assignment. The assignment is to take an input file and have the program compute the new wages based of the input file. The input file looks like this:
Miller Andrew 65789.87 .05
Green Sheila 75892.56 .06
Sethi Amit 74900.50 .061
The problem I am having is that I can get the program to work, but only on the first line. My math and code works perfect for the first line.
The book I am using does not explain loops or arrays yet and from what I seen that's what I have to do. I tried the while (!inFile.eof()) but that just hangs after the first line is done. Here is my code. Any help would be greatly appreciated.
It hangs because it isn't doing anything! Your file is not at the end but you are not doing anything except closing the inFile which is the next line (no brackets means the next line is what is run). So your program will forever keep closing inFile and since the end of file is never reached, it won't leave the while loop. Here is what you did with brackets
1 2 3 4 5
while (!inFile.eof())
{
inFile.close();
}
outFile.close();
Hopefully that helps explain the problem. Yout location of your while statement should be before you try to take anything from the file so that if it is empty, it doesn't cause your program problems.
I tried placing the while statement in another location and still nothing. I know absolutely nothing about while statements or loops and it is not covered until the next semester. I looked ahead and am I just using the wrong while statement?
I don't see how you can't do this problem without some kind of loop, unless you instructor just wants you to replicate code three times, which I find doubtful. Loops aren't difficult to understand; I recommend you read up on them.
But, you program needs to:
1 2 3 4 5 6 7 8
open the files
while infile ! eof
{
read in the data
calculate the updated pay
write out the new data
}
close the files
As William pointed out above, currently your while statement is only going to close the infile, probably over and over and over again, which is your hang.
Thanks mzimmers and William. It works now. My issue with everything was the brackets. Most of the programs we have wrote did not use brackets. Again, thanks to you both.