该程序非常简单。提示员工的工资单,工作时间,付费代码等。
// Project Name: Lab Assignment 3
// Programmer Name: Trenton Covieo
// Date Written: 9/18/2017
// Description: This program will calculate employee pay based on paycode and hours provided by the employee
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
// Begin main Function Definition
int main()
{
//intialization
char paycode;
string employee_number;
int hours;
const double min_wage = 8.90;
double pay_rate;
double calc_pay;
char again;
int employee_num = 0;
//Diplay identifcation on screen
cout << "Lab Assignment 3"<< endl;
cout << "programmed by Trenton Covieo" << endl << endl;
//Prompts for exmployee number
do{
cout << "Enter employee number: ";
cin >> employee_number;
// prompts for/ determines paycode and payrate
cout <<"Enter Paycode: ";
cin >> paycode;
switch (paycode)
{
case 'M':
pay_rate = min_wage;
break;
case 'O':
pay_rate = min_wage + 1;
break;
case 'T':
pay_rate = min_wage + 2;
break;
case 'm':
pay_rate = min_wage;
break;
case 'o':
pay_rate = min_wage + 1;
break;
case 't':
pay_rate = min_wage + 2;
break;
// An incorrect code was entered
default:
pay_rate = 0;
cout << "You did not enter a valid paycode!" << endl;
break;
}
// Prompts for hours worked
cout << "Enter hours worked: ";
cin >> hours;
//calculates pay based upon hours entered including overtime
if (hours <=40)
calc_pay = pay_rate * hours;
else if (hours > 40)
calc_pay = (pay_rate* 40) + pay_rate * (hours-40) * 1.5;
//outputs information entered
cout << "Employee#: " << employee_number << endl;
cout << "Pay Rate $: " << fixed
<< setprecision(2)<< pay_rate << endl;
cout << "Hours Worked: "
<< fixed << setprecision(2) << hours << endl;
cout << "Total Pay: " << fixed << setprecision(2) << calc_pay
<< endl;
// prompt for another employeee
cout<< "Do you want to enter another employee? (Y/N) ";
cin >> again;
}while (again =='Y' || again =='y') ;
/* This is the part that I can't seem to figure out.
I have anoter Do-While loop that says
whenever the paycode = 'm' it will add 1 to employee_num.
It works fine when i have one condition,
but if i try two (both 'M' and 'm') the cout won't work. */
do
{ employee_num++;
}while (paycode == 'm'|| paycode =='M') ;
cout <<"Number of Employees Processed: " << employee_num << endl;
答案 0 :(得分:0)
雅,因为do while
循环处于无限循环
使用if语句而不是
时执行if (paycode == 'm'|| paycode =='M') {
employee_num++;
}
cout <<"Number of Employees Processed: " << employee_num << endl;
答案 1 :(得分:0)
您的问题是由于您在第二个循环中未更改paycode
而导致的无限循环。一旦检测到'm'
或'M'
,它就会一直持续。
简单地说:
do
{ employee_num++;
}while (paycode == 'm'|| paycode =='M') ;
到
if (paycode == 'm'|| paycode =='M')
{ employee_num++;
}
将解决这个直接问题。
但是,我得到的印象是,您打算使用付费代码“mM”来计算员工数量。通过在前一个循环中进行计数,可以更优雅地实现这一点。
do{
/* ... */
switch (paycode)
{
case 'M':
case 'm':
employee_num++;
pay_rate = min_wage;
break;
/* ... combining O&o, T&t */
default:
/* ... */
}
// prompt for another employeee
cout<< "Do you want to enter another employee? (Y/N) ";
cin >> again;
} while (again =='Y' || again =='y');
cout <<"Number of Employees Processed: " << employee_num << endl;
/* ... */
或者,如果您想计算所有员工,
如变量的命名和循环后的输出所示,
在第一个循环中无条件地执行。
do{
/* ... */
switch (paycode)
{
/* ... */
}
employee_num++;
// prompt for another employeee
cout<< "Do you want to enter another employee? (Y/N) ";
cin >> again;
} while (again =='Y' || again =='y');
cout <<"Number of Employees Processed: " << employee_num << endl;
/* ... */