I emphasize using code tags for the simple reason it makes reading, and commenting on, code MUCH easier.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
#include<iostream> //Header File for console I/O
#include<iomanip>
#include <fstream>
#include<string>
using namespace std;
const double MIN_WAGE = 15;
const double NORM_HOURS = 40;
const double fed_tax = 0.15;
const double state_tax = 0.05;
const double soc_tax = 0.075;
const double med_tax = 0.015;
// begin min Function Definition
int main()
{
// ifstream inputFile;
ifstream myfile("payrolldata.txt", ios::in);
string employeeID, payrolldata, line;
double hoursWorked, totalPay, payRate;
char payCode, again;
float percentage, finalTotalPay;
// Display Identtification on screen
cout << "Assignment 4" << endl;
cout << "Programmed by Rodger Coleman" << endl;
if (myfile.is_open());
{
getline(myfile, line);
cout << line << endl;
myfile.close();
getline(myfile, line);
cout << line << endl;
myfile.close();
}
do
{
cout << "Enter Employees" << endl;
// cin >> employeeID >> payCode >> hoursWorked;
switch (payCode)
{
case 'm':
case 'M': payRate = MIN_WAGE;
break;
case 'o': case 'O': payRate = MIN_WAGE + 3.00;
break;
case 't':
case 'T': payRate = MIN_WAGE + 6.00;
break;
case 'n':
case 'N':
default: cout << "you have entered the incorrect paycode" << endl;
break;
}
if (payRate)
{
double totalPay(hoursWorked * payRate);
if (hoursWorked > NORM_HOURS)
totalPay += payRate * (hoursWorked - NORM_HOURS) * 1.5;
cout << setprecision(2) << fixed;
cout << "Pay rate: $ " << setw(2) << payRate << " Pay Amount: $ " << setw(2) << totalPay << '\n';
if (percentage = totalPay * fed_tax);
cout << "federal tax rate " << percentage << endl;
if (percentage = totalPay * state_tax);
cout << "State tax rate " << percentage << endl;
if (percentage = totalPay * soc_tax);
cout << "Social Security tax rate " << percentage << endl;
if (percentage = totalPay * med_tax);
cout << "Medicare tax rate " << percentage << endl;
if (finalTotalPay = totalPay - (totalPay * 0.15 + totalPay * 0.05 + totalPay * 0.075 + totalPay * 0.015));
cout << "Total pay " << finalTotalPay << endl;
}
cout << "add another employee? (Y/N): ";
cin >> again;
} while (again == 'Y' || again == 'y');
return 0;
}
|
Lots of warnings from Visual Studio with your code.
You have "empty control statements" on numerous lines. You terminate your if statements with ; so any following block of code is ALWAYS run. Lines 31, 77, 79, 81, 83 & 85.
You use uninitialized variables, payCode, payRate & hoursWorked, so you potentially have garbage data.
totalPay instantiated on line 22 is unreferenced (not used) when you instantiate it again on line 72.
The biggest problem is reading data from your file. Because you prematurely terminate your "is the file open" check you are attempting to read data whether the file is open or not.
Plus, as pointed out by seeplus, you read a line at line 34 and then close the file. Your 2nd file read at line 39 now tries to read data from a closed file!
Your code will compile, but the warnings you should be seeing demonstrate there are problems. And one rather nasty issue with your file I/O.