正在尝试循环回到开始以再次运行我的程序
Attempting to loop back to the beginning to run my program again
我目前正在编写一段代码,在我的主函数中,我只是在调用其他函数。我正试图重新循环到开始,这样用户就可以再次运行程序了。主函数只是调用函数,所以我的问题是,我知道不可能回到主函数,但有可能创建一个函数来再次循环所有其他函数吗?我觉得好像我尝试了一切,并继续获得无限的循环。我附上了我的代码
要压缩代码,请理解所有变量/类都已声明
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;
}
如您所知,在main中,您只需要有一个while循环,其中包含要在其中重复的代码:
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();
}
相关文章:
- 运行程序时出现问题
- 分段错误当我试图运行程序时出错
- C++ 每次运行程序时我都会"nan"输出的问题
- 通过 g++ 运行程序时没有这样的文件或目录
- 重新运行程序和字符串流?
- 无法在 VS Code 上使用代码运行程序运行C++文件
- 使用有限的 RAM 运行 c++ 程序
- 继续运行程序而无需任何干预,直到要求退出为止
- 如何在每次运行程序时写入文件的下一行?
- 控制台在运行C ++程序后立即关闭(无需调试)
- 运行程序时找不到共享对象库,但在编译过程中链接了它
- 远程运行程序
- 在 Heroku 上运行 C++ 程序
- 如何在Powershell中运行C++程序,就像CMD一样?
- 为什么与Java和Python相比,使用Cmake运行C++程序每次都需要这么长时间?
- C++随机数生成器通常在 Visual Studio 中运行程序时在 2 次后停止生成数字
- 崇高文本 3 - 在终端中编译并运行 C++ 程序(路径包含空格)
- 我收到阻止我运行程序的警告,但不确定如何解决
- C++ Visual Studio 无法完全运行程序
- 在SIGABRT c ++信号之后继续运行程序