Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算不正确 P>我对C++是相当新的,我已经被分配了一个相当基本的程序,用户可以用来购买票,但是我在计算方面有一些问题。_C++_Calculation - Fatal编程技术网

计算不正确 P>我对C++是相当新的,我已经被分配了一个相当基本的程序,用户可以用来购买票,但是我在计算方面有一些问题。

计算不正确 P>我对C++是相当新的,我已经被分配了一个相当基本的程序,用户可以用来购买票,但是我在计算方面有一些问题。,c++,calculation,C++,Calculation,这是到目前为止我的代码 #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 <

这是到目前为止我的代码

#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";

}
#包括
使用名称空间std;
int main()
{
双类型票,数量票,价格1,价格2,价格3,总价,决定;

您似乎没有在计算以下变量之前初始化
priceN
(其中
N
1、2、3
中的一个变量):

total_price = price1 + price2 + price3;
如果只有一种类型的票据,那么结果是不可预测的,因为变量包含垃圾

您应该从以下内容开始:

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

请解释“未正确完成”的含义。输入、输出、预期输出是什么?由于您是新用户,请感谢其他用户的时间,因此您至少需要:正确设置问题中的代码格式,解释具体问题(“无法正确工作”不是正确的描述)你的输入和输出。我刚刚运行你的程序,从你所提供的,它似乎工作良好。请求3票2型,总成本为450。请进入更多细节,这是如何不工作。你应该考虑写一个处理所有三个案件的功能。价格。对不起,我没有意识到我不够清楚,我的意思是,单个类别的计算很好,这就是你如何得到450,问题是用户从某个类别购买门票后,会问你是否要继续,如果1(是)输入时,程序返回到类别选择,但当用户输入2(否)时,程序应该显示我声明为总价的总计。总价应该是我声明的价格1、2和3的相加。这很有效,谢谢。我真的很感激,我确实尝试过这样做,但没有声明为双倍。双倍输入ket,num_tickets,price1=0,price2=0,price3=0,total_price,decision;我假设这样做是行不通的?这里的关键不是声明,而是初始化。多行声明是一种风格选择,但关键是没有de错误初始化为0,因此您必须自己执行。如果我在if-else部分(例如在这两个代码之间)添加了priceN=0,那么肯定会这样做,因为无法在声明站点初始化,以减少错误和更清晰的代码。