Divaakaran Shanmugam
Divaakaran Shanmugam

Reputation: 13

Calculation not being done right

I'm fairly new to c++, I have been given an assignment to do a fairly basic program that users can use to buy tickets but I am having some issues with the calculation.

This is my code so far.

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

}

The problem that I am having is when the user buys tickets from vvip and/or vip, the calculation for total_price is not being done right. When a price 3 has been entered however, the calculation works fine.

User buys vvip and/or vip = calculation not done right. User buys normal and vvip and/or vip = calculation done right.

Any help would be very much appreciated.

FYI, this code is not yet complete, but for now, this is what I have.

Upvotes: 1

Views: 65

Answers (1)

Alex
Alex

Reputation: 10126

You seem not to initialize priceN (where N is one of 1, 2, 3) variables before calculation of:

total_price = price1 + price2 + price3;

in case of only one type of the ticket, so the result is unpredictable because variables contain garbage.

You should start with :

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

Upvotes: 2

Related Questions