Help with an infinite loop in my C++ program?

Thursday, March 10, 2011

First, let me say I am very new at this... So, I have spent all day working on this and I think I have tried to fix it so many times that I have completely destroyed it now and now I am not sure if it's able to be saved... Anyway, the assignment is to calculate the gross salary, taxes, retirement fund contributions, and net salary for employees of a company and output the results into two output files. One in table format like so: **When I tried to do this originally, it undid all the indentations, so I put in ... to try and keep it somewhat formatted so it might be easier to read. The window on this Yahoo! thing is not quite wide enough, but I think you'll be able to get the idea.** Name.................Gross Salary...Benefits...Contributions.......… Salary Joe Smith............ $6850.00.......$62.50.… Norm Johnson.......Bad Data.......Invalid contribution not qualified John Jordan.........$1050.00...... $0.00.....… Nancy K. Brown.....Bad Data Invalid contribution 6%, Hours invalid And the other one in a detailed format like so: Name:................Joe Smith Gross Salary:........$6850.00 Medical Benefits:..........$62.50 Retirement Contribution: 5% ......Company:......$343.75 ......Employee:......$343.75 ......Total:.................$687.50 Taxes Owed:.........$168.50 Net Salary:.................$6275.25 There are a few more stipulations, but here's the code I have so far (my deepest apologies for how messed up it is right now....when you get to thinking in a circle, you can really mess things up): #include #include #include #include using namespace std; int main () { ifstream fin; ofstream fout1, fout2; string infile, outfile_payroll, outfile_report; string name, error1, error2, error3, error4, error5, error6; int level, i, benefit_code; long int ID; float payrate, percent, hours, grosspay, netpay; float taxes, retirement, benefit; float total_grosspay, total_benefits, total_retirement, total_taxes, total_netpay; const float payrate1 = 15.00; const float payrate2 = 25.00; const float payrate3 = 72.00; const float payrate4 = 125.00; cout<--------------------
I have not use getline myself, but that is the way I have seen it used. The do while thing is wrongly done. What you should do is something like this. Make a bool, not a number, to say if the loop is to continue. do - get the input file name. - see if it opens. - if not, - - print error message to user. - - set the bool to false. - else - - set the bool to true. - end if while bool is false (or not bool). Do the same loop structure for each filename you need to get. The way you have it, you are getting the in file. If this is ok, but one of the out files is not, then you are looping around to get the in file again. This maybe causing your infinite loop. There is one problem with doing it this way though. If the user types in the same name of the file, it will continually loop around as the file cannot open.
Source

0 comments: