数学逻辑功能不正确
Math logic incorrect in function?
我正在从事此练习,这是完整的,但是在计算老年人折扣时,数学在某个地方必须是错误的。我使用的软件运行了这两个输入以检查问题。
-
2010n212(运行良好)
-
2010y212(没有给我预期的结果)
这使我相信问题在于双重确定限制函数的老年人折扣部分。
预期的结果是"会员资格成本= $ 162.80" ,但我的代码给我"会员资格成本= $ 152.00"
我不确定这里怎么了。我希望第二组眼睛能帮助找到它。预先感谢您。
这是代码:
// headers
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// prototypes
void displayGeneralInformation ();
void readNecessaryInformation (double& regularCostPerMonth,
double& costPerPersonalTrainingSession,
bool& seniorCitizen, int& numberOfSessions,
int& numberOfMonths);
double determineMembershipCost (double regularCostPerMonth,
double costPerPersonalTrainingSession,
bool seniorCitizen, int numberOfSessions,
int numberOfMonths);
// main
int main()
{
//variables
double regularCostPerMonth;
double costPerPersonalTrainingSession;
bool seniorCitizen;
int numberOfSessions;
int numberOfMonths;
double cost;
// print menu
// calls
// call displayGeneralInformation
displayGeneralInformation ();
// call readNecessaryInformation
readNecessaryInformation (regularCostPerMonth,
costPerPersonalTrainingSession,
seniorCitizen, numberOfSessions,
numberOfMonths);
// call determineMembershipCost
cost = determineMembershipCost (regularCostPerMonth, costPerPersonalTrainingSession, seniorCitizen, numberOfSessions, numberOfMonths);
// Display cost of membership
cout << "nThe membership cost = $" << setprecision(2)<< fixed << cost << endl;
return 0;
}
// displayGeneralInformation function definition
void displayGeneralInformation ()
{
cout << "nWelcome to Stay Healthy 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% off 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;
}
// readNecessaryInformation function definition
void readNecessaryInformation (double& regularCostPerMonth,
double& costPerPersonalTrainingSession,
bool& seniorCitizen, int& numberOfSessions,
int& numberOfMonths)
{
cout << "nEnter the cost of a regular membership per month: $";
cin >> regularCostPerMonth;
cout << "Enter the cost of one personal training session: $";
cin >> costPerPersonalTrainingSession;
cout << "Are you a senior citizen (Y,y/N,n): ";
char ch;
cin >> ch;
if (ch == 'Y' || ch == 'y')
seniorCitizen = true;
else
seniorCitizen = false;
cout << "Enter the number of personal training sessions bought: ";
cin >> numberOfSessions;
cout << "Enter the number of months you are paying for: ";
cin >> numberOfMonths;
}
// determineMembershipCost function definition
double determineMembershipCost (double regularCostPerMonth, double costPerPersonalTrainingSession, bool seniorCitizen, int numberOfSessions, int numberOfMonths)
{
double cost = regularCostPerMonth * numberOfMonths;
if (seniorCitizen)
{
cost = cost - (regularCostPerMonth * 0.30 * numberOfMonths);
}
if (numberOfMonths >= 12)
{
cost = cost - (regularCostPerMonth * 0.15 * numberOfMonths);
}
cost = cost + (costPerPersonalTrainingSession * numberOfSessions);
if (numberOfSessions > 5)
{
cost = cost - (costPerPersonalTrainingSession * 0.20 * numberOfSessions);
}
return cost;
}
尝试一下:
cost = (cost - (cost * 0.30)); //for 30% off
cost = (cost - (cost * 0.15)); //15% off
相关文章:
- 使用2个键的cpp-stl::优先级队列排序不正确
- 正弦级数方程计算不正确
- 我试图制作一个程序,要求用户输入问题和答案,但程序循环不正确
- 密码登录程序将永远循环并显示不正确的结果
- 在C++中返回不正确的楼层函数值
- 形状对象的旋转和缩放不正确C++
- C++ 读取文件读取文件不正确
- 关于类的 Python 文档 - 对C++的引用不正确
- 特征 LLT 模块给出不正确的结果?
- glibcxx STL 在实现 std::valarray::sum() 时是否不正确?
- 如果语句逻辑不正确
- 来自逆 vp 矩阵和相机位置的光线方向不正确
- 不正确的比较和交换计数器输出用于快速排序功能
- 功能中的添加不正确
- 数学逻辑功能不正确
- 自定义功能不返回正确的值
- C 11 lambda可以分配给具有不正确签名的std ::功能
- 此功能可能出现哪些错误或不正确?
- 在C++中扩展容器(如std::vector)功能的正确方法,而不从中继承
- 如何获得可变参数模板功能的正确分辨率而不会太冗长