Get double as a result of division of integer values - c++

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

Related

C++ Overtime / Payroll / Time and a half/ Double Time

i know my if statement is not correct somewhere but i do not know where? or maybe its just the logical error?
After 8 hours any hours worked will be paid time and a half. That is, given the wage per hour multiply by 1.5. That wage is paid for hours after 8 hours.
After 10 hours any hours worked will be paid double time. That is, given the wage per hour multiply by 2.0. That wage is paid for hours after 10 hours.
Please show: ( Example output )
Wage per hour: 12.37
Hours worked : 10.3
Pay for ( 0 to 8 hours) : 98.96
Pay for hours 8 to 10) : 37.11
Pay for hours (10 and beyond): 7.42
Total Gross Pay : 143.49
// Example program
#include <iostream>
#include <string>
using namespace std;
double HoursWorked;
double WagePerHour;
double TotalWages;
double TimeAndHalf;
double Overtime;
char ContinueChar;
//test cases: 10.5 hours # 12/hour = $96.00, 2 hours at 1.5 rate = $36.00, .5 hour at 2.0 rate = $12.00
// 6.3 hours # 12/hour = 75.6, no hours of overtime or double time
//12.5 hours # 14.34/ hour = $114.72, 2 hours at 1.5 rate = 43.02, 2.5 hours at 2.0 rate = $71.70
//3.7 hours # 19/hour = $70.30
// 14 hours # 23.50/hour = $188, 2 hours at 1.5 rate = $70.50, 4 hours at 2.0 rate = $188
//I tested with test test cases and the program had the same results.
int main()
{
cout << "Ticket #64220\n";
cout << "CMPR-120\n";
cout << "Instructor : Joel Kirscher\n";
cout << "Student: Seyed Shariat\n";
cout << "Payroll Overtime";
cout << "\n\n";
do {
cout << "How many hours did you work this pay period: \n";
cin >> HoursWorked;
cout << "What wage do you get paid per hour?: \n";
cin >> WagePerHour;
cout << "So you your paycheck will be: " << HoursWorked * WagePerHour << " before taxes are taken out. \n";
if (HoursWorked > 8 && <= 10)
cout << "For the hours you worked over 8, and less than or equal to 10 you made: " << HoursWorked * 1.5 * WagePerHour << " \n";
else (HoursWorked >10);
cout << "For the hours you worked over 10: " << HoursWorked * 2.0 * WagePerHour << " \n";
cout << "Do you want this to run again? (y=Yes): ";
cin >> ContinueChar;
} while (ContinueChar == 'y' || ContinueChar == 'Y');
cin.get();
return 0;
}
There are many approaches to this. Here is one. The comments may help you to follow my reasoning. Fortunately the mathematics behind this is not very complicated.
/*
We need to find the area under this pay rate / hours worked graph.
The following approach divides the graph into three rectangles as shown below
2.0 | +--+
| | |
1.5 | +-+--|
| | |
1.0 +-------+----|
| |
0.5 | |
| |
0.0 +------------+
0 8 10
*/
// Calculate basic pay before any overtime consideration
Pay = HoursWorked * HourlyRate;
// Hours above 8 earn half-again more than the standard wage.
// (This will be only positive if more than 8 hours were worked.)
OvertimePay = (HoursWorked - 8.0) * HourlyRate * 0.5;
if(OvertimePay > 0.0)
Pay += OvertimePay;
// Hours above 10 earn an additional 50% extra
// (This will be only positive if more than 10 hours were worked.)
OvertimePay = (HoursWorked - 10.0) * HourlyRate * 0.5;
if(OvertimePay > 0.0)
Pay += OvertimePay;

Is there a calculation error in my program?

