You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++健身房会员年费循环计算逻辑错误排查修复

Ronda's Strikeforce Gym 会员费计算程序Bug修复

已知问题

  • 输出的首年会费未从1200美元基础值起始,提前计入了年度涨幅
  • 多次选择不同会员等级计算时,费用变量沿用上一次计算的最终值,未重置为基础会费,计算结果不符合预期

错误根因

  1. 费用累计变量初始化逻辑错误:用于存储逐年费用的变量仅在程序启动时初始化一次,每次开启新的计算流程前未重置为基础会费值
  2. 首年值计算逻辑错误:分支判断中提前计算了基础值加涨幅的结果,循环输出首年数据时又叠加了一次费用,导致首年值偏高
  3. 年涨幅计算逻辑不符合需求:原代码使用固定值(基础会费*费率)作为每年的涨幅增量,没有实现“逐年按当年会费比例上调”的复利计算规则

修复点说明

  • 每次用户选择有效会员等级(1-3)后,第一时间将当前会费变量重置为1200美元基础值,避免历史计算值干扰
  • 调整10年费用循环逻辑:每轮循环先输出当前年度的会费值,再按对应等级的费率计算下一年度的会费,保证第一年输出的是无涨幅的基础值
  • 修正年涨幅计算规则:每年度的上调费用 = 当年会费 * 对应等级费率,下一年度会费 = 当年会费 + 当年上调费用,符合逐年按比例上调的要求
  • 清理代码中定义后未使用的冗余变量,调整输出格式让年费表格对齐更规整

修复后完整代码

//Assignment 4-A.cpp: 按会员等级计算未来10年会员费
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

    // 菜单选项常量
    const int GOLD = 1,
              SILVER = 2,
              BRONZE = 3,
              QUIT = 4;
    // 各等级年涨幅
    const double rate_gold = 0.01,
                 rate_silver = 0.02,
                 rate_bronze = 0.04;
    // 年基础会费
    const double base_charge = 1200.00;

    int user_input;
    double current_rate;
    double current_fee;

    do
    {
        // 输出菜单
        cout << "\t Welcome to Ronda's Strikeforce Gym!!" << endl;
        cout << "x";
        for (int i = 0; i <= 50; i++)
        {
            cout << "-";
        }
        cout << "x" << endl;
        cout << "\t Membership Fee Calculation" << endl;
        cout << "1. Gold" << endl;
        cout << "2. Silver" << endl;
        cout << "3. Bronze" << endl;
        cout << "4. Quit" << endl << endl;
        cout << "Please enter your membership level (1-3  Enter 4 to Quit) ";
        cin >> user_input;
        cout << endl;

        // 输入合法性校验
        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 (user_input == QUIT)
        {
            break;
        }

        // 匹配对应等级费率,计算前重置会费为基础值
        current_fee = base_charge;
        switch (user_input)
        {
            case GOLD:
                current_rate = rate_gold;
                break;
            case SILVER:
                current_rate = rate_silver;
                break;
            case BRONZE:
                current_rate = rate_bronze;
                break;
        }

        // 输出10年会费表格
        cout << "Year\tMembership Fee" << endl;
        for (int year = 1; year <= 10; year++)
        {
            cout << year << "\t$" << current_fee << endl;
            // 计算下一年度会费
            current_fee = current_fee * (1 + current_rate);
        }
        cout << endl;

    } while (true);

    // 退出提示
    cout << "Thank you for using Ronda's Fee Calculator! \n" << endl;
    cout << "Press any key to continue . . .";

    return 0;
}

内容的提问来源于stack exchange,提问作者Harris

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.29 06:45:42