首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在一个循环中确定年度税收的计算?

如何在一个循环中确定年度税收的计算?
EN

Stack Overflow用户
提问于 2022-06-17 17:22:23
回答 1查看 45关注 0票数 -2

我需要写一个项目记录健身房的会员费。健身房每年收费1,200美元,基本费用,但根据您的会员等级,每年收取一个百分点的费率:金会员1%,银会员2%,铜牌会员4%

它将显示以下菜单:

欢迎来到隆达的Strikeforce健身房!!

会费计算器

  1. 黄金
  2. 银牌
  3. 青铜
  4. 退出

请输入您的会员级别(1-3输入4退出)>

所列菜单项的验证输入,如果输入1到3,程序将使用一个循环输出成员未来10年的预期费用。输出格式是一个具有相应年份和会员费的表。程序应该继续循环,直到用户准备退出为止。我应该将代码封装在一个并发循环中。如果输入4,则程序应该退出。任何其他输入都应该将错误消息显示为输出。我的输出不是从1200的基数开始,而是从百分比中的因素开始的。在程序的另一次迭代中,最后计算的值被用作起点。

这就是我所拥有的:

代码语言:javascript
复制
//Assignment 4-A.cpp: The program will calculate the membership fees over the next 10 years based on the membership level

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

int main() // Driver program

{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);
    //To hold menu choice
    const int GOLD = 1,
        SILVER = 2,
        BRONZE = 3,
        QUIT = 4;
    //To hold the percentage tax of the different gym choices
    const double tax_gold = .01,
        tax_silver = .02,
        tax_bronze = .04;
    //To hold the base charge per year
    const double base_charge = 1200.00;
    //variables
    double fees = 0, cost = base_charge, cost_1 = 1200.00;
    //To hold the users input
    int user_input;
    const int min_number = 1,
              max_number = 10;
    int i;

    do
    {

        cout << "\t Welcome to Ronda's Strikeforce Gym!!" << endl; //Title

        cout << "x";

        for (int i = 0; i <= 50; i++) //Dashed line, row
        {
            cout << ("-");
        }

        cout << "x";

        cout << endl;

        cout << "\t Membership Fee Calculation" << endl; //Title

        //Display of gym membership plan choice
        cout << "1. Gold" << endl;
        cout << "2. Silver" << endl;
        cout << "3. Bronze" << endl;
        cout << "4. Quit" << endl << "\n";

        cout << "Please enter your membership level (1-3  Enter 4 to Quit) ";
        cin >> user_input;

        cout << endl;

        // Validate user input(must input 1 through 4), loop if value is invalid
        while (user_input < 1 || user_input > 4)
        {
            cout << "Invalid entry! ";
            cout << "Please enter a selection from 1 through 4: ";
            cin >> user_input;

            cout << endl;

        }

        // If values 1 - 3 were entered perform calculations else if 4 was entered exit program with thank you message.
        // Perform calculation for specified membership for 10 years and output
        if (user_input == 4)
        {
            break;

        }

        if (user_input != QUIT)
        {
            //Start selection loop and calculation
            switch (user_input)
            {
            case 1:
                fees = base_charge * tax_gold;
                cost = base_charge + fees;
                break;
            case 2:
                fees = base_charge * tax_silver;
                cost = base_charge + fees;
                break;
            case 3:
                fees = base_charge * tax_bronze;
                cost = base_charge + fees;
                break;
            }

            //Display Yearly Cost
            for (int years = 1; years <= 10; ++years)

            {
                cout << "Year   " << years << "\t $" << (cost_1 += fees) << endl;

            }

            cout << "\n";

        }

    } while (user_input != QUIT);
        {

        cout << "Thank you for using Ronda's Fee Calculator! \n";

        }
    
    cout << endl;

    cout << "Press any key to continue . . .";

    return 0;
}

这就是我的电脑上显示的:在这里输入图像描述

这就是它应该看起来的样子:在这里输入图像描述

谢谢

EN

回答 1

Stack Overflow用户

发布于 2022-06-17 17:56:56

您的计算应该从基本费率(cost = 1200)开始。每年,打印当前税率,然后添加税率*税。(cost += cost * tax)。

下面是一些修改后的代码:

代码语言:javascript
复制
//To hold menu choice
const int GOLD = 1, SILVER = 2, BRONZE = 3, QUIT = 4;
//To hold the percentage tax of the different gym choices
const std::array<double, 3> tax_rates{.01, .02, .04};
//To hold the base charge per year
const double base_charge = 1200.00;

while (true) {
    cout << "\t Welcome to Ronda's Strikeforce Gym!!\nx";   //Title
    for (int i = 0; i <= 50; i++)   //Dashed line, row
    {
        cout << ("-");
    }
    cout << "x\n\t Membership Fee Calculation\n";   //Title
    //Display of gym membership plan choice
    cout << "1. Gold" << endl;
    cout << "2. Silver" << endl;
    cout << "3. Bronze" << endl;
    cout << "4. Quit" << endl << "\n";
    
    cout << "Please enter your membership level (1-3  Enter 4 to Quit) ";
    int user_input;
    cin >> user_input;
    cout << endl;
    
    // Validate user input(must input 1 through 4), loop if value is invalid
    while (user_input < 1 || user_input > 4)
    {
        cout << "Invalid entry! ";
        cout << "Please enter a selection from 1 through 4: ";
        cin >> user_input;
        cout << endl;
    }
    
    // If values 1 - 3 were entered perform calculations else if 4 was entered exit program with thank you message.
    // Perform calculation for specified membership for 10 years and output
    if (user_input == QUIT)
    {
        break;
    }
    
    //Display Yearly Cost
    double cost = base_charge;
    for (int years = 1; years <= 10; ++years)
    {
        cout << "Year   " << years << "\t $" << cost << endl;
        cost += cost * tax_rates[user_input-1];
    }
    cout << "\n";
}

cout << "Thank you for using Ronda's Fee Calculator!\n\nPress any key to continue . . .";

附带注意:建议您不要对货币使用浮点值。FP可以引入舍入误差。您可以在货币的基本面值中使用整数。美元是美分(便士)。只是为了将来的项目要注意的事情。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72662830

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档