Расчет сделан неправильно

Я довольно плохо знаком с 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 = расчет сделан правильно.

Любая помощь будет очень высоко ценится.

К вашему сведению, этот код еще не завершен, но сейчас это то, что у меня есть.

1

Решение

Вы, кажется, не инициализировать priceN (где N один из 1, 2, 3) переменные до расчета:

total_price = price1 + price2 + price3;

в случае только одного типа заявки результат непредсказуем, поскольку переменные содержат мусор.

Вы должны начать с:

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

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector