As to IDE, I'm using MS Visual C++ Express. I also have Borland's Turbo C++ but couldn't figure out how to use it yet (I'm just developing 1 .cpp file at a time, and Borland, at least as far as I can tell at my currently primitive level, seems almost require that you know how to navigate around a project containing multiple .cpp code files, etc. I did notice that Borland fills in more than MS).
The problem may be the result of getting over-ambitious on exercises where I've tried to go beyond what "the book" (Gaddis, up to and including chap. 5) clearly wants on the exercise. For example, I'm imposing more strict validation than he presumably wants at this point (hence a previous question here), and I'm asking the user to confirm entries at certain strategic points in the program.
And... well, if you guys want to see some code, here goes. I won't claim that it's optimal efficiency, even restricting oneself to the portions of the language I currently know, but it does produce pretty much the desired result, and I can't see how to make the somewhat complex loop structure significantly more efficient with the stuff I know. But maybe you guys will see something I don't.
Here's the task (Gaddis, chap. 5, prog. challenge 15): Display a weekly payroll report given data for employee number, gross pay, state tax, federal tax, and FICA with various validations for gross pay and taxes (should be obvious from my code). Also, entering employee number 0 should act as a sentinel to terminate data entry and display the payroll report.
Here's my solution:
Code:
// payroll report
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int Num;
double Gross, TotGross = 0, State, TotState = 0, Fed, TotFed = 0,
FICA, TotFICA = 0;
bool Val = 0;
char Conf = 'n';
do
{
// get employee number
do
{
cout << "Enter employee number or \"0\" to end list: ";
cin >> Num;
// validate input
while (cin.fail() || (Num < 0))
{
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!\n\n"
<< "Enter employee number or \"0\" to end list: ";
cin >> Num;
}
// exit loop on 0 entry for employee number
if (Num == 0)
{
cout << "\nAre you finished entering employee data (Y/N)? ";
cin >> Conf;
}
if ((Num == 0) && ((Conf == 'Y') || (Conf == 'y')))
Val = 1;
else if (Num == 0)
cout << endl; // mistaken 0 entry
else
{
// confirm employee number
cout << "Employee number is " << Num
<< ". \nConfirm (Y/N): ";
cin >> Conf;
cout << endl;
// get info only when confirmed; otherwise rerun employee num entry loop
if ((Conf == 'Y') || (Conf == 'y'))
{
do
{
// gross pay
cout << "Employee " << Num << " gross pay: ";
cin >> Gross;
// validate
while (cin.fail() || (Gross < 0))
{
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!\n\n"
<< "Employee " << Num << " gross pay: ";
cin >> Gross;
}
// state tax
cout << "\nEmployee " << Num << " state tax: ";
cin >> State;
// validate
while (cin.fail() || (State < 0) || (State > Gross))
{
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!\n";
if (State < 0)
cout << "State tax cannot be negative.\n";
if (State > Gross)
cout << "State tax cannot exceed gross wages.\n";
cout << "Employee " << Num << " state tax: ";
cin >> State;
}
// federal tax
cout << "\nEmployee " << Num << " federal tax: ";
cin >> Fed;
// validate
while (cin.fail() || (Fed < 0) || (Fed > Gross))
{
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!\n";
if (Fed < 0)
cout << "Federal tax cannot be negative.\n";
if (Fed > Gross)
cout << "Federal tax cannot exceed gross wages.\n";
cout << "Employee " << Num << " federal tax: ";
cin >> Fed;
}
// FICA
cout << "\nEmployee " << Num << " FICA: ";
cin >> FICA;
// validate
while (cin.fail() || (FICA < 0) || (FICA > Gross))
{
cin.clear();
cin.ignore(100, '\n');
cout << "Invalid input!\n";
if (FICA < 0)
cout << "FICA cannot be negative.\n";
if (FICA > Gross)
cout << "FICA cannot exceed gross wages.\n";
cout << "Employee " << Num << " FICA: ";
cin >> FICA;
}
// validate
if (State + Fed + FICA > Gross)
{
cout << "Total taxes cannot exceed gross wages.\n"
<< "Re-enter data for employee " << Num << ".\n";
Val = 0;
}
// confirm
else
{
cout << setprecision(2) << fixed
<< "Wages and taxes for employee " << Num << ":\n"
<< " Gross wages: $" << setw(10) << right << Gross << endl
<< " State tax: $" << setw(10) << right << State << endl
<< " Federal tax: $" << setw(10) << right << Fed << endl
<< " FICA: $" << setw(10) << right << FICA << endl
<< "Confirm (Y/N): ";
cin >> Conf;
if ((Conf == 'Y') || (Conf == 'y'))
{
Val = 1;
cout << endl;
}
else
{
Val = 0;
cout << "Re-enter data for employee " << Num << ".\n";
}
}
}
while (Val == 0); // ends do loop for specific data after confirmation of employee number
// update accumulators
TotGross += Gross;
TotState += State;
TotFed += Fed;
TotFICA += FICA;
Val = 0;
}
/* ends if statement that asks for the numbers for a specific employee
after confirmation of correct employee number
*/
} // closes the else for the case that employee num is a real employee
}
while (Val == 0);
}
while (Num != 0);
cout << "\nPayroll Report\n\n"
<< setprecision(2) << fixed
<< "Gross pay total: $" << setw(12) << right << TotGross << endl
<< "State tax total: $" << setw(12) << TotState << endl
<< "Federal tax total: $" << setw(12) << TotFed << endl
<< "FICA total: $" << setw(12) << TotFICA << endl << endl;
return 0;
}