Calculations no processing C++ - c++

I'm making a simple payroll calculator and for some reason my "taxes" aren't calculating. When I run the program they just show $0 in the output. Any idea whats causing this? I commented "// Not calculating. Shows $0" on the lines that aren't calculating.
#include <iostream>
using namespace std;
int main()
// Parameters: None
// Returns: Zero
// Calls: None
{
int const hourly_wage = 16.78, overtime_rate = 25.17, social_security = 0.06, federal_income = 0.14, state_income = 0.05, union_dues = 10;
int gross_pay, hours_worked, number_of_dependents, ss_withheld, federal_withheld, state_withheld, net_pay, insurance, overtime_pay, again;
cout << "This program will calculate payroll for an employee." << endl;
do
{
cout << "\n\nEnter the number of hours the employee worked: " << endl;
cin >> hours_worked;
if (hours_worked > 40) {
overtime_pay = (hours_worked - 40) * overtime_rate;
gross_pay = (40 * hourly_wage) + overtime_pay;
}
else {
gross_pay = hours_worked * hourly_wage;
}
cout << "\n\nEnter the number of dependents for employee: " << endl;
cin >> number_of_dependents;
if (number_of_dependents >= 3) {
insurance = 35;
}
else {
insurance = 0;
}
// Payroll Calculation
ss_withheld = gross_pay * social_security;
federal_withheld = gross_pay * federal_income;
state_withheld = gross_pay * state_income;
net_pay = gross_pay - (ss_withheld + federal_withheld + state_withheld + insurance + union_dues);
cout << "\n\nGross Pay: $" << gross_pay << endl;
cout << "Social Security Withhholding: $" << ss_withheld << endl; // Not calculating. Shows $0
cout << "Federal Withholding: $" << federal_withheld << endl; // Not calculating. Shows $0
cout << "State Withholding: $" << state_withheld << endl; // Not calculating. Shows $0
cout << "Union Dues: $" << union_dues << endl;
cout << "Insurance Deduction: $" << insurance << endl;
cout << "Net Pay: $" << net_pay << endl;
cout << "\nDo you want to do another employee(Y/N)? ";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\nHope they enjoy the money!" << endl;
return 0;
}

It looks like you are creating int Const for double values. ie.
#include "stdafx.h";
#include <iostream>
using namespace std;
int main()
// Parameters: None
// Returns: Zero
// Calls: None
{
int const union_dues = 10;
double const hourly_wage = 16.78, overtime_rate = 25.17, social_security = 0.06, federal_income = 0.14, state_income = 0.05;
double gross_pay, hours_worked, number_of_dependents, ss_withheld, federal_withheld, state_withheld, net_pay, insurance, overtime_pay, again;
cout << "This program will calculate payroll for an employee." << endl;
do {
cout << "\n\nEnter the number of hours the employee worked: " << endl;
cin >> hours_worked;
if (hours_worked > 40) {
overtime_pay = (hours_worked - 40) * overtime_rate;
gross_pay = (40 * hourly_wage) + overtime_pay;
}
else {
gross_pay = hours_worked * hourly_wage;
}
cout << "\n\nEnter the number of dependents for employee: " << endl;
cin >> number_of_dependents;
if (number_of_dependents >= 3) {
insurance = 35;
}
else {
insurance = 0;
}
// Payroll Calculation
ss_withheld = gross_pay * social_security;
federal_withheld = gross_pay * federal_income;
state_withheld = gross_pay * state_income;
net_pay = gross_pay - (ss_withheld + federal_withheld + state_withheld + insurance + union_dues);
cout << "\n\nGross Pay: $" << gross_pay << endl;
cout << "Social Security Withhholding: $" << ss_withheld << endl; // Not calculating. Shows $0
cout << "Federal Withholding: $" << federal_withheld << endl; // Not calculating. Shows $0
cout << "State Withholding: $" << state_withheld << endl; // Not calculating. Shows $0
cout << "Union Dues: $" << union_dues << endl;
cout << "Insurance Deduction: $" << insurance << endl;
cout << "Net Pay: $" << net_pay << endl;
cout << "\nDo you want to do another employee(Y/N)? ";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\nHope they enjoy the money!" << endl;
return 0;
}

Related

How to update a while loop with multiple if statements?

I am working on the "checkout" process of my vending machine code. I want to write it so that the program will keep asking for the amount needed from the user until all the money is entered. However, this code segment does not completely work.
"Checkout" Segment of Code:
while (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: $" << flush;
float payment;
cin >> payment;
}
if (money > total) {
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
Full code below:
#include <iostream>
#include <iomanip>
using namespace std;
string menuItems[5] = { "Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate" };
float cost[5] = { 2, 3, 2.50, 1.50, 1 };
void vendingMachine() {
for (int i = 0; i < 5; i++)
cout << i + 1 << ". " << menuItems[i] << ": $" << cost[i] << endl;
}
int main() {
cout.precision(2);
cout << std::fixed;
cout << "Vending Machine" << endl;
cout << "----Items------" << endl;
vendingMachine();
cout << "Enter 0 to checkout" << endl;
float total;
total = 0;
int item;
do {
cout << "Enter your selection: " << flush;
cin >> item;
item = item - 1;
//here will be printed : $0 has been added to cart even if you pressed 0 and what to escape
//is it possible to fix this??
cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
total = total + cost[item];
} while (item != -1);
cout << " " << endl;
cout << "Proceding to checkout..." << endl;
cout << "========================" << endl;
cout << "Amount due: $" << total << endl;
cout << "Insert money here: $" << flush;
float money;
cin >> money;
while (money < total) {
float amountOwed = total - money;
cout << "Please insert another $" << amountOwed << endl;
cout << "Enter amount: $" << flush;
float payment;
cin >> payment;
}
if (money > total) {
float change = money - total;
cout << "Thank you! You have $" << change << " change." << endl;
}
if (money == total) {
cout << "Thank you! Have a nice day!." << endl;
}
return 0;
}
In this loop:
while (money < total) {
you are not modifying money or total so the loop will never exit.
You probably want to update money like this:
while (money < total) {
// ...
cin >> payment;
money += payment;
}

How to repeat the whole process for another customer until user stop the program?

When the user enters is prompt with the "Continue with another customer (Y/N)?" They should be asked to press Y to repeat the program with a different customer, or N to end the program entirely. I'm not sure where to put the do-while statement in the code for. I have tried to put if statments, that doesnt work. When doing the do while statements the "Welcome to pizza world" text comes up, but it does not repeat the code, it will just end the program normally.
#include <iostream>
using namespace std;
int main() {
int pizzas, drinks;
char drink_type, more;
double order = 0.0;
int count = 0, total_sodas = 0, total_milkshake = 0, total_pizzas = 0;
//This will set the precision of the decimal output 2 decimal places.
cout.setf(ios::showpoint);
cout.precision(2);
cout.setf(ios::fixed);
cout << "Welcome to Pizza World" << endl;
do{
count++;
cout << "\n\nHow many pizzas would you like?";
cin >> pizzas;
while (pizzas < 0)
{
cout << "That's an invalid number of pizzas.Try again:";
cin >> pizzas;
}
cout << "How many drinks would you like?";
cin >> drinks;
while (drinks < 0)
{
cout << "That's an invalid number of drinks.Try again:";
cin >> drinks;
}
if (drinks > 0) {
cout << "You chose to order " << drinks << " drinks. Enter 'S' for soda and 'M' for milkshake:";
cin >> drink_type;
switch (drink_type)
{
case 'S':
case 's':
order = order + 1.95 * drinks;
total_sodas = total_sodas + drinks;
break;
case 'M':
case 'm':
order = order + 4.25 * drinks;
total_milkshake = total_milkshake + drinks;
break;
}
}
order = order + 12.49 * pizzas;
total_pizzas = total_pizzas + pizzas;
order = order + 0.06 * order;
cout << "\n\nThe current order total including tax is $" << order << endl << endl;
cout << "Would you like to place another order(Y/N)?";
cin >> more;
// Resetting the order.
order = 0.0;
}while (more == 'Y' || more == 'y');
double subtotal = 0.0, tax;
cout << "\n##########################" << endl;
cout << "Your itemized bill for " << count << " orders:" << endl;
// Showing the calculations of every item only if its count is more than zero
if (total_pizzas > 0)
{
subtotal = subtotal + total_pizzas * 12.49;
cout << "Pizzas: " << total_pizzas << " x $12.49 = $" << total_pizzas * 12.49 << endl;
}
if (total_sodas > 0)
{
subtotal = subtotal + total_sodas * 1.95;
cout << "Sodas: " << total_sodas << " x $1.95 = $" << total_sodas * 1.95 << endl;
}
if (total_milkshake > 0)
{
subtotal = subtotal + total_milkshake * 4.25;
cout << "Sodas: " << total_milkshake << " x $4.25 = $" << total_milkshake * 4.25 << endl;
}
cout << "-------------------------------" << endl;
cout << "Subtotal = $" << subtotal << endl;
tax = subtotal * 0.06;
cout << "Tax = $" << tax << endl;
cout << "Total = $" << (subtotal + tax) << endl;
do {
cout << "Continue with another customer (Y/N)? \n";
cin >> more;
cout << "Welcome to Pizza World" << endl;
if (more == 'Y' || more == 'y')break;
} while (1);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
int main() {
int pizzas, drinks;
char drink_type, more;
double order = 0.0;
int count = 0, total_sodas = 0, total_milkshake = 0, total_pizzas = 0;
//This will set the precision of the decimal output 2 decimal places.
cout.setf(ios::showpoint);
cout.precision(2);
cout.setf(ios::fixed);
cout << "Welcome to Pizza World" << endl;
int ch=1;
while(ch){
do{
count++;
cout << "\n\nHow many pizzas would you like?";
cin >> pizzas;
while (pizzas < 0)
{
cout << "That's an invalid number of pizzas.Try again:";
cin >> pizzas;
}
cout << "How many drinks would you like?";
cin >> drinks;
while (drinks < 0)
{
cout << "That's an invalid number of drinks.Try again:";
cin >> drinks;
}
if (drinks > 0) {
cout << "You chose to order " << drinks << " drinks. Enter 'S' for soda and 'M' for milkshake:";
cin >> drink_type;
switch (drink_type)
{
case 'S':
case 's':
order = order + 1.95 * drinks;
total_sodas = total_sodas + drinks;
break;
case 'M':
case 'm':
order = order + 4.25 * drinks;
total_milkshake = total_milkshake + drinks;
break;
}
}
order = order + 12.49 * pizzas;
total_pizzas = total_pizzas + pizzas;
order = order + 0.06 * order;
cout << "\n\nThe current order total including tax is $" << order << endl << endl;
cout << "Would you like to place another order(Y/N)?";
cin >> more;
// Resetting the order.
order = 0.0;
}while (more == 'Y' || more == 'y');
double subtotal = 0.0, tax;
cout << "\n##########################" << endl;
cout << "Your itemized bill for " << count << " orders:" << endl;
// Showing the calculations of every item only if its count is more than zero
if (total_pizzas > 0)
{
subtotal = subtotal + total_pizzas * 12.49;
cout << "Pizzas: " << total_pizzas << " x $12.49 = $" << total_pizzas * 12.49 << endl;
}
if (total_sodas > 0)
{
subtotal = subtotal + total_sodas * 1.95;
cout << "Sodas: " << total_sodas << " x $1.95 = $" << total_sodas * 1.95 << endl;
}
if (total_milkshake > 0)
{
subtotal = subtotal + total_milkshake * 4.25;
cout << "Sodas: " << total_milkshake << " x $4.25 = $" << total_milkshake * 4.25 << endl;
}
cout << "-------------------------------" << endl;
cout << "Subtotal = $" << subtotal << endl;
tax = subtotal * 0.06;
cout << "Tax = $" << tax << endl;
cout << "Total = $" << (subtotal + tax) << endl;
cout << "Continue with another customer (1/0)? \n";
cin >> ch;
}
system("pause");
return 0;
}
Ive actually removed this piece of code:
do {
cout << "Continue with another customer (Y/N)? \n";
cin >> more;
cout << "Welcome to Pizza World" << endl;
if (more == 'Y' || more == 'y')break;
} while (1);
and placed the entire do while loop inside a while that prompts if you want to continue to next customer.

Code WORKS Math DOESN'T

My code compiles nicely, but the math formulas that I am using aren't providing the right outcome. I need to calculate the balance, withdrawn, and interest for all 3 months. I am also required to validate user's input. For these purposes I am using nested loops. Please let me know if you spot my mistake. Thank you lovely people!
cout << "Please enter the starting balance: ";
cin >> startBalance;
cout << "Please enter the annual interest rate: ";
cin >> annualInterest;
for (int month = 1; month <= 3; month++) {
do {
cout << setprecision(5);
cout << "Please enter the total amount deposited on month " << month << ": ";
cin >> balance;
if (balance <0) {
goodChoice = false;
cout << "\n\t***ERROR " << balance << " ***";
cout << "*** Choice must be positive***\n" << endl;
}
else {
goodChoice = true;
}
startBalance += balance; //need to calculate the balance for all 3 months
} while (!goodChoice);
do {
cout << setprecision(5);
cout << "Please enter the total amount withdrawn on " << month << ": ";
cin >> withdrawn;
if ( (withdrawn <0) || (withdrawn > startBalance) ) {
goodChoice = false;
cout << "***ERROR " << withdrawn << " ***";
cout << "*** Choice must be positive or greater than the balance***" << endl;
}
else {
goodChoice = true;
}
finalWithdrawn += withdrawn; // the total amount of withdrawn
finalBalance = startBalance - withdrawn;
monthInterest = ((startBalance + finalBalance) / 2) * (annualInterest / 12);
totalInterest += monthInterest; //total interest for all 3 months
endBalance = monthInterest + finalBalance;
} while (!goodChoice);
}
cout << "Total Deposit: " << endBalance << endl;
cout << "Total Withdrawn: " << finalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << endBalance << endl;
You have a lot of extra variables defined which aren't needed. Also, your interest rate may have been in percentage instead of a decimal number, i.e. 10% = 0.1. Also, your monthInterest is taking an average then applying interest. I wrote this up and it seems to work.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float totalDeposit = 0.0f;
float totalWithdrawn = 0.0f;
float totalInterest = 0.0f;
float totalBalance = 0.0f;
float interestRate = 0.0f;
setprecision(5); //?
cout << "Enter starting balance: ";
cin >> totalBalance;
cout << "Enter annual interest rate (%): ";
cin >> interestRate;
// Convert to monthly and non-percent; i.e. 10% = 0.1 = 10 / 100
interestRate = interestRate / 100.0f / 12.0f;
for (int month = 1; month <= 3; month++)
{
float deposit = -1.0; // Default to an error state
while (deposit < 0.0)
{
cout << "Enter total deposited in month " << month << ": ";
cin >> deposit;
if (deposit < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
float withdrawn = -1.0f; // Default to an error state
while (withdrawn < 0.0f)
{
cout << "Enter total withdrawn in month " << month << ": ";
cin >> withdrawn;
if (withdrawn < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
totalDeposit += deposit;
totalWithdrawn += withdrawn;
totalBalance = totalBalance + deposit - withdrawn;
totalBalance += totalBalance * interestRate;
totalInterest += totalBalance * interestRate;
}
cout << "Total Deposit: " << totalDeposit << endl;
cout << "Total Withdrawn: " << totalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << totalBalance << endl;
int wait; // Pause so console window doesn't close. Delete this line.
cin >> wait;
return 0;
}

Comparing Cell Phone plans c++

I'm new here. Sorry if I'm not posting correctly. My program is comparing cell phone plans based on minutes. I want to know how I can compare plans to determine the best plan for the money. I believe I should be using minimum functions for this, but I'm honestly stuck. My code is below. Is it possible to find the minimum value from 3 separate functions? I'm not looking for an answer, but maybe an example or an article how to do so. Thanks!
#include<iostream>
using namespace std;
void companyA();
void companyB();
void CompanyC();
int min(); // I want to use this to find my minimum
int numEmployees;
int avgMin;
double totalCost;
// double bestChoice; Not used yet
int main()
{
double newMin;
cout << "Please enter number of employees." << endl;
cin >> numEmployees;
cout << "Please enter average minutes used by each employee. " << endl;
cin >> avgMin;
if (numEmployees < 0)
{
cout << "Incorrect value. Please enter positive number of employees." << endl;
cin >> numEmployees;
}
else if (avgMin < 0)
{
cout << "Incorrect value. Please enter positive number of minutes. " << endl;
cin >> avgMin;
}
cout << "\nStandard Packages" << endl;
cout << "Company A: For $29.99 per month, 450 minutes are included. Additional minutes are $0.35 per minute." << endl;
cout << "Company B: For $49.99 per month, 900 minutes are provided. Additional minutes are $0.30 per minute." << endl;
cout << "Company C: For $59.99 per month, unlimited minutes are provided." << endl;
companyA();
companyB();
companyC();
cout << "\nBased on the number of employees and average minutes used, " << x << "is the best choice." << endl;
system("pause");
return 0;
}
void companyA()
{
if (avgMin <= 450)
{
totalCost = 29.99*numEmployees;
cout << "\nCompany A will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 450)
{
totalCost = (avgMin-450)*0.35+29.99;
cout << "\nCompany A will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
}
void companyB()
{
if (avgMin <= 900)
{
totalCost = 49.99*numEmployees;
cout << "Company B will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 900)
{
totalCost = (avgMin - 900)*0.30 + 49.99;
cout << "Company B will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
}
void companyC()
{
totalCost = 59.99*numEmployees;
cout << "Company C will cost an $" << totalCost << " a month for " << numEmployees << " employee(s)." << endl;
}
I don't know if this is what you're looking for.
void minimum(double numEmployees, double avgMin);
double minimumCompany(double x, double y, double z);
void minimum(double numEmployees, double avgMin)
{
double totalCostA, totalCostB, totalCostC, minimumTotal;
string choice;
if (avgMin <= 450)
{
totalCostA = 29.99 * numEmployees;
cout << "\nCompany A will cost an $" << totalCostA << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 450)
{
totalCostA = (avgMin - 450) * 0.35 + 29.99;
cout << "\nCompany A will cost an $" << totalCostA << " a month for " << numEmployees << " employee(s)." << endl;
}
if (avgMin <= 900)
{
totalCostB = 49.99*numEmployees;
cout << "Company B will cost an $" << totalCostB << " a month for " << numEmployees << " employee(s)." << endl;
}
else if (avgMin > 900)
{
totalCostB = (avgMin - 900)*0.30 + 49.99;
cout << "Company B will cost an $" << totalCostB << " a month for " << numEmployees << " employee(s)." << endl;
}
totalCostC = 59.99*numEmployees;
cout << "Company C will cost an $" << totalCostC << " a month for " << numEmployees << " employee(s)." << endl;
minimumTotal = minimumCompany(totalCostA, totalCostB, totalCostC);
if (minimumTotal == totalCostA)
{
choice = "Company A";
}
else if (minimumTotal == totalCostB)
{
choice = "Company B";
}
else
{
choice = "Company C";
}
cout << "\nBased on the number of employees and average minutes used, " << choice << " is the best choice." << endl;
}
double minimumCompany(double x, double y, double z)
{
double minimum = x;
if (y < minimum ) {
minimum = y;
}
if (z < minimum) {
minimum = z;
}
return minimum;
}
int main()
{
double numEmps, averageMin;
bool isValid = true;
while (isValid)
{
cout << "Please enter number of employees." << endl;
cin >> numEmps;
cout << "Please enter average minutes used by each employee. " << endl;
cin >> averageMin;
if ((numEmps < 0))
{
cout << "Please enter number of employees. " << endl;
cin >> numEmps;
isValid = true;
}
else
{
isValid = false;
}
if ((averageMin < 0))
{
cout << "Please enter average minutes used by each employee. " << endl;
cin >> numEmps;
isValid = true;
}
else
{
isValid = false;
}
}
cout << "\nStandard Packages" << endl;
cout << "Company A: For $29.99 per month, 450 minutes are included. Additional minutes are $0.35 per minute." << endl;
cout << "Company B: For $49.99 per month, 900 minutes are provided. Additional minutes are $0.30 per minute." << endl;
cout << "Company C: For $59.99 per month, unlimited minutes are provided." << endl;
minimum(numEmps, averageMin);
system("pause");
return 0;
}

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;