Need help declaring a value for "cash". - c++

it says in the instruction "you may assume the cash to be equal or greater than the total bill". How do I declare a value for cash and make it greater than or equal to the total bill? Is my declaration of the variables for "change" "totalBill" are correct? I really need some help and like I said this is my first time.
#include <iostream>
#include <iomanip>
using namespace std;
const double CHILDPRICE = 10.00;
const double ADULTPRICE = 20.50;
int main ()
{
double adultTotal, childTotal, totalBill, change;
int childTix, adultTix, cash;
cout << "\n Chesapeake Amusement Park" << endl << endl;
cout << " Enter children tickets... ";
cin >> childTix;
cout << " Enter adult tickets...... ";
cin >> adultTix;
childTotal = CHILDPRICE * childTix;
adultTotal = ADULTPRICE * adultTix;
totalBill = childTotal + adultTotal;
change = cash - totalBill;
//The following is a sample of how you can format the output
//childTix is an integer variable
//CHILDPRICE is a double const
//childTotal is a double variable
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\n\n Chesapeake Amusement Park";
cout << "\n -------------------------";
cout << "\n\n Tickets Price Total\n";
cout << " Children " << setw(3) << childTix
<< setw(14) << CHILDPRICE
<< setw(11) << childTotal;
cout << "\n Adults " << setw(3) << adultTix
<< setw(14) << ADULTPRICE
<< setw(11) << adultTotal;
cout << "\n ";
cout << "\n ";
cout << "\n Total Bill " << setw(11) << totalBill;
//Place remaining input/output statements here
cout << "\n ";
cout << "\n ";
cout << "\n Cash recieved...... ";
cin >> cash;
cout << "\n ";
cout << "\n ";
cout << "\n Change " << setw(4) << change;
return 0;
}

Related

How to store multiple string inputs and displaying it after

I'm just starting in studying C++, and I am doing a simple challenge which is GWA Calculator, but I am having a problem finding out how to store the multiple strings input (which is the Subjects/Course) and displaying it after together with the Units and Grades. I am really sorry, but I tried finding out how and I couldn't find an answer. Hope you can help me out.
#include <stdlib.h>
using namespace std;
void calculateGWA();
int main()
{
system("cls");
int input;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
cout << "\t\t| GWA Calculator |" << endl;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
cout << "\t\t| MENU:\t\t\t\t\t\t\t " << "|" << endl;
cout << "\t\t| 1. Calculate GWA (General Weighted Average)\t\t " << "|" << endl;
cout << "\t\t| 2. Calculate CGWA (Cummulative Weighted Average) " << "|" << endl;
cout << "\t\t| 4. Exit Application\t\t\t\t\t " << "|" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t -------------------------------------------------------------------------- " << endl;
sub:
cout << "\t\tEnter your choice: ";
cin >> input;
switch(input)
{
case 1:
calculateGWA();
break;
case 2:
//calculateCGPA();
break;
case 3:
main();
break;
case 4:
exit(EXIT_SUCCESS);
break;
default:
cout << "You have entered wrong input.Try again!\n" << endl;
goto sub;
break;
}
}
void calculateGWA()
{
int q;
system("cls");
cout << "-------------- GWA Calculator -----------------"<<endl;
cout << " How many course(s)?: ";
cin >> q;
char c_name[50];
float unit [q];
float grade [q];
cout << endl;
for(int i = 0; i < q; i++)
{
cout << "Enter the Course Name " << i+1 << ": ";
cin >> c_name;
cout << "Enter the Unit " << c_name << ": ";
cin >> unit[i];
cout << "Enter the Grade " << c_name << ": ";
cin >> grade[i];
cout << "-----------------------------------\n\n" << endl;
}
float sum = 0;
float tot;
for(int j = 0; j < q; j++)
{
tot = unit[j] * grade[j];
sum = sum + tot;
}
float totCr = 0;
for(int k = 0; k < q; k++)
{
totCr = totCr + unit[k];
}
system("cls");
// PRINTS OUT THE COURSES - UNITS - GRADES AND GWA //
cout << "\t\t =============================================================== " << endl;
cout << "\t\t| COURSE | UNIT | GRADE |" << endl;
cout << "\t\t =============================================================== " << endl;
cout << "Total Points: " << sum << " \n Total Credits: " << totCr << " \nTotal GPA: " << sum/totCr << " ." << endl;
cout << c_name << "\n" << endl;
cout << "===================================" << endl;
sub:
int inmenu;
cout << "\n\n\n1. Calculate Again" << endl;
cout << "2. Go Back to Main Menu" << endl;
cout << "3. Exit This App \n\n" << endl;
cout << "Your Input: " << endl;
cin >> inmenu;
switch(inmenu)
{
case 1:
calculateGPA();
break;
case 2:
main();
break;
case 3:
exit(EXIT_SUCCESS);
default:
cout << "\n\nYou have Entered Wrong Input!Please Choose Again!" << endl;
goto sub;
}
}

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.
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;

