【问题标题】:Can I get any suggestions on how to fix this C++ program on calculating data plans?我可以就如何修复此 C++ 程序计算数据计划获得任何建议吗?
【发布时间】:2018-04-04 01:25:26
【问题描述】:

我是一个编码新手,对如何使这个程序工作感到困惑和困惑。

显示一个菜单以显示不同的数据计划。计划 1 每月 39.99 美元,包括 450 分钟。额外分钟数为每分钟 0.45 美元。计划 2 每月 59.99 美元,提供 900 分钟。额外分钟数为每分钟 0.40 美元。计划 3 每月 69.99 美元,分钟数不限。

• 客户的帐号 • 客户的计划编号 • 客户使用的总分钟数 • 到期总金额(格式化)

输入验证:确保用户只选择包 1、2 或 3。

我用三种不同的搜索查询变体搜索了 stackoverflow:“数据计划”、“手机数据计划”、“如何计算数据计划”,我确实找到了这篇文章 Comparing Cell Phone Plans in C++,它是一个有点相似的程序,但不是足够相似。我也用谷歌搜索,发现了一些类似的程序,但它们更有帮助,但太复杂了,对我的帮助不够。

我处于停顿状态,不知道如何使用计划一和计划二使用的分钟数来计算总费用,这些分钟数超过了这两个计划的分配分钟数。

这是我目前所拥有的:

int main()
{
    // Declare and initialize double constant variables
    const double planOne = 39.99,
                 planTwo = 59.99,
                 planThree = 69.99,
                 planOneExtMins = 0.45,
                 planTwoExtMins = 0.40;

    // Declare integer variables
    int planThreeUnlMins,
        custPlanNum,
        numMinsUsed;

    // Declare double variables
    double billOne,
           billTwo,
           billThree;

    // Declare unsigned variable
    unsigned int planChoice;

    // Initialize string variable
    string custAcctNum;

    // Ask for customer account number
    cout << "\n\nPlease enter your customer account number in all capital 
    letters and numbers and then press enter when done: ";
    cin >> custAcctNum;

    // Welcome statement
    cout << "Welcome to the Python Services Mobile Provider program that 
    will help you determine your monthly bill\n\n";

    // Display menu with the different data plans offered
    cout << "Data Plans Menu";
    cout << "\n\n\t1. Plan One is $39.99 per month with 450 included 
    minutes. Additional minutes are 0.45 per minute.";
    cout << "\n\t2. Plan Two is $59.99 per month with 900 provided minutes. 
    Additional minutes are $0.40 per minute.";
    cout << "\n\t3. Plan Three is $69.99 per month with unlimited minutes." 
    << endl;
    cout << "Please enter your choice of a data plan: ";

    // Validate if the choice of the user is a number between one and four
    int planChoice = 0;
    cin >> planChoice;

    if(!( planChoice == 3 )) {
            // Display restart program statement and end program
            cout << "Please restart the program and enter a number between 
            one and four" << endl;
            cout << "Program ending";
            return 0;
            }

    // Ask user for the number of minutes used
    cout << "\n\nPlease enter the number of minutes used: ";
    cin >> numMinsUsed;

    if( planChoice == 1 ) {
    // What code can I use to use the number of minutes used to  
    calculate the total cost of a particular data plan?
    }

    // Set the numeric output formatting
    cout << fixed << showpoint << setprecision(2);

    switch (planChoice) {

    case 1: cout << "Please enter the amount of minutes used: " << endl;
    cin >> numMinsUsed;
    break;

    case 2: cout << "Please enter the amount of minutes used: " << endl;
    cin >> numMinsUsed;
    break;

    case 3: cout << "Please enter the amount of minutes used: " << endl;
    break;

    default: cout << "Please enter a valid plan number from above" << endl;
    cin >> numMinsUsed;

    }

    if( planChoice == 1 ) {
        billOne = planOne + ( .45 * numMinsUsed );
    }
    else if( planChoice == 2 ) {
        billTwo = planTwo + ( .40 * numMinsUsed );
    }

    else if (planChoice == 3 )  {
        billThree = planThree;
    }

    return 0;
}

【问题讨论】:

  • 作为对您问题的改进,我建议您摆脱所有不相关的东西(代码和文本)。由于您只是在询问如何计算某些东西,因此请只关注它。有一些测试输入,以及你期望它们的值是什么以及你得到的值。
  • 您的代码还有其他问题,例如 1 和 2 永远无法作为初始输入,但让我们忽略它并进行一些数学运算。计划 1,已使用 1000 分钟。 39.99 美元 + (1000-450) * 0.45 = ?这有意义吗?
  • @Tas 谢谢你的建议。我试图摆脱大部分不相关的代码和文本。我也必须尝试测试输入。
  • @RetiredNinja 确实有道理。我只是不知道如何将其转换为代码。

标签: c++ logic


【解决方案1】:

这是一个示例,说明如何根据使用的分钟数计算每个计划的成本。

我使用包含 0 分钟(每分钟 0 分钟)来表示无限分钟,因为它使计算变得简单,并使用函数返回这些分钟的显示值,因此 0 =“无限”。

#include <iostream>
#include <string>

double calculateCost(double planCost, 
    double minuteCost, 
    int minutesIncluded, 
    int minutesUsed)
{
    double cost = planCost;
    int overage = minutesUsed - minutesIncluded;
    if (overage > 0)
    {
        cost += minuteCost * overage;
    }
    return cost;
}

std::string includedMinutesString(int numMinutes)
{
    if (numMinutes > 0)
    {
        return std::to_string(numMinutes);
    }
    return "unlimited";
}

int main()
{
    {
        double planCost = 39.99;
        double minuteCost = 0.45;
        int minutesIncluded = 450;
        int minutesUsed = 1000;
        std::cout << "Plan Cost: " << planCost << " with " << 
            includedMinutesString(minutesIncluded) << " minutes included\n";
        std::cout << "Cost with " << minutesUsed << " minutes used is  " << 
            calculateCost(planCost, minuteCost, minutesIncluded, minutesUsed) << "\n";
    }
    std::cout << "\n";
    {
        double planCost = 59.99;
        double minuteCost = 0.40;
        int minutesIncluded = 900;
        int minutesUsed = 1000;
        std::cout << "Plan Cost: " << planCost << " with " << 
            includedMinutesString(minutesIncluded) << " minutes included\n";
        std::cout << "Cost with " << minutesUsed << " minutes used is  " << 
            calculateCost(planCost, minuteCost, minutesIncluded, minutesUsed) << "\n";
    }
    std::cout << "\n";
    {
        double planCost = 69.99;
        double minuteCost = 0;
        int minutesIncluded = 0;
        int minutesUsed = 1000;
        std::cout << "Plan Cost: " << planCost << " with " << 
            includedMinutesString(minutesIncluded) << " minutes included\n";
        std::cout << "Cost with " << minutesUsed << " minutes used is  " << 
            calculateCost(planCost, minuteCost, minutesIncluded, minutesUsed) << "\n";
    }
    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 2019-03-29
    • 1970-01-01
    • 2015-08-07
    相关资源
    最近更新 更多