I need help to fix the code without(not) using a break and keep using while meth
ID: 3796792 • Letter: I
Question
I need help to fix the code without(not) using a break and keep using while method
#include
#include
using namespace std;
//children and adult price as constants
const double CHILDREN_PRICE = 12.00;
const double ADULT_PRICE = 26.50;
//Main method
int main()
{
// main method variables
int childtix;
int adulttix,count=0,confirmation_no=100;
double childtotal,bal;
double adulttotal,security_pay=15.00;
double totalbill;
double totalcash;
double change;
// print output title
cout << " Chesapeake Amusement Park" << endl << endl;
//Ask user to enter number of childrens and adults tickets
while(true)
{
cout << " Enter children tickets or -1 to Stop ..... ";
cin >> childtix;
if(childtix==-1)
{
break;
}
cout << " Enter adults tickets:..................... ";
cin >> adulttix;
count=childtix/4;
childtotal = (childtix * CHILDREN_PRICE)-(count*CHILDREN_PRICE);
if(adulttix>5)
{
adulttotal = (adulttix * ADULT_PRICE)-3;
}
else
{
adulttotal = (adulttix * ADULT_PRICE);
}
if(adulttix+childtix>20 || childtix>=14)
{
// total calculations
totalbill = childtotal + adulttotal+security_pay;;
}
else
{
totalbill = childtotal + adulttotal;
}
// displays output title, number of tickets,Price, Total price, Total cash, and change
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << " Chesapeake Amusement Park";
cout << " -------------------------";
cout << " Tickets Price Total ";
cout << " Children " << setw(3) << childtix
<< setw(15) << CHILDREN_PRICE << setw(12) << childtotal; cout << " ";
cout << " Adult " << setw(6) << adulttix
<< setw(15) << ADULT_PRICE << setw(12) << adulttotal; cout << " ";
cout << setw(13) << childtix + adulttix<< endl; cout << " ";
if(adulttix+childtix>20 || childtix>=14)
cout << " Security " <
cout << " Total Bill "<< setw(20) << totalbill; cout << " ";
//Getting the Total cash.
cout << " Cash Received ....... ";
cin >> totalcash;
bal=totalbill-totalcash;
if(bal>0)
{
while(true)
{
cout<<" Cash must be >= Total Bill";
cout << " Cash Received ....... ";
cin >> totalcash;
bal=totalbill-totalcash;
if(bal==0)
break;
else if(bal>0)
{
continue;
}
else if(bal<0)
{
change=-(bal);
break;
}
}
}
cout << " Change " << setw(25) << change; cout << " ";
cout<<" Confirmation Number = "<
}
system("pause");
return 0;
}
Explanation / Answer
There were two while loops and I have re-written both in such a way that break statement is not required.
PROGRAM CODE:
#include <iostream>
#include <iomanip>
using namespace std;
//children and adult price as constants
const double CHILDREN_PRICE = 12.00;
const double ADULT_PRICE = 26.50;
//Main method
int main()
{
// main method variables
int childtix;
int adulttix,count=0,confirmation_no=100;
double childtotal,bal;
double adulttotal,security_pay=15.00;
double totalbill;
double totalcash;
double change = 0.00;
// print output title
cout << " Chesapeake Amusement Park" << endl << endl;
//Ask user to enter number of childrens and adults tickets
//rewriting the while llop here and asking the user to enter Y/N to continue the loop
char tobecontinued = 'Y';
while(tobecontinued == 'y' || tobecontinued == 'Y')
{
cout << " Enter children tickets or -1 to Stop ..... ";
cin >> childtix;
cout << " Enter adults tickets:..................... ";
cin >> adulttix;
count=childtix/4;
childtotal = (childtix * CHILDREN_PRICE)-(count*CHILDREN_PRICE);
if(adulttix>5)
{
adulttotal = (adulttix * ADULT_PRICE)-3;
}
else
{
adulttotal = (adulttix * ADULT_PRICE);
}
if(adulttix+childtix>20 || childtix>=14)
{
// total calculations
totalbill = childtotal + adulttotal+security_pay;;
}
else
{
totalbill = childtotal + adulttotal;
}
// displays output title, number of tickets,Price, Total price, Total cash, and change
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
cout << " Chesapeake Amusement Park";
cout << " -------------------------";
cout << " Tickets Price Total ";
cout << " Children " << setw(3) << childtix
<< setw(15) << CHILDREN_PRICE << setw(12) << childtotal; cout << " ";
cout << " Adult " << setw(6) << adulttix
<< setw(15) << ADULT_PRICE << setw(12) << adulttotal; cout << " ";
cout << setw(13) << childtix + adulttix<< endl; cout << " ";
if(adulttix+childtix>20 || childtix>=14)
cout << " Security ";
cout << " Total Bill "<< setw(20) << totalbill; cout << " ";
//Getting the Total cash.
cout << " Cash Received ....... ";
cin >> totalcash;
bal=totalbill-totalcash;
//Rewrote the loop over here to remove the break keyword
while(bal > 0)
{
cout<<" Cash must be >= Total Bill";
cout << " Cash Received ....... ";
cin >> totalcash;
bal=totalbill-totalcash;
}
if(bal<0)
change=-(bal);
cout << " Change " << setw(25) << change; cout << " ";
cout<<" Confirmation Number = ";
cout<<"Do you want to continue(Y/N): ";
cin>>tobecontinued;
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.