How do I create an accumulator in C++ using a user defined function?

I am trying to keep a running total of cups of coffee sold and I have to use a user defined function to do it. I have tried numerous variations of the attached code but nothing seems to work. What am I doing wrong? Also I am a newb to C++ so that is why it looks amateurish!
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int SM_OZ = 8;
const int MD_OZ = 12;
const int LG_OZ = 16;
const double SM_PRICE = 1.19;
const double MD_PRICE = 1.49;
const double LG_PRICE = 1.89;
const double TAX = .0825;
void amtSold(int &smtCup, int &mdtCup, int &lgtCup);
int main()
{
int selection;
int smCup;
int mdCup;
int lgCup;
int smtCup;
int mdtCup;
int lgtCup;
smCup = 0;
mdCup = 0;
lgCup = 0;
do
{
cout << "COFFEE SHOP" << endl;
cout << "1. Sell Coffee" << endl;
cout << "2. Total Number of Cups Sold" << endl;
cout << "3. Total Amount of Coffee Sold" << endl;
cout << "4. Total Amount of Money made" << endl;
cout << "0. Exit" << endl;
cout << "Type a number to continue: ";
cin >> selection;
cout << endl;
//loop through the solutions based on the user's selection
switch (selection)
{
case 1:
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;
system("cls");
cout << fixed << setprecision(2) << endl;
//Sale Coffee Receipt Page
cout << "COFFEE SHOP" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE)+(mdCup*MD_PRICE)+(lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))+(((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 2:
//Total Number of Cups Sold
cout << "REPORT - NUMBER OF COFFEE CUPS SOLD" << endl;
amtSold(smtCup, mdtCup, lgtCup);
cout << "SIZE" << setw(21) << "Number" << endl;
cout << "Small: " << setw(18) << smCup << endl;
cout << "Medium: " << setw(17) << mdCup << endl;
cout << "Large: " << setw(18) << lgCup << endl;
cout << endl;
cout << endl;
break;
case 3:
//Total Amount of Coffee Sold
cout << "REPORT - AMOUNT OF COFFEE SOLD" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "OZ" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << smCup*SM_OZ << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << mdCup*MD_OZ << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << lgCup*LG_OZ << endl;
cout << "Total: " << setw(36) << (smCup*SM_OZ) + (mdCup*MD_OZ) + (lgCup*LG_OZ) << endl;
cout << endl;
cout << endl;
break;
case 4:
//Total Amount of Money made
cout << "COFFEE SHOP - REPORT MONEY MADE" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE)) + (((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 0:
system("cls");
break;
default:
//notify the user that an invalid selection has been inputted
cout << "You have made an invalid selection. Please choose a number from the list." << endl;
cout << endl;
}
} while (selection != 0);
system("pause");
return 0;
}
void amtSold(int &smtCup, int &mdtCup, int &lgtCup)
{
int smCup;
int mdCup;
int lgCup;
smCup = 0;
mdCup = 0;
lgCup = 0;
smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;
}
So as you probably know, you're not keeping track of the number of coffee cups of each size that you're selling (i.e. smtCup, mdtCup, and lgtCup).
I'm assuming that these variables mean the total number of cups for each size, you might want to put some comments during the variable declaration step. You'll want to initialise the variables to 0:
int smtCup = 0;
int mdtCup = 0;
int lgtCup = 0;
As this is a fairly simple program, you can perform accumulation without using your amtSold function, so you can delete that.
Then, in case 1 of your switch statement, you'll want to update smtCup, mdtCup, and lgtCup every time you update the values. Please be aware that smCup, mdCup, and lgCup are used only for input in this program.
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;
smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;
From here on out, you can print out the total number of small, medium and large cups by calling smtCup, mdtCup, and lgtCup in the other cases! Change smCup, mdCup, and lgCup to smtCup, mdtCup, and lgtCup in cases 2-4. Hope this helps!
Edit: Can't comment, so I'll just say you're welcome here!
Thanks KTing! It is disappointing to know I was much closer to a correct answer with an earlier version of my code. I could not figure out why it would not initialize and so I started to get desperate and try things I was 95% sure would not work. I ended up going with the following solution.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
//Constant for size of cup of coffee
const int SM_OZ = 8;
const int MD_OZ = 12;
const int LG_OZ = 16;
//Constant for price of cup of coffee and Tax
const double SM_PRICE = 1.19;
const double MD_PRICE = 1.49;
const double LG_PRICE = 1.89;
const double TAX = .0825;
int main()
{
//declare and initialize the variables for the individual cups of coffee
int selection;
int smCup = 0;
int mdCup = 0;
int lgCup = 0;
//declare and initialize the variables for the total cups of coffee
int smtCup = 0;
int mdtCup = 0;
int lgtCup = 0;
do
{
//get input from user as to what they want to do
cout << "COFFEE SHOP" << endl;
cout << "1. Sell Coffee" << endl;
cout << "2. Total Number of Cups Sold" << endl;
cout << "3. Total Amount of Coffee Sold" << endl;
cout << "4. Total Amount of Money made" << endl;
cout << "0. Exit" << endl;
cout << "Type a number to continue: ";
cin >> selection;
cout << endl;
//loop through the solutions based on the user's selection
switch (selection)
{
case 1:
//get the number of cups of coffee from the user
cout << "How many small cups of coffee: ";
cin >> smCup;
cout << "How many medium cups of coffee: ";
cin >> mdCup;
cout << "How many large cups of coffee: ";
cin >> lgCup;
//get the total cups of coffee and store it as a variable
smtCup += smCup;
mdtCup += mdCup;
lgtCup += lgCup;
system("cls");
cout << fixed << setprecision(2) << endl;
//Sale Coffee Receipt Page
cout << "COFFEE SHOP" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smCup << setw(18) << SM_PRICE << setw(18) << smCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdCup << setw(18) << MD_PRICE << setw(18) << mdCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgCup << setw(18) << LG_PRICE << setw(18) << lgCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smCup*SM_PRICE)+(mdCup*MD_PRICE)+(lgCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))+(((smCup*SM_PRICE) + (mdCup*MD_PRICE) + (lgCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 2:
//Total Number of Cups Sold
cout << "REPORT - NUMBER OF COFFEE CUPS SOLD" << endl;
cout << "SIZE" << setw(21) << "Number" << endl;
cout << "Small: " << setw(18) << smtCup << endl;
cout << "Medium: " << setw(17) << mdtCup << endl;
cout << "Large: " << setw(18) << lgtCup << endl;
cout << endl;
cout << endl;
break;
case 3:
//Total Amount of Coffee Sold
cout << "REPORT - AMOUNT OF COFFEE SOLD" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "OZ" << endl;
cout << "Small: " << setw(18) << smtCup << setw(18) << smtCup*SM_OZ << endl;
cout << "Medium: " << setw(17) << mdtCup << setw(18) << mdtCup*MD_OZ << endl;
cout << "Large: " << setw(18) << lgtCup << setw(18) << lgtCup*LG_OZ << endl;
cout << "Total: " << setw(36) << (smtCup*SM_OZ) + (mdtCup*MD_OZ) + (lgtCup*LG_OZ) << endl;
cout << endl;
cout << endl;
break;
case 4:
//Total Amount of Money made
cout << "COFFEE SHOP - REPORT MONEY MADE" << endl;
cout << "SIZE" << setw(21) << "Number" << setw(18) << "Price" << setw(18) << "Total" << endl;
cout << "Small: " << setw(18) << smtCup << setw(18) << SM_PRICE << setw(18) << smtCup*SM_PRICE << endl;
cout << "Medium: " << setw(17) << mdtCup << setw(18) << MD_PRICE << setw(18) << mdtCup*MD_PRICE << endl;
cout << "Large: " << setw(18) << lgtCup << setw(18) << LG_PRICE << setw(18) << lgtCup*LG_PRICE << endl;
cout << "Subtotal: " << setw(51) << (smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE) << endl;
cout << "Tax: (8.25%)" << setw(49) << ((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE))*TAX << endl;
cout << "Total: " << setw(54) << ((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE)) + (((smtCup*SM_PRICE) + (mdtCup*MD_PRICE) + (lgtCup*LG_PRICE))*TAX) << endl;
cout << endl;
cout << endl;
break;
case 0:
system("cls");
break;
default:
//notify the user that an invalid selection has been inputted
cout << "You have made an invalid selection. Please choose a number from the list." << endl;
cout << endl;
}
//loop through if the user is still making a valid selection
} while (selection != 0);
system("pause");
return 0;
}

How do I properly format C++ floats to two decimal places?

I'm having some issues using setprecision. I don't understand how it works completely. I searched the problem and was able to extrapolate some code that should've worked. I don't understand why it's not. Thank you for your help, I'm still kind of new at this.
//monthly paycheck.cpp
//Paycheck Calculator
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int main() {
//Constants
const double
FEDERAL_TAX = 0.15, //Federal Tax
STATE_TAX = 0.035, //State Tax
SSA_TAX = 0.085, //Social Security & Medicare
HEALTH_INSURANCE = 75; //Health Insurance
//Variables
int year;
double grossAmount;
string employeeName, month;
// Initialize variables with input
cout << "Hello, what's your first name? ";
cin >> employeeName;
cout << "What is your gross amount? ";
cin >> grossAmount;
cout << "Please enter the month and year: ";
cin >> month >> year;
// Output
cout << "***********************************" << endl;
cout << "Paycheck" << endl;
cout << "Month: " << month << "\tYear: " << year << endl;
cout << "Employee Name: " << employeeName << endl;
cout << "***********************************" << endl;
cout << setprecision(5) << fixed;
cout << "Gross Amount: $" << grossAmount << endl;
cout << "Federal Tax: $" << FEDERAL_TAX*grossAmount << endl;
cout << "State Tax: $" << STATE_TAX*grossAmount << endl;
cout << "Social Sec / Medicare: $" << SSA_TAX*grossAmount << endl;
cout << "Health Insurance: $" << HEALTH_INSURANCE << endl << endl;
cout << "Net Amount: $" << fixed << grossAmount-grossAmount*(FEDERAL_TAX+STATE_TAX+SSA_TAX)-HEALTH_INSURANCE << endl << endl;
system("PAUSE");
return 0;
}
If you want to format floats to display with 2 decimal places in C++ streams, you could easily:
float a = 5.1258f;
std::cout << std::fixed << std::setprecision(2) << a << std::endl;
See std::fixed and std::setprecision
Use stream manipulators:
std::cout.fixed;
std::cout.precision(Number_of_digits_after_the_decimal_point);

Adding a while loop to continue or exit the console in C++

Hey guys basically I need to put a while loop in my code for example a user can enter the number of tickets for the children and adding -1 to stop. Can you teach me how and where to put my while loop? Its my first time taking a C++ class. I would greatly appreciate your help guys. here is my code and an example of the output.
Example output
Chesapeake Amusement Park
Enter children ticets or -1 to stop... 10
Enter adult tickets................... 11
Chesapeake Amusement Park
----------------------------
Tickets Price Total
Child 10 10.00 10.00
Adult 11 20.50 220.50
21
Security Fee 15.00
Total Bill $ 335.00
Cash received.....340
Change 4.50
Enter children tickets or -1 to stop...
Code
#include <iostream>
#include <iomanip>
using namespace std;
const double ADULTPRICE = 20.50;
const double SECURITYFEE = 15.00;
int main ()
{
double adultTotal, childTotal, totalBill, change, cash;
double CHILDPRICE = 12.00;
int childTix, adultTix, tixTotal;
cout << "\n Chesapeake Amusement Park" << endl << endl;
cout << " Enter children tickets or -1 to stop... ";
cin >> childTix;
if (childTix >= 8)
CHILDPRICE = 10.00;
cout << " Enter adult tickets.................... ";
cin >> adultTix;
childTotal = CHILDPRICE * childTix;
adultTotal = ADULTPRICE * adultTix;
totalBill = childTotal + adultTotal;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\n\n Chesapeake Amusement Park";
cout << "\n -------------------------";
cout << "\n\n Tickets Price Total\n";
cout << " Children " << setw(3) << childTix
<< setw(14) << CHILDPRICE
<< setw(11) << childTotal;
cout << "\n Adults " << setw(3) << adultTix
<< setw(14) << ADULTPRICE
<< setw(11) << adultTotal;
tixTotal = childTix + adultTix;
cout << "\n ";
cout << "\n " << setw(11) << tixTotal;
cout << "\n ";
if ((tixTotal >= 20) || (childTix >= 14))
cout << "\n Security Fee " << setw(14) << SECURITYFEE;
cout << "\n";
cout << "\n Total Bill $" << setw(11) << totalBill;
cout << "\n ";
cout << "\n ";
cout << "\n Cash received...... ";
cin >> cash;
change = cash - totalBill;
cout << "\n ";
cout << "\n ";
cout << "\n Change " << setw(11) << change;
return 0;
}
Your code is great as a beginner! You just need to do minor changes to your code.
The primary change is to use a correct while expression to make the loop stops when the user inputs (-1) as a value for childTix. I do not recommend that you use the break keyword to exist the loop. You should always try to stop the loop naturally by letting the while expression be evaluated to FALSE. This makes your program easier to read and trace.
Another thing you need to add is the constant variable DISCOUNTEDCHILDPRICE when the number of children tickets is greater than 7. You cannot play with the value of CHILDPRICE inside the while loop cause this will lead to inconsistent results after you run the while loop the next time if the number of child tickets is less than 8. Instead, use if statement inside the body of the loop to figure out if there is a discount or not on children' tickets.
There is also a few other changes not mentioned above.
Here is the revised version of your code. I put the whole program so you and other beginners can learn from it and avoid having such problems in your future programs.
#include <iostream>
#include <iomanip>
using namespace std;
const double ADULTPRICE = 20.50;
const double SECURITYFEE = 15.00;
const double CHILDPRICE = 12.00;
const double DISCOUNTEDCHILDPRICE = 10;
int main()
{
double adultTotal, childTotal, totalBill, change, cash;
int childTix, adultTix, tixTotal;
cout << "\n Chesapeake Amusement Park" << endl << endl;
cout << " Enter children tickets or -1 to stop... ";
cin >> childTix;
while (childTix != -1)
{
cout << " Enter adult tickets.................... ";
cin >> adultTix;
if (childTix < 8)
childTotal = CHILDPRICE * childTix;
else
childTotal = DISCOUNTEDCHILDPRICE * childTix;
adultTotal = ADULTPRICE * adultTix;
totalBill = childTotal + adultTotal;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\n\n Chesapeake Amusement Park";
cout << "\n -------------------------";
cout << "\n\n Tickets Price Total\n";
cout << " Children " << setw(3) << childTix
<< setw(14) << CHILDPRICE
<< setw(11) << childTotal;
cout << "\n Adults " << setw(3) << adultTix
<< setw(14) << ADULTPRICE
<< setw(11) << adultTotal;
tixTotal = childTix + adultTix;
cout << "\n\n " << setw(11) << tixTotal;
cout << "\n ";
if ((tixTotal >= 20) || (childTix >= 14))
cout << "\n Security Fee " << setw(14) << SECURITYFEE;
cout << "\n\n Total Bill $" << setw(11) << totalBill;
cout << "\n\n";
cout << "\n Cash received...... ";
cin >> cash;
change = cash - totalBill;
cout << "\n\n";
cout << "\n Change " << setw(11) << change;
cout << "\n Chesapeake Amusement Park" << endl << endl;
cout << " Enter children tickets or -1 to stop... ";
cin >> childTix;
}
return 0;
}
First try on your own , then see the solution .
while(1)
{
cout << " Enter adult tickets.................... ";
cin >> adultTix;
if(adultTix == -1)
break;
childTotal = CHILDPRICE * childTix;
adultTotal = ADULTPRICE * adultTix;
totalBill = childTotal + adultTotal;
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << "\n\n Chesapeake Amusement Park";
cout << "\n -------------------------";
cout << "\n\n Tickets Price Total\n";
cout << " Children " << setw(3) << childTix
<< setw(14) << CHILDPRICE
<< setw(11) << childTotal;
cout << "\n Adults " << setw(3) << adultTix
<< setw(14) << ADULTPRICE
<< setw(11) << adultTotal;
tixTotal = childTix + adultTix;
cout << "\n ";
cout << "\n " << setw(11) << tixTotal;
cout << "\n ";
if ((tixTotal >= 20) || (childTix >= 14))
cout << "\n Security Fee " << setw(14) << SECURITYFEE;
cout << "\n";
cout << "\n Total Bill $" << setw(11) << totalBill;
cout << "\n ";
cout << "\n ";
cout << "\n Cash received...... ";
cin >> cash;
change = cash - totalBill;
cout << "\n ";
cout << "\n ";
cout << "\n Change " << setw(11) << change;
}
Another way of writing while loop is for( ; ; ) . The main point was to use keyword break .