Я написал этот код для класса, все работает отлично, за исключением полного цикла программы, спрашивая у пользователя, хотят ли они повторить программу. Кажется, он не распознает входные символы, чтобы закончить цикл. Любая помощь приветствуется.Помогло бы мне понять, почему этот цикл повторяется?
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define REGULAR_HOURS_LIMIT 40.0 // hours at which overtime begins
#define OVERTIME_HOURS_LIMIT 20.0 // Maximum overtime hours
#define OVERTIME_RATE 1.5 // overtime rate
#define TAX_RATE 0.30 // tax rate (30%)
#define PARKING_RATE 10.00 // parking deduction
#define MAXIMUM_PAY_RATE 99.99 // Maximum employee pay
int main()
{
string first_name, // employee's first name, input by user
last_name, // employee's last name, input by user
full_name; // employee's concatenated full name
double hours, // number of hours worked, input by user
regular_hours, // number of regular hours
overtime_hours, // number of overtime hours
hourly_rate, // hourly pay rate
gross_pay, // employee's gross pay
tax, // employee's tax amount
deductions, // monthly deductions
net_pay; // employee's net pay
char yesno; // Prompt for new input
do
{
// Input section
cout << "Enter employee's first name: ";
cin >> first_name;
cout << "Enter employee's last name: ";
cin >> last_name;
cout << "Enter number of hours worked: ";
cin >> hours;
while (hours < 0 || hours > (REGULAR_HOURS_LIMIT + OVERTIME_HOURS_LIMIT))
{
cout << "Hours must be between 0 and 60" << endl;
cout << "Enter number of hours worked: ";
cin >> hours;
}
cout << "Enter hourly pay rate: $";
cin >> hourly_rate;
while (hourly_rate < 0 || hourly_rate > MAXIMUM_PAY_RATE)
{
cout << "Pay rate must be between 0.00 and 99.99" << endl;
cout << "Enter hourly pay rate: $";
cin >> hourly_rate;
}
// Processing section
full_name = last_name + ", " + first_name;
if (hours <= REGULAR_HOURS_LIMIT)
{
regular_hours = hours;
overtime_hours = 0.0;
}
else
{
regular_hours = REGULAR_HOURS_LIMIT;
overtime_hours = hours - REGULAR_HOURS_LIMIT;
}
gross_pay = (regular_hours * hourly_rate) + (overtime_hours *
hourly_rate * OVERTIME_RATE);
tax = gross_pay * TAX_RATE;
deductions = PARKING_RATE;
net_pay = gross_pay - tax - deductions;
// Output section
cout << endl;
cout << "123456789##21.00##21.00##321.00##4321.00##321.00"
<< "##321.00##4321.00" << endl;
cout << " Reg. Ovt. Hourly Gross "
<< " Net " << endl;
cout << "Name Hours Hours Rate Pay Taxes "
<< " Deduct Pay " << endl;
cout << "==================== ===== ===== ====== ======= ======"
<< " ====== =======" << endl;
cout << left << setw(20) << full_name << " ";
cout << right << fixed << setprecision(2);
cout << setw(5) << regular_hours << " ";
cout << setw(5) << overtime_hours << " ";
cout << setw(6) << hourly_rate << " ";
cout << setw(7) << gross_pay << " ";
cout << setw(6) << tax << " ";
cout << setw(6) << deductions << " ";
cout << setw(7) << net_pay << endl << endl;
cout << "Process another employee (Y/N)?";
cin >> yesno;
if (yesno == 'y' || 'Y' || 'n' || 'N')
{
cout << endl;
}
else
{
do
{
cout << "Please type 'Y' for yes or 'N' for no";
cin >> yesno;
}
while (yesno != 'y' || 'Y' || 'n' || 'N');
}
}
while (yesno == 'y' || 'Y');
}
Сделал трюк. Благодарю. Я не понимал, что мне нужно пересчитать переменную с каждой проверкой, я буду помнить об этом в будущем. Еще раз спасибо! – Ruke