尝试循环回到开始以再次运行程序

时间:2019-11-15 15:46:17

标签: c++ loops nested

我目前正在编写一个代码,其中在我的主要功能中,我只是在调用其他功能。我试图重新循环回到开头,以便用户可以再次运行该程序。主要功能只是调用功能,所以我的问题是我知道不可能返回到主要功能,但是有可能创建一个将再次循环所有其他功能的功能吗?我感觉好像我尝试了一切并继续获得无限循环。我附上了代码。
要压缩代码,请理解所有变量/类都已声明

void instructions();
void full_outputs(string, double, double, double);

int main()
{
    instructions();

    employee_num = employee_ID();
    //cout << employee_num << " This is the employee ID."<<endl;
    base_salary = baseSalary();
    //cout << base_salary << " This is the employee's base salary." <<endl;
    per_commission = percentage_commission();
    //cout << per_commission << " This is the employee's percentage commission." << endl;
    base_commission = base_and_com(base_salary, per_commission);
    cout<< base_commission << "This is the total base salary with comission" << endl;
    gross_pay = grossPay(base_commission);
    //cout << gross_pay << "This is the gross pay"<<endl;
    state_tax_hold = stateTax_hold(gross_pay);
    //cout<< state_tax_hold << "This is the state tax hold on the amount" <<endl;
    fica_total = ficaTotal(gross_pay);
    //cout << fica_total << " This is the fica hold on the amount" <<endl;
    fed_tax = fedTax(gross_pay);
    //cout << fed_tax << " THis is the federal tax hold on the amount" << endl;
    total_tax_hold = withholding_total(state_tax_hold, fica_total, fed_tax);
    //cout << total_tax_hold << " This is the total tax withholding" << endl;
    net_pay = netPay(total_tax_hold, gross_pay);
    //cout << net_pay << " This is the total net pay" << endl;



    full_outputs(employee_num, gross_pay, total_tax_hold, net_pay);




    return 0;   
}

void instructions()
{
    cout << " This program will process sales employee's base salary \n";
    cout << " and their percentage commission. \n";
    cout << " You will be prompted to enter the employee's ID, base salary \n";
    cout << " and percentage commission. \n";
    cout << " \n";
    cout << " The program will terminate if unspecified characters are used. \n";

}



string employee_ID()
{
        string employee_num;

        cout << " Please enter the employees eight digit ID number" << endl;
        cin >> employee_num;

    return employee_num;
}



double baseSalary()
{
        double base_salary;

        cout << " Please enter the employees base salary " << endl;
        cin >> base_salary;

    return base_salary;

}


float percentage_commission()
{
        float per_commission;

        cout << " Please enter the employees percentage commission."<< endl;
        cout << " Please do not enter the percent symbol." << endl;
        cout << " Percentage commission is between 0.05% - 10%" << endl;
        cin >> per_commission;
        while ((per_commission < 0.05)||(per_commission > 10))
        {

            cout << "The commission rate is not between 0.05% and 10%" << endl;
            cout << "Please try again " << endl;
            cin >> per_commission;

        }
        per_commission = per_commission / PERCENT_TO_DECIMAL;

    return per_commission;

}

double base_and_com(double base_salary, float per_commission)
{
    double base_commission;
    double total;
    total = base_salary*per_commission;
    base_commission = total + base_salary;


    return base_commission;

}



double grossPay(double base_commission)
{
        double gross_pay;

        gross_pay = base_commission;

        cout << fixed << showpoint << setprecision(2);

    return gross_pay;
}


double stateTax_hold(double gross_pay)
{
        double state_tax_hold;
        state_tax_hold= gross_pay*STATE_TAX;

    return state_tax_hold;
}


double ficaTotal (double gross_pay)
{
        double fica_total;
        fica_total = gross_pay* FICA; 

    return fica_total;  
}


double fedTax (double gross_pay)
{
        double fed_tax;
        if (gross_pay <= 500)
        {
            fed_tax = gross_pay * FEDERAL_TAX_UNDER;
        }
        else
        {
            fed_tax = gross_pay * FEDERAL_TAX_OVER;
        }
    return fed_tax;
}


double withholding_total(double fed_tax, double fica_total, double state_tax_hold )
{
        double tax_withholding_total;

        tax_withholding_total = fed_tax + fica_total + state_tax_hold;
        cout << fixed << showpoint << setprecision(2);

    return tax_withholding_total;


}



double netPay(double total_tax_hold, double gross_pay)
{
        double net_pay;

        net_pay = (gross_pay - total_tax_hold);
        cout << fixed << showpoint << setprecision(2);

    return net_pay;

}

void full_outputs(string employee_num, double gross_pay, double total_tax_hold, double net_pay)
{
    cout << " The employee ID : " << right << employee_num << endl; 
    cout << " The gross pay is: " << right << gross_pay << endl;
    cout << " The total tax withholding amount is : " << right << total_tax_hold << endl; 
    cout << " The net pay is: " << right << net_pay << endl; 

}

1 个答案:

答案 0 :(得分:3)

您知道,主要来说,您可以在其中循环一个要重复的代码:

int main()
{
    // this will loop forever, until you explicitly break or return
    while (true) {
        // your code here
    }
}

...但是由于您受到人为限制的限制,即只能在main中进行函数调用...

void run()
{
    // this will loop forever, until you explicitly break or return
    while (true) {
        // your code here
    }
}

int main()
{
    run();
}