Help! me please with my code i am stuck with a annoying bug in c++

Hi, basically I am working on a program and I seem to be getting a very annoying bug which I am strugulling with. It wont print the total price for each person.
Here is my code below:
#include <iostream>

#include <string>

using namespace std;

int main () {

string staffName;

string movieName1 = “Deadpool”;

string movieName2 = “Goosebumps”;

string movieSelected;

cout << “Hi and Welcome to the Cinema Ticket Generator” << endl;

cout << "Please enter your name: " << endl;

cin >> staffName;

cout << "Welcome to the program " << staffName << endl;

cout << “There are two movies available to watch in the Cinema today” << endl;

cout << “Which movie would you like to watch " << movieName1 << " or " << movieName2 <<”: " << endl;

cin >> movieSelected;

float adultRate = 10.30;

float seniorRate = 20/100 * adultRate;

float calculatedSeniorRate = adultRate - seniorRate;

float childrenRate = 50/100 * adultRate;

int adult = 0;

int senior = 0;

int children = 0;

float totaladultPrice = adultRate * adult;

float totalSeniorPrice = calculatedSeniorRate * senior;

float totalChildrenPrice = childrenRate * children;

double ticketTotalPrice = totaladultPrice + totalSeniorPrice + totalChildrenPrice;

if (movieSelected == movieName1) {

cout << "How many Adults are there: ";

cin >> adult;

cout << "How many Senior are there: " << endl;

cin >> senior;

cout << "How many Children are there: " << endl;

cin >> children;

cout << "The total price for Adults is: " << totaladultPrice << endl;

cout << "The total price for Senior is: " << totalSeniorPrice << endl;

cout << "The total price for Children is: " << totalChildrenPrice << endl;

cout << "The Ticket is being printed: " << endl;

cout << movieName1 << endl;

cout << adult << ": " <<totaladultPrice << endl;

cout << senior << ": " << totalSeniorPrice << endl;

cout << children << ": " << totalChildrenPrice << endl;

cout << ticketTotalPrice << endl;

} else {

cout << "How many Adults are there: " << endl;

cin >> adult;

cout << "How many Senior are there: " << endl;

cin >> senior;

cout << "How many Children are there: " << endl;

cin >> children;

cout << "The total price for Adults is: " << totaladultPrice << endl;

cout << "The total price for Senior is: " << totalSeniorPrice << endl;

cout << "The total price for Children is: " << totalChildrenPrice << endl;

cout << "The Ticket is being printed: " << endl;

cout << movieName2 << endl;

cout << adult << ": " << totaladultPrice << endl;

cout << senior << ": " << totalSeniorPrice << endl;

cout << children << ": " << totalChildrenPrice << endl;

cout << ticketTotalPrice << endl;

}

cout << "Enjoy your movie!! " << endl;

return 0;

}

Please Help me I am really stuck and I cant seem to fix it.
Thanks

I’m assuming the result you are getting is always 0?

Pay close attention to where you are initializing variables, where you use them and where you set them.

your current logic:

int adult = 0; //sets adult to 0

float totaladultPrice = adultRate * adult; // totaladultPrice = adultRate(0.5)(0) NOTE: x(0)=0

cin >> adult; // useless since adult is no longer used in future calculations, only for display

cout << adult << ": " << totaladultPrice << endl; // output will be user input: 0

I assure you the multiplicative property of Zero is not a bug. In fact, you’ve proven it to be true :slight_smile:

I was wondering is if you had placed you up-casted the right variables? I used to run into this problem when I was finishing up my programming C assignments and the value isn’t specified. All because they weren’t up-casted with “double”.

Not sure it’s written in C++ but the idea is the same