Getting Bonus Pay to Calculate [duplicate] - c++

This question already has answers here:
Adding Overtime Pay
(2 answers)
Closed 9 years ago.
I've got two ifs, the first if which is the overtime if works, but i cannot get the 2nd if to work, which is the bonus pay if days work is greater than 5.
It's not ready the if code i typed for bonuspay,
#include<iostream>
#include<iomanip>
using namespace std;
int main() {
float ftax, stax, SDI, SS, hw, hp, dw(0), pay, netpay, gp, OvertimePay = 0,
bonusPay(0);
int daysWorked(0);
cout << "please enter the hoursWorked: ";
cin >> hw;
cout << "---------------------" << endl;
cout << "please enter the hourlyPay: ";
cin >> hp;
cout << "---------------------" << endl;
cout << "please enter the daysWorked in the week: ";
cin >> dw;
if (hw > 40) {
OvertimePay = (hw - 40) * hp * .5;
if (daysWorked > 5) {
bonusPay = (hw - 40) * hp * .25;
}
}
gp = (hw * hp) + (OvertimePay) + (bonusPay);
ftax = gp * .10;
stax = gp * .08;
SDI = gp * .01;
SS = gp * .06;
netpay = gp - (ftax + stax + SDI + SS);
cout << " grosspay =\t\t\t\t\t" << gp << endl;
cout << " federal taxes =\t\t\t\t" << ftax << endl;
cout << " state taxes =\t\t\t\t\t" << stax << endl;
cout << " SDI =\t\t\t\t\t\t" << SDI << endl;
cout << " Social Securities =\t\t\t\t" << SS << endl;
cout << " netpay =\t\t\t\t\t" << netpay << endl;
cout << "---------------------" << endl;
cout << "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" << endl;
cout << " grosspay = " << gp << endl;
cout << " federal taxes = " << ftax << endl;
cout << " state taxes = " << stax << endl;
cout << " SDI = " << SDI << endl;
cout << " Social Securities = " << SS << endl;
cout << " netpay = " << netpay << endl;
cout << "---------------------" << endl;
cout << "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t" << endl;
cout << left;
cout << setw(30) << " grosspay =" << gp << endl;
cout << setw(30) << " federal taxes =" << ftax << endl;
cout << setw(30) << " state taxes =" << stax << endl;
cout << setw(30) << " SDI =" << SDI << endl;
cout << setw(30) << " Social Securities =" << SS << endl;
cout << setw(30) << " netpay =" << netpay << endl;
cout << "---------------------" << endl;
system("pause");
}

