如何在另一个循环中输入循环?

时间:2013-04-22 13:25:46

标签: c++ visual-studio-2010

#include<iostream>
#include<iomanip>
using namespace std;


 int main()
{
int employeeNum = 0;            int totalEmployees  = 0;
double hourlyRate   = 0.0;          double totalhoursWork   = 0.0;
double hoursWork    = 0.0;          double totalnetPay      = 0.0;
double grossPay     = 0.0;          double averagehoursWork = 0.0;
double netPay       = 0.0;   double totalwithHoldings = 0.0;
double withHoldings = 0.0;          double overTime         = 0.0;
                                    int x                   = 0;




    while(employeeNum!= 9999)
     {
        cout <<" Enter Employee Number  (9999 to Stop):";
        cin >> employeeNum;
        if(employeeNum ==9999)
        break; 
        cout <<"Enter hourly rate:";
        cin >> hourlyRate;
        cout <<"Enter hours worked:";
        cin >> hoursWork;
        cout <<"Employee Number:"<<employeeNum << endl;
        if(hoursWork <= 40)
        {
            grossPay= hoursWork * hourlyRate;
    cout <<" Gross Weekly Pay=" << fixed <<setprecision(2)<< grossPay << endl;
        }
        else if (hoursWork > 40)
        {
            overTime = hoursWork-40;
            grossPay = (overTime * 1.5 + 40)* hourlyRate;


        cout <<" Gross Weekly Pay="<< fixed <<setprecision(2)grossPay << endl;

        }
        if( grossPay > 1000.00)
        {
            withHoldings= grossPay*0.28;

        }
        else  if( grossPay <= 1000.00)
        {
            withHoldings= grossPay*0.21;

        }

        netPay= grossPay-withHoldings;
        cout <<" Net Weekly Pay="<<fixed << setprecision(2) << netPay << endl;

        totalhoursWork+=hoursWork;
        totalnetPay+=netPay;
        totalwithHoldings+= withHoldings;
        averagehoursWork= totalhoursWork/totalEmployees;
        totalEmployees++;

            }       
    averagehoursWork= totalhoursWork/totalEmployees;
     for (int x = 1; x < 44; x = x + 1) 
         cout <<"*";
         cout << endl;



    cout <<"Total Employees Entered=" << totalEmployees << endl;
    cout <<" Total Hours Worked="<< fixed << setprecision(2) << totalhoursWork << endl;
    cout <<" Average Hours Worked="<< fixed << setprecision(2) << averagehoursWork << endl;
    cout <<" Total Net Pay="<<fixed << setprecision(2) << totalnetPay << endl;
    cout <<" TotalwithHoldings=" << fixed << setprecision(2)<< totalwithHoldings << endl;

     for (int x = 1; x < 44; x = x + 1) 
         cout <<"*";
         cout << endl;


system("pause");
return 0;
      }

每小时费率必须大于$ 7.25且小于$ 100.00。工作小时数必须大于0且小于120.如果用户输入无效数据显示和相应的错误消息并要求用户重新输入。 我应该对这部分使用什么声明?我应该把它放在哪里?

2 个答案:

答案 0 :(得分:0)

你可以使用Cin和Cout进行输入和输出,只需使用do while循环

 int employeeNum = 0;

            do
            {
                Console.WriteLine("enter Employee /Number 9999 to STOP");
                employeeNum = int.Parse(Console.ReadLine());

                if (employeeNum == 9999)
                    break;

                Console.WriteLine("enter hourly rate ");
                double hourRate = Double.Parse(Console.ReadLine());




            } while (employeeNum != 9999);

答案 1 :(得分:0)

您可以这样做:

cout << "Enter hourly rate:";
cin >> hourlyRate;
while (hourlyRate <= 7.25 || hourlyRate >= 100) {
    cout << endl << "Reenter hourly rate (must be in (7.25 - 100))";
    cin >> hourlyRate;
}

但是stackoverflow不适合别人做你的作业。