This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
enter code here
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
double employeeNum = 0.0; double totalEmployees = 0.0;
double hourlyRate = 0.0; double totalhoursWork = 0.0;
double hoursWork = 0.0; double totalnetPay = 0.0;
double grossPay = 0.0; double averagehoursWork = 0.0;
double netPay = 0.0; double totalwithHoldings = 0.0;
double withHoldings = 0.0;
cout <<" Enter Employee Number or 9999 to Stop:";
cin >> employeeNum;
cout <<"Enter hourly rate:";
cin >> hourlyRate;
cout <<"Enter hours worked:";
cin >> hoursWork;
while(employeeNum != 9999)
{
if (hoursWork >= 40)
{
grossPay= hoursWork * hourlyRate;
cout <<" Gross Weekly Pay:" << grossPay << endl;
}
else (hoursWork > 40);
{
grossPay= hoursWork * hourlyRate*1.5;
cout <<" Gross Weekly Pay:" << grossPay << endl;
}
while( grossPay > 1,000.00)
{
withHoldings= grossPay/ 0.28;
}
while( grossPay < 1,000.0)
{
withHoldings= grossPay/ 0.21;
}
netPay= grossPay-withHoldings;
cout <<" Net Weekly Pay:" << netPay << endl;
cout <<" Enter Employee Number or 9999 to Stop:";
cin >> employeeNum;
cout <<"Enter hourly rate:";
cin >> hourlyRate;
cout <<"Enter hours worked:";
cin >> hoursWork;
}
system("pause");
return 0;
}
Starting with a blank solution, write a program to prompt the user for an employee number, hourly rate and hours worked. Compute and display the employee number, gross weekly pay and net weekly pay. Gross weekly pay is calculated as hours worked times rate for the first 40 hours, plus hours times 1.5 the rate for any hours over 40. Net pay is gross minus withholdings. Withholdings is calculated as 28 percent of gross pay if gross pay is over $1,000, 21 percent of gross pay if gross pay is $1000 or less. All input from the user should be verified as valid. Hourly rate must be greater than $7.25 and less than $100.00. Hours worked must be greater than 0 and less than 120. If the user enters invalid data display and appropriate error message and ask the user to re-enter. Use a post test loop to repeat this process until the user enters 9999 the program should display a total number of employees entered, total hours worked, average hours worked, total net pay and total withholdings. All numeric output should display in fixed notation with two decimal places
while( grossPay > 1000.00)
{
withHoldings= grossPay/ 0.28;
}
Either the condition is true, then is stays true and the loop will loop forever, or it isn't, and the statement is never executed.
Change while to if or modify grossPay inside the loop.
Also, don't include a comma in a floating point constant.
Your loops are not executing because you have a ',' in your floating point constant. use 1000.00 instead of 1,000.00
You have the second condition duplicating the first one ((hoursWork > 40);) and there is a semicolon at the end of the condition. Please check the syntax carefully before posting here.
Related
This question already has an answer here:
Dividing two integers to produce a float result [duplicate]
(1 answer)
Closed 1 year ago.
Here i want to calculate the number of sweets that i have been sold in Day 2.
int main()
{
float rate;
int left, total, sell ;
cout << "rate" ; // the rate that sweets have been sold per day
cin >> rate;
cout << "sell"; // number of sweets that have been sold in day1.
cin >> sell;
cout << "total"; // total number that the shops originally have
cin >> total;
left = total - sell;
sell = sell*(1+rate*(left/total));
cout << sell;
return 0;
}
And i want to calculate the numbers of sweets that have been sold in day2.
Assume the input are rate = 0.5, sell=100, total = 10000.
The output should be 14950. [ 10000 * (1+0.5*((10000-100)/10000)) ]
But why I still get 10000 after running the program.
Updated:
However, I have one more question. If i want to output a rounded up sell value. How could I do it? because i am not allowed to change the datatype of the variables, which means it should be still int sell; at the initialization part. I have tried cout << round((double) sell); But it still does not work.
As mentioned in the comment, you are doing an integer division. Here is the corrected code:-
#include<iostream>
using namespace std;
int main()
{
float rate;
int left, total, sell ;
cout << "rate" ; // the rate that sweets have been sold per day
cin >> rate;
cout << "sell"; // number of sweets that have been sold in day1.
cin >> sell;
cout << "total"; // total number that the shops originally have
cin >> total;
left = total - sell;
sell = sell*(1+rate*(left/(float)total));
cout << sell;
return 0;
}
I have typecasted the denominator.
The question wants me to create 2 scenarios
The user accepts the dealer’s rebate offer and finances the car through his or her local credit union.
and
The user declines the dealer’s rebate offer but accepts the dealer’s lower financing rate.
It expects for me to use the periodic payment formula, which is: principal * rate / (1 – (rate + 1)-term) and use it to get either a monthly or annual payment.
The problem that I am having with my code I believe has something to do with my equations I am using to get annual or monthly loan payments, it for sure is not giving me the correct answer to my inputs and I do not know why.
I have tried changing the equations around several times and still no avail.
int main()
// these are the variables I will be using in maths for this project
double annualpayment; // what will be displayed once all is calculated annually
double monthlypayment; // what will be displayed once all is calculated monthly
double prinicple; // amount borrowed
double rate; // interest rate
double mterm; // what the user will enter for monthly term
double yterm; // what user will enter for yearly term
double years; // term of loan (yearly)
double month; // term of loan (monthly)
double sqrdMonth; // sqrt of term of loan (monthly)
double sqrdYear; // sqrt of term of loan (yearly)
char choice;
}
{
cout << "Enter your principle: " << endl; // total amount borrowing
cin >> prinicple;
cout << "Enter your your interest rate: " << endl; // interest rate on loan
cin >> rate;
cout << "Will this be (M)onthly or (Y)early payment? (enter y or m)"; // declaring if it will be a monthly or yearly payment
cin >> choice;
if (choice = 'M') // if its monthly
mterm = 12; // there are 12 months within a year
cout << "How many years will this loan be for?" << endl;
cin >> years; // I need this information for getting the exact
month = mterm * years;
sqrdMonth = sqrt(month); // I need to square root the months for the periodic payment formula
monthlypayment = (prinicple * rate) / (rate); sqrdMonth; // this is where my problem is
// ^^^^ why is it asking me to close my equation with a ';'
cout << "Your monthly loan payment is: ";
cout << monthlypayment;
if (choice = 'Y')
yterm = 1;
cout << "How many years will this loan be for?" << endl;
cin >> years;
years = yterm * years;
sqrdYear = sqrt(years); // I need to square root the years for the periodic payment formula
annualpayment = (prinicple * rate) / (rate); sqrdYear; // this is where my problem is
// ^^^^ why is it asking me to close my equation with a ';'
cout << "Your annual loan payment is: ";
cout << annualpayment;
}
}
I expect for the user to input the principle, rate, and length of loan then the compiler to do the math and then output the correct numbers. My actual results are negative numbers or irrational numbers.
Several mistakes
if (choice = 'M') // if its monthly
mterm = 12; // there are 12 months within a year
First point is that should say
if (choice == 'M') // if its monthly
mterm = 12; // there are 12 months within a year
In C++ we use == to test for equality and = to assign to a variable.
Even more seriously think about this
if (choice == 'M') // if its monthly
mterm = 12; // there are 12 months within a year
cout << "How many years will this loan be for?" << endl;
cin >> years; // I need this information for getting the exact
month = mterm * years;
Now suppose choice is not 'M' what do you think the value of mterm will be?
The answer is that it is undefined. Yet you are using the variable in the formula two lines down. It's bad to use variables with undefined values.
It looks to me that you need to restructure your code to include more statements inside of the if statement
if (choice == 'M')
{
mterm = 12; // there are 12 months within a year
cout << "How many years will this loan be for?" << endl;
cin >> years; // I need this information for getting the exact
month = mterm * years;
sqrdMonth = sqrt(month); // I need to square root the months for the periodic payment formula
monthlypayment = (prinicple * rate) / (rate); sqrdMonth; // this is where my problem is
// ^^^^ why is it asking me to close my equation with a ';'
cout << "Your monthly loan payment is: ";
cout << monthlypayment;
}
Finally this
monthlypayment = (prinicple * rate) / (rate); sqrdMonth;
I've no idea why you've got two semi-colons. Makes no sense to me, but I not sure what the formula should be. There's no mention of square roots in the formula in your question, so I'm not sure why you included one here.
If a person has low income (<= 25,000) and the consulting time is less than or equal to 30 minutes, there are no charges; otherwise, the service charges are 40% of the regular hourly rate for the time over 30 minutes.
For others, if the consulting time is less than or equal to 20 minutes, there are no service charges; otherwise, service charges are 70% of the regular hourly rate for the time over 20 minutes.
(For example, suppose that a person has low income and spent 1 hour and 15 minutes, and the hourly rate is $70.00. Then the billing amount is 70.00 * 0.40 * (45 / 60) = $21.00.)
Write a program in C++ that prompts the user to enter the hourly rate, the total consulting time, and whether the person has low income. The program should output the billing amount.
This is what I have done so far and the program looks correct to me, but for some reason I do not get the correct output. I get 0.00 which is a wrong output value.
#include<iostream>
#include <iomanip>
using namespace std;
double calculateBill(int income, int consultingMinutes, double hourlyRate);
int main()
{
int income;
double consultingMinutes;
double hourlyRate;
cout << "Please enter the clients income: $" ;
cin >> income;
cout << "Please enter the consulting time in minutes: ";
cin >> consultingMinutes;
cout << "Please enter the hourly rate: $";
cin >> hourlyRate; cout << fixed << showpoint << setprecision(2);
cout << "Your total bill ammount comes to: $" << calculateBill(income, consultingMinutes, hourlyRate) << endl;
return 0;
}
double calculateBill(int income, int consultingMinutes, double hourlyRate)
{
if (income <= 25000) {
if (consultingMinutes <= 30)
return 0;
else
return hourlyRate * 0.40 * ((consultingMinutes - 30) / 60);
}
else {
if (consultingMinutes <= 20)
return 0;
else
return hourlyRate * 0.70 * ((consultingMinutes - 20) / 60);
}
}
Mixing of integers and floats is not a very good thing. Try this:
return hourlyRate * 0.40 * (((double) consultingMinutes - 30.0) / 60.0);
instead of this:
return hourlyRate * 0.40 * ((consultingMinutes - 30) / 60);
(And apply the same fix for the second wrong place return hourlyRate * 0.70 * ((consultingMinutes - 20) / 60);)
In a population, the birth rate is the percentage increase of the population due to births, and the death rate is the percentage decrease of the population due to deaths. Write a program that asks for the following:
The starting size of a population (minimum 2) (Prompt Enter starting size:)
The annual birth rate (Prompt Enter annual birth rate:)
The annual death rate (Prompt Enter annual death rate:)
The number of years to display (minimum 1) (Prompt Enter years to display:)
The program should then display the starting population and the projected population at the end of each year. It should use a function that calculates and returns the projected new size of the population after a year. The formula is
N = P(1 + B)(1 - D)
where N is the new population size, P is the previous population size, B is the birth rate, and D is the death rate. Annual birth rate and death rate are the typical numbers of births and deaths in a year per 1000 people, expressed as a decimal.
So, for example, if there are normally about 32 births and 26 deaths per 1000 people in a given population, the birth rate would be .032 and the death rate would be .026.
Here is my code; I am having trouble figuring out how to do the calculation.
#include "stdafx.h" // Defines IDE required "pre-compiled" definition files
#include <iomanip>
#include <iostream>
using namespace std
int main ()
{
double startPop, // To hold the starting population.
float birthRate, // To hold the birth rate.
float deathRate; // To hold the death rate.
int numYears; // To hold the number of years to track population changes.
// Input and validate starting population
cout << "This program calculates population change.\n";
cout << "Enter the starting population size: ";
cin >> startPop;
while (startPop < 2.0)
{
cout << "Starting population must be 2 or more. Please re-enter: ";
cin >> startPop;
}
// Input and validate annual birth and death rates
cout << "Enter the annual birth rate (as % of current population): ";
cin >> birthRate;
while (birthRate < 0)
{
cout << "Birth rate percent cannot be negative. Please re-enter: ";
cin >> birthRate;
}
birthRate = birthRate / 100; // Convert from % to decimal.
cout << "Enter the annual death rate (as % of current population): ";
cin >> deathRate;
while (deathRate < 0)
{
cout << "Death rate percent cannot be negative. Please re-enter: ";
cin >> deathRate;
}
deathRate = deathRate / 100; // Convert from % to decimal.
cout << "For how many years do you wish to view population changes? ";
cin >> numYears;
while (numYears < 1)
{
cout << "Years must be one or more. Please re-enter: ";
cin >> numYears;
population = projectedNewSize(populationStartingSize, annualBirthRate, annualDeathRate);
cout << population << endl;
populationStartingSize = population;
}
printPopulations(startPop, birthRate, deathRate, numYears);
return 0;
} // End of main function
You can do this recursively or using a simple for loop.
E.g. Say if the numYears = 10, you would want to loop 10 times.
Create a temporary variable before your for loop and assign it the value of your startPop, e.g. endPop.
Then, starting with an initial population size of endPop, and death rate of deathRate as well as birth rate of birthRate, you calculate the population size after one year.
Having computed the population after one year in the first loop, you update endPop with the new value.
Subsequently, in the second loop, you use endPop once again as the new starting population size and the cycle repeats itself up till the end of your for loop, i.e. when 10 years have passed.
You did not declare the variable population in the above code snippet before using it.
Implementation:
while (numYears < 1)
{
cout << "Years must be one or more. Please re-enter: ";
cin >> numYears;
}
double population = populationStartingSize;
for ( int i = 0; i < numYears; i++) {
population = projectedNewSize(population, annualBirthRate, annualDeathRate);
cout << "After " << i+1 << "years: " << population << endl;
}
}
Take note that there is chance of over- and under-flow if your number gets too small or too big.
Implementation:
double projectedNewSize(double populationStartingSize, float annualBirthRate, float annualDeathRate) {
return populationStartingSize * (1 + annualBirthRate) * (1 - annualDeathRate);
}
For reading of numYears, you could consider using a do-while loop, :p.
4.16 (Salary Calculator) Develop a C++ program that uses a while statement to determine the
gross pay for each of several employees.
When someone works 41 hours or more. They get paid 1.5x more so my problem is that in my else if statement. I did rate * 1.5 which should translate into 10 * 1.5 = 15 but I get 425 because my code on the top probably, but I don't understand it at the moment.
He gave us this example. Which I'm trying to emulate.
"
Enter hours worked (-1 to end): 41
Enter hourly rate of the employee ($00.00): 10.00
Salary is $415.00
"
#include <iostream>
using namespace std;
int main()
{
double salary=0,rate=0,hours=0,counter=0;
cout << "Enter your hours worked: ";
cin >> hours;
cout << "Enter your rate: ";
cin >> rate;
while(counter<=hours)
{ salary=hours*rate;
counter++;
}
if(hours==-1)
{exit(0);
}
else if(hours>=41)
{
rate = rate * 1.5;
salary = salary + rate;
}
cout << "$" << salary;
return 0;
}
The loop you used has no functionality related to the problem, so I omitted it. Below is what will work. As others have said, define why you need a loop. I'm guessing you need to loop the code so the user can repeat this to their heart's content. If that is the case, I'll let you try to figure out how to break out of the loop when a user enters -1.
if (hours == -1)
exit(0);
else if (hours <= 40)
salary = rate * hours;
else {
double overtimeHours = hours - 40;
salary = rate * 40;
rate = rate * 1.5;
salary = salary + (rate * overtimeHours);
}
cout << "$" << salary;