Apparently there is a calculation error somewhere in my program, but I simply can’t find it.
The only information I have as to why there is a calculation error is this following feedback given by MyProgrammingLab (A site that automatically tests code to see if it's incorrect or not). I don't know what values were entered for the annual death rate and annual birth rate to cause it. Could it be that I'm right but MyProgrammingLab is wrong? Honestly, all my own tests seem fine.
Expected Output:
Year 1: 200 176
Year 2: 176 154
Year 3: 154 135
Year 4: 135 118
Year 5: 118 103
Year 6: 103 90
Year 7: 90 79
Actual Output:
Year 1: 200 199
Year 2: 199 198
Year 3: 198 197
Year 4: 197 196
Year 5: 196 195
Year 6: 195 194
Year 7: 194 193
I built the program according to the following assignment:
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 number 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.
My code:
#include <iostream>
using namespace std;
int projectedNewSize(float population, float annualBirthRate, float annualDeathRate) {
float annualBirthRate2 = annualBirthRate / 1000;
float annualDeathRate2 = annualDeathRate / 1000;
int newpopulation = population * (1 + annualBirthRate2) * (1 - annualDeathRate2);
return newpopulation;
}
int main() {
int populationStartingSize = 0;
float annualBirthRate = 0;
float annualDeathRate = 0;
int numberOfYearsToDisplay = 0;
do {
cout << "Enter starting population size: ";
cin >> populationStartingSize;
cout << "Enter annual birth rate: ";
cin >> annualBirthRate;
cout << "Enter annual death rate: ";
cin >> annualDeathRate;
cout << "Enter years to display: ";
cin >> numberOfYearsToDisplay;
} while (!(populationStartingSize >= 2) || !(numberOfYearsToDisplay >= 1));
int population;
for (int i = 1; i <= numberOfYearsToDisplay; i++) {
cout << "Year " << i << ": " << populationStartingSize << " ";
population = projectedNewSize(populationStartingSize, annualBirthRate, annualDeathRate);
cout << population << endl;
populationStartingSize = population;
}
system("pause");
return 0;
}
So, The answer is
There is no need to divide the annualBirthRate and the annualDeathRate by 1000. Since annualBirthRate is calculated as annual births per 1000 of a population, It need not be divided by 1000 again.
Thus removing these lines
float annualBirthRate2 = annualBirthRate / 1000;
float annualDeathRate2 = annualDeathRate / 1000;
and changing
int newpopulation = population * (1 + annualBirthRate2) * (1 - annualDeathRate2);
to
int newpopulation = population * (1 + annualBirthRate) * (1 - annualDeathRate);
So, the final code would look like this:
#include <iostream>
using namespace std;
int projectedNewSize(float population, float annualBirthRate, float annualDeathRate) {
int newpopulation = population * (1 + annualBirthRate) * (1 - annualDeathRate);
return newpopulation;
}
int main() {
int populationStartingSize = 0;
float annualBirthRate = 0;
float annualDeathRate = 0;
int numberOfYearsToDisplay = 0;
do {
cout << "Enter starting population size: ";
cin >> populationStartingSize;
cout << "Enter annual birth rate: ";
cin >> annualBirthRate;
cout << "Enter annual death rate: ";
cin >> annualDeathRate;
cout << "Enter years to display: ";
cin >> numberOfYearsToDisplay;
} while (!(populationStartingSize >= 2) || !(numberOfYearsToDisplay >= 1));
int population;
for (int i = 1; i <= numberOfYearsToDisplay; i++) {
cout << "Year " << i << ": " << populationStartingSize << " ";
population = projectedNewSize(populationStartingSize, annualBirthRate, annualDeathRate);
cout << population << endl;
populationStartingSize = population;
}
system("pause");
return 0;
}
You guys discussed in the comments section and left the question unanswered..
int projectedNewSize(float population, float annualBirthRate, float annualDeathRate) {
int newpopulation = roundf(population * (1.0 + annualBirthRate) * (1.0 - annualDeathRate);
return newpopulation;
}
In the calculatiton you dont have to consider the factor of 1000 if it is already done in the input values. But if you want a 'most accurate' table you have to round the values in a proper mind. The return of the calculation is a float. If you assign it to an int - it always will be truncated. A little difference wich will be carried over from one loop to the next, ending up in some reasonable differences to the expected values at the end. The most proper way would be to change the type of 'newcalculation' to float and round only when display the value.
N = P + BP - DP
Program asked for birthrate and death rate, assume user entered 5 for 5%.
Basic math needs to be calculated here 5/100 = 0.05
The formula is now
N = P + (BP/100) - (DP/100)
Replace
float annualBirthRate2 = annualBirthRate / 1000;
float annualDeathRate2 = annualDeathRate / 1000;
int newpopulation = population * (1 + annualBirthRate2) * (1 - annualDeathRate2);
With
int newpopulation = population + (population * birthRate/100) - (population * deathRate/100)

User input giving wrong results

Okay well, I am having trouble with the user input from taxRate, when I compile the code I get the wrong results.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
long propertyValue;
long taxRate;
long exemption = 5000;
cout << "What is the actual value of your property?\n";
cin >> propertyValue;
cout << "What is the current tax rate?\n";
cin >> taxRate;
cout << "You will pay an annual property tax of ";
cout << (propertyValue - exemption) * taxRate / 100.00;
cout << " on your property\n";
cout << "and your quarterly tax bill will be; ";
cout << (propertyValue - exemption) * taxRate / 100.00 / 4 << endl;
system("pause");
return 0;
}
This is a simple error in the code, I'll explain why.
Let's take for example a PropertyValue of 300,000 and a tax rate of let's say 5.6.
You used the formula for the annual property tax rate:
(PropertyValue - exemption) * taxRate / 100.00;
And you declared taxRate as a long (aka a long int)
long taxRate;
300,000 - 5000 = 295,000 * 5.6 <----- Your mistake is right here!
Remember, long is a property of int so 5.6 becomes 5. Changing it to a double (floating-point number) will fix your problem. I tested this several times and it fix's your code.

Salary calculator (over time pay) require assistance of senior level programmers

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;

My loops are not looping [closed]

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.