As from your question:
but i cannot get the 2nd if to work, which is the bonus pay if days work is greater than 5
Despite it's not completely clear what you're asking, one problem in your code for sure is that you check for the daysWorked value
if (daysWorked > 5) {
// ...
but you never set it again after initialization
int daysWorked(0);
anywhere I could spot in your code sample.
Thus the value of daysWorked will always be 0, and the code in the if() clauses is ignored.

Change
cin >>dw to cin >>daysWorked

I can see two mistakes here.
You initialised
float dw(0);
and
int daysWorked(0);
There is no reason for you to initialise that way seeing through your code, just leave it at
float dw;
int daysWorked;
Another problem I see in your code is, you are storing the value of days worked into dw as such
cout << "please enter the daysWorked in the week: ";
cin >> dw;
but yet you are checking for daysWorked
if (daysWorked > 5)
and in between there wasnt any code leading up to transferring the value of dw to daysWorked.
My advice is to change both dw and bonusPay from
float dw(0);
float bonusPay(0);
to
float dw;
float bonusPay;
and delete
int daysWorked(0);
lastly
if (hw > 40) {
OvertimePay = (hw - 40) * hp * .5;
if (dw > 5) {
bonusPay = (hw - 40) * hp * .25;
}
}

Related

Visual Studios Issue where only first section is being displayed

I am having some troubles where my code is only displaying the first section I have inputed "Sphere" and is not letting me input any other values when I go to Debug.
The basis of the code is to have the user choose a selction, and be able to input values to find the surface areas and volumes of a shape. Below is my code. (Also I am new to this so if there are any pointers I would greatly appreciate it!)
This is for an exam I have so just some input as to where I am going wrong would be great. I want to learn it not have the whole thing completed for me
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
int main()
{
//Text
cout << "\nHello there! This program will help " << endl;
cout << "\ncalulate the surface areas and the " << endl;
cout << "\nvolumes of the displayed shapes below." << endl;
cout << endl;
cout << "----------------------------------------" << endl;
cout << " A: Sphere " << endl;
cout << " B: Cube " << endl;
cout << " C: Dodecahedron " << endl;
cout << " D: Cylinder " << endl;
cout << " E: Cone " << endl;
cout << "----------------------------------------" << endl;
cout << endl;
//Variables
double Sphere = 0.0,
Cube = 0.0,
Dodecahedron = 0.0,
Cylinder = 0.0,
Cone = 0.0;
float A = (Sphere),
B = (Cube),
C = (Dodecahedron),
D = (Cylinder),
E = (Cone);
//Question
enter code here
cout << "Please select one of the following..." << endl;
cin >> A;
cin >> B;
cin >> C;
cin >> D;
cin >> E;
{
//Sphere
int SphereRadius = 0.0;
double pi = 3.1415926535898;
double SphereSA = (4 * pi * (SphereRadius * SphereRadius));
double SphereV = ((int(4 / 3)) * (pi * (SphereRadius * SphereRadius * SphereRadius)));
if (Sphere);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a radius. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> SphereRadius;
cout << endl;
cout << "\nWith the given radius... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << SphereSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << SphereRadius << endl;
}
{
//Cube
int CubeSide = 0.0;
double CubeSA = (6 * (CubeSide * CubeSide));
double CubeV = (CubeSide * CubeSide * CubeSide);
if (Cube);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a side length. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the side length: " << endl;
cin >> CubeSide;
cout << endl;
cout << "\nWith the given side length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << CubeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << CubeV << endl;
cout << endl;
}
{
//Cone
int ConeH = 0.0;
double ConeR = 0.0;
double ConeSA = (pi * ConeR * (ConeR + sqrt((ConeH * ConeH) + (ConeR * ConeR))));
double ConeV = (pi * ((ConeR * ConeR) * (ConeH / 3)));
if (Cone);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a radius and a height. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> ConeR;
cout << endl;
cout << "\nPlease input a positive number for the height: " << endl;
cin >> ConeH;
cout << endl;
cout << "\nWith the given side length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << ConeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << ConeV << endl;
cout << endl;
}
{
//Dodecahedron
int DodeEdge = 0.0;
double DodeSA = (3 * (sqrt(25 + 10 * sqrt(5) * DodeEdge)));
double DodeV = (DodeEdge * ((15 + (7 * sqrt(5)) / 4)));
if (Dodecahedron);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need to have an edge. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the edge: " << endl;
cin >> DodeEdge;
string DodeEdge;
cout << "\nWith the given edge length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << DodeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << DodeV << endl;
cout << endl;
}
{
//Cylinder
int CylinderH = 0.0;
int CylinderR = 0.0;
double CylinderSA = ((2 * CylinderR * pi * CylinderH) + (2 * pi * (CylinderR * CylinderR)));
double CylinderV = (pi * (CylinderR * CylinderR) * CylinderH);
if (Cylinder);
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need to have a radius and a height. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> CylinderR;
cout << endl;
cout << "\nPlease input a positive number for the height: " << endl;
cin >> CylinderH;
cout << endl;
cout << "\nWith the given radius and height... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << CylinderSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << CylinderV << endl;
cout << endl;
}
return 0;
}
The error is that the code order is not correct:
int SphereRadius = 0.0;
double pi = 3.1415926535898;
double SphereSA = (4 * pi * (SphereRadius * SphereRadius));
double SphereV = ((int(4 / 3)) * (pi * (SphereRadius * SphereRadius * SphereRadius)));
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> SphereRadius;
I modified code by using switch-case:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
#define Sphere 1
#define Cube 2
#define Dodecahedron 3
#define Cylinder 4
#define Cone 5
using namespace std;
double pi = 3.1415926535898;
int main()
{
//Text
cout << "\nHello there! This program will help " << endl;
cout << "\ncalulate the surface areas and the " << endl;
cout << "\nvolumes of the displayed shapes below." << endl;
cout << endl;
cout << "----------------------------------------" << endl;
cout << " 1: Sphere " << endl;
cout << " 2: Cube " << endl;
cout << " 3: Dodecahedron " << endl;
cout << " 4: Cylinder " << endl;
cout << " 5: Cone " << endl;
cout << "----------------------------------------" << endl;
cout << endl;
cout << "Please select one of the following..." << endl;
//Variables
int UserSelect = 0;
cin >> UserSelect;
while (UserSelect < 1 || UserSelect>5)
{
cout << "Please select the right option" << endl;
cin >> UserSelect;
cout << endl;
}
switch (UserSelect)
{
case Sphere:
{
//Sphere
int SphereRadius = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a radius. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> SphereRadius;
cout << endl;
double SphereSA = (4 * pi * (SphereRadius * SphereRadius));
double SphereV = ((int(4 / 3)) * (pi * (SphereRadius * SphereRadius * SphereRadius)));
cout << "\nWith the given radius... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << SphereSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << SphereRadius << endl;
}
break;
case Cube:
{
//Cube
int CubeSide = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a side length. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the side length: " << endl;
cin >> CubeSide;
cout << endl;
double CubeSA = (6 * (CubeSide * CubeSide));
double CubeV = (CubeSide * CubeSide * CubeSide);
cout << "\nWith the given side length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << CubeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << CubeV << endl;
cout << endl;
}
break;
case Dodecahedron:
{
//Dodecahedron
int DodeEdge = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need to have an edge. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the edge: " << endl;
cin >> DodeEdge;
double DodeSA = (3 * (sqrt(25 + 10 * sqrt(5) * DodeEdge)));
double DodeV = (DodeEdge * ((15 + (7 * sqrt(5)) / 4)));
//string DodeEdge;
cout << "\nWith the given edge length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << DodeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << DodeV << endl;
cout << endl;
}
break;
case Cylinder:
{
//Cylinder
int CylinderH = 0.0;
int CylinderR = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need to have a radius and a height. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> CylinderR;
cout << endl;
cout << "\nPlease input a positive number for the height: " << endl;
cin >> CylinderH;
cout << endl;
double CylinderSA = ((2 * CylinderR * pi * CylinderH) + (2 * pi * (CylinderR * CylinderR)));
double CylinderV = (pi * (CylinderR * CylinderR) * CylinderH);
cout << "\nWith the given radius and height... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << CylinderSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << CylinderV << endl;
cout << endl;
}
break;
case Cone:
{
//Cone
int ConeH = 0.0;
double ConeR = 0.0;
cout << "\nIn order to find the surface area and " << endl;
cout << "\nvolume we will need a radius and a height. " << endl;
cout << endl;
cout << "\nPlease input a positive number for the radius: " << endl;
cin >> ConeR;
cout << endl;
cout << "\nPlease input a positive number for the height: " << endl;
cin >> ConeH;
cout << endl;
double ConeSA = (pi * ConeR * (ConeR + sqrt((ConeH * ConeH) + (ConeR * ConeR))));
double ConeV = (pi * ((ConeR * ConeR) * (ConeH / 3)));
cout << "\nWith the given side length... " << endl;
cout << endl;
cout << "\nSurface area will be: " << setprecision(2) << ConeSA << endl;
cout << endl;
cout << "\nVolume will be: " << setprecision(2) << ConeV << endl;
cout << endl;
}
break;
default:
break;
}
return 0;
}

Why is the calculation in the subtotal of this program incorrect?

I am trying to create a C++ program that calculates sales tax for a customer and displays a receipt. For example, if you entered 10 as the first sale amount and the tax rate is 0.0825 it should display the total tax as $0.83. Why does my subtotal and total due at the end of the receipt display $10.82 when it should be $10.83?
//Customer Receipt
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
struct Item_Receipt
{
double item;
double cost;
double tax;
double subtotal;
};
int main()
{
vector <Item_Receipt> Store_Receipt;
Item_Receipt Purchase;
//Variables
const double item_tax = .0825;
double Item_Total = 0.0;
double Tax_Total = 0.0;
double Total_Sales = 0.0;
int numSales = 0;
cout << "First sales amount (Enter a 0 to stop): ";
cin >> Purchase.item;
Purchase.tax = Purchase.item * item_tax;
Purchase.subtotal = Purchase.item + Purchase.tax;
Store_Receipt.push_back(Purchase);
Item_Total += Purchase.item;
Tax_Total += Purchase.tax;
Total_Sales += Purchase.subtotal;
numSales++;
while (Purchase.item > 0.0)
{
cout << "Next sales amount (Enter a 0 to stop): ";
cin >> Purchase.item;
if(Purchase.item > 0.0)
{
Purchase.tax = Purchase.item * item_tax;
Purchase.subtotal = Purchase.item + Purchase.tax;
Store_Receipt.push_back(Purchase);
Item_Total += Purchase.item;
Tax_Total += Purchase.tax;
Total_Sales += Purchase.subtotal;
numSales++;
}
else
cout << endl << "That was the last item being puchased.\nHere is your itemized receipt." << endl << endl;
}
//end while
//Output
cout << "----------------------------------------- " << endl;
cout << "\tReceipt of Purchase" << endl;
cout << "----------------------------------------- " << endl << endl;
cout << fixed << setprecision(2);
cout << setw(10) << "Item Cost" <<
setw(15) << "Item Tax" <<
setw(15) << "Subtotal" << '\n';
cout << "----------------------------------------- " << endl;
for(int x=0;x<numSales;x++)
cout << setw(8) << Store_Receipt[x].item << setw(15) << Store_Receipt[x].tax <<
setw(15) << Store_Receipt[x].subtotal << endl;
cout << "----------------------------------------- " << endl;
cout << setw(10) << "Item Total" <<
setw(15) << "Tax Total" <<
setw(15) << "Total Due" << endl;
cout << setw(8) << Item_Total << setw(15) << Tax_Total <<
setw(15) << Total_Sales << endl;
cout << "----------------------------------------- " << endl;
cout << "\tYou purchased " << numSales << " items." << endl;
cout << "----------------------------------------- " << endl;
cout << "\tThank you! Have a nice day!" << endl;
cout << "----------------------------------------- " << endl;
cin >> numSales;
return 0;
}
setprecision(2) doesn't mean "round to 2 decimal digits," it means "display 2 decimal digits." The actual value is 10.825 but you're only displaying the first two decimal digits.
If you want to round away from the midpoint, you need to use one of the rounding functions on the result.
Since you want to round to the second decimal place, you have to first multiply the number by 100, then round it, then divide by 100. You could do this with the help of a function:
double round_to_cents(double v) {
return std::round(v * 100) / 100;
}
Then round the tax calculation:
Purchase.tax = round_to_cents(Purchase.item * item_tax);
(Demo)

Adding Overtime Pay

the program works, but i'm not sure how to get the grosspay to add the overtime pay once the if condition is set, I was told to set overtime pay to zero on the declaration. Is there a way to change the overtime pay accordingly once the if condition is met? for example
overtimepay= 50
so the formula for gross pay will now be hw*hp+ 50
#include<iostream>
using namespace std;
int main()
{
float ftax, stax, SDI, SS, hw, hp, pay, netpay, gp, OvertimePay = 0;
cout << "please enter the hoursWorked: ";
cin >> hw;
cout << "---------------------" << endl;
cout << "please enter the hourlyPay: ";
cin >> hp;
gp = hw * hp + (OvertimePay);
ftax = gp*.10;
stax = gp*.08;
SDI = gp*.01;
SS = gp*.06;
netpay = gp - (ftax + stax + SDI + SS);
cout << " grosspay = " << gp << endl;
cout << "---------------------" << endl;
cout << " federal taxes = " << ftax << endl;
cout << "---------------------" << endl;
cout << " state taxes = " << stax << endl;
cout << "---------------------" << endl;
cout << " SDI = " << SDI << endl;
cout << "---------------------" << endl;
cout << " Social Securities = " << SS << endl;
cout << "---------------------" << endl;
cout << " netpay = " << netpay << endl;
cout << "---------------------" << endl;
if(hw > 40)
cout << "OvertimePay = " << (hw - 40) * hp * 0.5 << endl;
system("pause");
}
This is one way to do it. You weren't ever actually setting the OvertimePay variable to equal anything other than 0. You should move the if condition up in the program logic and then set the variable accordingly before calculating gross pay (gp).
#include<iostream>
using namespace std;
int main()
{
float ftax, stax, SDI, SS, hw, hp, pay, netpay, gp, OvertimePay = 0;
cout << "please enter the hoursWorked: ";
cin >> hw;
cout << "---------------------" << endl;
cout << "please enter the hourlyPay: ";
cin >> hp;
if(hw > 40) {
OvertimePay = (hw - 40) * hp * .5;
} else {
OvertimePay = 0;
}
gp = (hw * hp) + OvertimePay;
ftax = gp*.10;
stax = gp*.08;
SDI = gp*.01;
SS = gp*.06;
netpay = gp - (ftax + stax + SDI + SS);
cout << " grosspay = " << gp << endl;
cout << "---------------------" << endl;
cout << " federal taxes = " << ftax << endl;
cout << "---------------------" << endl;
cout << " state taxes = " << stax << endl;
cout << "---------------------" << endl;
cout << " SDI = " << SDI << endl;
cout << "---------------------" << endl;
cout << " Social Securities = " << SS << endl;
cout << "---------------------" << endl;
cout << " netpay = " << netpay << endl;
cout << "---------------------" << endl;
}
You need to calculate the overtime pay first, before outputting the gross pay:
if(hw > 40)
OvertimePay = (hw - 40) * hp * 0.5;
gp = hw * hp + OvertimePay;

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 .

Variable not changing after its modified in C++

I am trying to run balN with the variable N changing as I change it in the program but balN is only running it with the value that was assigned with N at the very beginning. How can I make it so balN will change as the value of N changes? I am new to programming so any help will be appreciated. Thanks!
const double Monintrate = 0.09 / 12.0;
const double Totnum = 36.0;
const double Payment = 165.25;
int main ()
{
double N = 1.0;
double balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << fixed;
cout << setprecision(2) << "Monthly Payment: $" << Payment << endl;
cout << "Annual Interest rate: 9%" << endl;
cout << "Total Number of Payments: " << Totnum << endl << endl;
cout << "Balance after Payment:" << endl;
cout << "1: $" << balN << endl;
N++;
cout << "2: $" << balN << endl;
N++;
cout << "3: $" << balN << endl;
}
Because in your program, you only modified the value of N, not balN, you need to call
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
every time you modified N.
cout << "1: $" << balN << endl;
N++;
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << "2: $" << balN << endl;
N++;
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << "3: $" << balN << endl;
You never reassign balN with the new values depending on N.
You should reassign it every time you change N:
cout << "1: $" << balN << endl;
N++;
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << "2: $" << balN << endl;
N++;
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << "3: $" << balN << endl;
Maybe a loop here can be a solution to avoid code repetition:
for ( int i = 1; i <= 3; i++ )
{
balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
cout << i << ": $" << balN << endl;
N++;
}
Or Use a function to do that:
double calcBalN( double N )
{
return Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
}
int main ()
{
// ...
cout << "1: $" << calcBalN( N ) << endl;
N++;
cout << "2: $" << calcBalN( N ) << endl;
N++;
cout << "3: $" << calcBalN( N ) << endl;
//...
}
double balN = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
The expression at the right side of the assignment will be evaluated and the result will be store in balN.
The value of balN won't be modified if you don't reassign a new value to it, even if the value of N is modified.
What you could do is define your calculation in a function and update balN with it :
double process(double N) {
const double Monintrate = 0.09 / 12.0;
const double Totnum = 36.0;
const double Payment = 165.25;
return Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;;
}
int main ()
{
double N = 1.0;
double balN = process(N);
cout << fixed;
cout << setprecision(2) << "Monthly Payment: $" << Payment << endl;
cout << "Annual Interest rate: 9%" << endl;
cout << "Total Number of Payments: " << Totnum << endl << endl;
cout << "Balance after Payment:" << endl;
cout << "1: $" << balN << endl;
N++;
balN = process(N);
cout << "2: $" << balN << endl;
N++;
balN = process(N);
cout << "3: $" << balN << endl;
}
Make balN a function. C language does not support reactive programming otherwise you could have benefitted with your code. Following code will help.
const double Monintrate = 0.09 / 12.0;
const double Totnum = 36.0;
const double Payment = 165.25;
double balN (double N)·
{
double bal = Payment * (1 - pow(1 + Monintrate, N - Totnum))/Monintrate;
return bal;·
}
int main ()
{
double N = 1.0;
cout << fixed;
cout << setprecision(2) << "Monthly Payment: $" << Payment << endl;
cout << "Annual Interest rate: 9%" << endl;
cout << "Total Number of Payments: " << Totnum << endl << endl;
cout << "Balance after Payment:" << endl;
cout << "1: $" << balN() << endl;
N++;
cout << "2: $" << balN() << endl;
N++;
cout << "3: $" << balN() << endl;
}