【问题标题】:Calculation not being done right计算不正确
【发布时间】:2017-05-14 08:40:33
【问题描述】:

我是 C++ 的新手,我被分配了一个相当基本的程序,用户可以用它来买票,但我在计算上遇到了一些问题。

这是我目前的代码。

#include <iostream>

using namespace std;

int main()
{
double type_ticket, num_tickets, price1, price2, price3, total_price, decision;

cout << "Welcome to the ticket kiosk.";
cout << "\n";
cout << "\n";
cout << "1. VVIP - RM 200";
cout << "\n";
cout << "2. VIP - RM 150";
cout << "\n";
cout << "3. Normal - RM 100" << endl;
cout << "\n";


do
{
cout << "Please select the category of ticket you would like to purchase: ";
cin  >> type_ticket;
cout << "\n";


if (type_ticket == 1)
{
cout << "How many would you like: ";
cin  >> num_tickets;
cout << "\n";
price1 = num_tickets * 200;
cout << "The price is: RM " << price1 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin  >> decision;
cout << "\n";
}


else if (type_ticket == 2)
{
cout << "How many would you like: ";
cin  >> num_tickets;
cout << "\n";
price2 = num_tickets * 150;
cout << "The price is: RM " << price2 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin  >> decision;
cout << "\n";
}


else if (type_ticket == 3)
{
cout << "How many would you like: ";
cin  >> num_tickets;
cout << "\n";
price3 = num_tickets * 100;
cout << "The price is: RM " << price3 << endl;
cout << "\n";
cout << "\n";
cout << "1. YES" << endl;
cout << "2. NO" << endl;
cout << "\n";
cout << "Would you like to continue purchasing more tickets: ";
cin  >> decision;
cout << "\n";
}


else
{
cout << "You have entered an invalid input, please try again. " << endl;
cout << "\n";
}


}
while (decision == 1);

total_price = price1 + price2 + price3;
cout << "The grand total is: RM " << total_price << endl;
cout << "\n";
cout << "Thank you for using this service today, we hope you enjoy the show." << endl;
cout << "\n";

}

我遇到的问题是当用户从 vvip 和/或 vip 购买门票时,total_price 的计算没有正确完成。但是,当输入价格 3 时,计算工作正常。

用户购买 vvip 和/或 vip = 计算不正确。 用户购买正常且 vvip 和/或 vip = 计算正确。

非常感谢任何帮助。

仅供参考,此代码尚未完成,但现在,这就是我所拥有的。

【问题讨论】:

  • 请解释“未正确完成”的含义。输入、输出、预期输出是什么?
  • 由于您是 SO 新手,请感谢其他用户的时间,因此您至少需要:在您的问题中正确格式化您的代码,解释一个具体问题(“无法正常工作”是'不是一个正确的描述)与您的输入和输出。
  • 我刚刚运行了您的程序,从您提供的内容来看,它似乎工作正常。请求 3 张类型 2 的票,总费用为 450。请详细了解这不起作用。
  • 您应该考虑编写一个处理所有这三种情况的函数。除了存储价格的变量之外,代码是相同的。
  • 对不起,没意识到我说的不够清楚,我的意思是,个别类别的计算工作正常,这就是你得到450的方式,问题是用户买票后从某个类别中,询问您是否要继续的问题,如果输入 1(是)程序循环回到选择的类别,但是当用户输入 2(否)时,程序应该显示总计我声明为total_price。 total_price 应该只是我声明的价格 1,2 和 3 的加法。

标签: c++ calculation


【解决方案1】:

在计算以下变量之前,您似乎没有初始化 priceN(其中 N1, 2, 3 之一)变量:

total_price = price1 + price2 + price3;

如果只有一种类型的票,结果是不可预测的,因为变量包含垃圾。

你应该从:

double price1 = 0;
double price2 = 0;
double price3 = 0;

【讨论】:

  • 这行得通,谢谢你,我真的很感激,我确实尝试过这样做,但没有将其声明为双倍三倍。 double type_ticket, num_tickets, price1 = 0, price2 = 0, price3 = 0, total_price, 决定;我假设这样做是行不通的?
  • 这里不是声明,关键是初始化。多行声明是一种风格选择,但关键是没有默认初始化为 0,所以你必须自己做。肯定会的
  • 所以如果我在 if else 部分添加 priceN = 0,例如在这两个代码之间,cout
  • 在声明站点初始化以减少错误和更清晰的代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-13
  • 2011-06-13
  • 1970-01-01
相关资源
最近更新 更多