用户定义的功能

成为健身中心会员的费用如下:


老年人折扣为30%。
如果购买并支付了12个月或更长时间的会员资格,则折扣为15%
如果购买并支付了五个以上的私人培训课程,则每个课程的折扣为20%。


编写一个菜单驱动程序,确定新会员的费用。您的程序必须包含一个功能,该功能显示有关健身中心及其费用的一般信息,该功能可获取确定会员费用的所有必要信息,以及一个确定会员费用的功能。使用适当的参数将信息传入和传出函数。 (请勿使用任何全局变量。)

我的代码:

#include <iostream>
#include <iomanip>

using namespace std;

// program constants

void setPrices(double&, double&);
void getInfo(bool&, bool&, bool&, int&, int&);
double membershipCost(double, int, double, int, bool, bool, bool);

void displayCenterInfo();


int main()
{
    bool seniorCitizen;
    bool boughtFiveOrMoreSessions;
    bool paidTwelveOrMoreMonths;

    int numberOfMembershipMonths;
    int numberOfPersonalTrainingSessions;
    double regularMembershipChargesPerMonth;
    double costOfOnePersonalTrainingSession;

    double memberCost;

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

    displayCenterInfo();

    cout << endl;

    setPrices(regularMembershipChargesPerMonth, costOfOnePersonalTrainingSession);

    getInfo(seniorCitizen, boughtFiveOrMoreSessions, paidTwelveOrMoreMonths, numberOfMembershipMonths, numberOfPersonalTrainingSessions);

    // cal getInfo
    memberCost = membershipCost(regularMembershipChargesPerMonth, numberOfMembershipMonths, costOfOnePersonalTrainingSession,
        numberOfPersonalTrainingSessions, seniorCitizen, boughtFiveOrMoreSessions, paidTwelveOrMoreMonths);

    cout << "$" << memberCost;

    system("pause");
    return 0;
}

void displayCenterInfo()
{
    cout << "Welcome to Stay Healty and Fit center." << endl;
    cout << "This program determines the cost of a new membership." << endl;
    cout << "If you are a senior citizen, then the discount is 30% of "
        << "of the regular membership price." << endl;
    cout << "If you buy membership for twelve months and pay today, the "
        << "discount is 15%." << endl;
    cout << "If you buy and pay for 6 or more personal training session today, "
        << "the discount on each session is 20%." << endl;
}

void setPrices(double& regMemPrice, double& personalTrSesCost)
{

    cout << "Please enter the cost of regular Membership per month: " << endl;
    cin >> regMemPrice;

    cout << "Please enter the cost of one personal traning session: " << endl;
    cin >> personalTrSesCost;

}

void getInfo(bool& senCitizen, bool& bFiveOrMoreSess, bool& paidTwMnth,
    int& nOfMonths, int& nOfPersonalTrSess)
{
    //Senior Verification
    char userInputSenior;
    cout << "Are you Senior? Please enter 'Y' or 'N': ";
    cin >> userInputSenior;

    if (userInputSenior == 'y' && userInputSenior == 'Y')
    {
        senCitizen = true;
    }
    else
        senCitizen = false;

    cout << endl;


    //Number of personal training session.
    cout << "Enter the number of personal training sessions bought: ";
    cin >> nOfPersonalTrSess;

    if (nOfPersonalTrSess >= 5)
    {
        bFiveOrMoreSess = true;
    }
    else
        bFiveOrMoreSess = false;

    cout << endl;


    //Number of months
    cout << "Enter the number of months you are paying for: ";
    cin >> nOfMonths;

    if (nOfMonths >= 12)
    {
        paidTwMnth = true;
    }
    else
        paidTwMnth = false;

}

double membershipCost(double regMemPricePerMth, int nOfMonths,
    double personalTrSesCost, int nOfPersonalTrSess,
    bool senCitizen, bool bFiveOrMoreSess, bool paidTwMnth)
{
    double finalMembershipCost, finalSessionCost;


    //Session Discount
    if (bFiveOrMoreSess)
    {
        personalTrSesCost = personalTrSesCost * 0.8;
    }
    else
    {
        personalTrSesCost = personalTrSesCost;
    }


    //Month Discount
    if (paidTwMnth)
    {
        regMemPricePerMth = regMemPricePerMth * 0.85;
    }
    else
    {
        regMemPricePerMth = regMemPricePerMth;
    }


    finalMembershipCost = regMemPricePerMth * nOfMonths;
    finalSessionCost = personalTrSesCost * nOfPersonalTrSess;

    // Check if Senior Citizen Discount Applies
    if (senCitizen) {
        return (finalMembershipCost * 0.7) + finalSessionCost ;
    }
    else {
        return finalMembershipCost + finalSessionCost;
    }

}


我的测试结果

c&#43;&#43; - 计算折扣健身价格(优惠30%,20%和15%)-LMLPHP

“老年人折扣”发生错误。

c&#43;&#43; - 计算折扣健身价格(优惠30%,20%和15%)-LMLPHP

绿色-我的输出。

红色-输出(正确答案)。

我不知道如何通过我的代码获得答案(2260.00美元)。我已经检查了很多次,但无法解决问题。请帮我!

最佳答案

您应使用or-Statement来检测其是否为老年人:

if (userInputSenior == 'y' || userInputSenior == 'Y')


顺便说一句:在计算个人课程的折扣时,您还有另一个小错误,您只能获得5次以上的折扣,因此相应的if语句应为

(nOfPersonalTrSess > 5)

关于c++ - 计算折扣健身价格(优惠30%,20%和15%),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48635617/

10-13 02:29