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.
Related
This question already has answers here:
Why not use Double or Float to represent currency?
(16 answers)
Closed 3 years ago.
The code seems to work fine when it performs the first step of multiplying the number of quarters entered by 0.25, but then it just doesn't work with the next two steps.
#include <iostream>
using namespace std;
int main()
{
int quarter, dime, nickle;
int result;
quarter = 25;
dime = 10;
nickle = 5;
int numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
result = (numQuarters * quarter) + (numNickles * nickle) + (numDimes * dime);
cout << "The total amount of pennies is: " << result;
return 0;
}
I expect the output of 4 quarters, 10 dimes & 20 nickels to be 300 pennies
The output is 102
Edit: Code Fixed and working now!
Hmm... your code seems fine, but I think something was wrong with the Order of Operations in your Math. I just changed the value of nickel to 0.05 and the value of dime to 0.10 (I think that was a mistake in your code). I aslo moved the *100 down to the cout statement, and that helped clear things up...
#include <iostream>
using namespace std;
int main()
{
float quarter, dime, nickle, penny;
float result;
quarter = 0.25;
dime = 0.10;
nickle = 0.05;
float numQuarters, numDimes, numNickles;
cout << "Please enter the number of quarters and press Enter: ";
cin >> numQuarters;
cout << "Please enter the number of nickles and press Enter: ";
cin >> numNickles;
cout << "Please enter the number of dimes and press Enter: ";
cin >> numDimes;
result = (numQuarters * quarter) + (numNickles * nickle)+ (numDimes * dime);
cout << "The total amount of pennies is: " << result * 100;
return 0;
}
Oh, and just like what #Pete Becker said, do your calculations in pennies (whole numbers). Not only does it fix some minor errors with floats as money, it also makes your code easier to read and manipulate.
I am taking a class on C++ currently and need help with a certain part of my code. It all compiles but when I go to test it I am only allowed to input one value into the window before the rest of the code advances through without input.
For reference, this is the question I am answering:
11. Payroll
Design a PayRoll class that has data members for an employee’s hourly pay rate, number of hours worked of type double . The default constructor will set the hours worked and pay rate to zero. The class must have a mutator function to set the pay rate for each employee and hours worked. The class should include accessors for both the hours worked and the rate of pay. The class should lastly have a getGross function that will return a double calculated by multiplying the hours worked by the rate
of pay.
Write a program with an array of seven PayRoll objects . The program should ask the user for the rate of pay for each employee and the number of hours each employee has worked. Be sure to include an employee claiming to work more then 60 hours per week. Print out, the array number of the employee, the hours worked, the rate of pay, and the gross pay, of all the employee's each on their own line. Set the precision for printing the doubles to two decimal places.
Input Validation: Do not accept values greater than 60 for the number of hours worked, simply have the set function set number of hours worked to 60, the maximum allowed.
My code is as follows:
#include <iostream>
#include <iomanip>
//Garrett Bartholomay
/*This program defines and implements the Payroll class.
* The class is then used in a program that calculates gross pay for an array of
* Payroll objects after accepting values for hours and pay rate from standard input*/
class Payroll
{
private:
int hoursWorked;
double payRate;
public:
Payroll();
Payroll(int, double);
void setHours(int);
void setPayRate(double);
int getHours() const;
double getPayRate() const;
double getGross()const;
};
Payroll::Payroll()
{
hoursWorked = 0;
payRate = 0.0;
}
Payroll::Payroll(int h, double r)
{
payRate = r;
hoursWorked = h;
}
void Payroll::setHours(int h)
{
hoursWorked = h;
}
void Payroll::setPayRate(double p)
{
payRate = p;
}
int Payroll::getHours() const
{
return hoursWorked;
}
double Payroll::getPayRate() const
{
return payRate;
}
double Payroll::getGross() const
{
double gross = static_cast<double>(hoursWorked) * payRate;
return gross;
}
using namespace std;
int main()
{
const int NUM_EMPLOYEES = 7;
Payroll employee[NUM_EMPLOYEES];
int pay;
int hours;
int i;
double grossPay;
for (i = 0; i < NUM_EMPLOYEES; i++)
{
cout << "Enter the # " << (i) << " employee's rate of pay per hour: ";
cin >> pay;
cout << "Enter the # " << (i) << " employee's hours worked for the week: ";
cin >> hours;
employee[i].setPayRate(pay);
employee[i].setHours(hours);
}
cout << "\n\nHere is the gross pay for each employee:\n";
cout << fixed << showpoint << setprecision(2);
for (int i = 0; i < NUM_EMPLOYEES; i++)
{
grossPay = employee[i].getGross();
cout << "The gross pay for employee # " << (i) << " is: " << grossPay << endl;
}
return 0;
}
The input resides within the loops.
Any help would be greatly appreciated.
Thanks,
G
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;
#include <cstdlib>
#include <iostream>
#include <cmath>
using namespace std;
int main(int argc, char *argv[])
{
double amount, rate, time, interest, month;
interest = rate/(12.0*100);
month = (amount * rate)/1.0 -(1.0 + interest), pow(time*12);
cout << "What is the amount of the loan? $ ";
cin >> amount;
cout << "What is the annual percentage rate? ";
cin >> rate;
cout << "How many years will you take to pay back the loan? ";
cin >> time;
cout <<"\n-------------------------------------------------------\n";
cout << "Amount Annual % Interest Years Monthly Payment\n\n\n";
cout <<amount <<" " <<rate <<" " <<time << " " <<month;
cout <<"\n\n";
system("PAUSE");
return EXIT_SUCCESS;
}
I am getting an error here and am not sure exactly how to use the pow function properly. The formula according to the problem is:
Monthly payment = (loan amount)(monthly interest rate) / 1.0 - (1.0 + Monthly interest rate)^-(number of months)
Here is the line that I am having trouble with
month = (amount * rate)/1.0 -(1.0 + interest), pow(time*12);
Thanks for the help
std::pow accepts two arguments like so
pow (7.0,3)
So your code should be more like this
month = (amount * rate)/1.0 - std::pow( (1.0 + interest), 12);
Your code has other flaws as well, like you do calculation before you set the values.
I Have this code to find out the total purchase price of some numbers and i need to know how to round the numbers to only 2 decimal places.
#include <iostream.h>
#include <cstdlib.h>
#include <string.h>
int main() {
float sale_price;
float tax_rate;
float discount_rate;
system("cls");
system("color 07");
cout << "\n\nWelcome to the second version of my total purchase price calculator!\n";
cout << "How much did your recently purchased item cost? ";
cin >> sale_price;
cout << "What is the tax rate in your area? ";
cin >> tax_rate;
cout << "What was the discount rate, if any (if none, just put down 1) ";
cin >> discount_rate;
float tax = sale_price*(tax_rate/100);
float discount = sale_price*(discount_rate/100);
float total_price = sale_price + tax - discount;
cout << "The total price of your item is $"<<total_price<<" with $"<<tax<<" tax minus $"<<discount<<" due to discount.\n";
cout << "Would you like a reciept? y or n. ";
string answer;
End:
cin >> answer;
if (answer == "y") {
goto Reciept;
}
else if (answer == "n") {
return 0;
}
else {
cout << "Try another answer\n";
goto End;
}
Reciept:
system("cls");
system("color 70");
cout << "\xda\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xc4\xbf\n";
}
this usually gives me 4 decimal places just FYI
Round to 2 decimals: int((n * 100.0) + 0.5) / 100;
Round to 3 decimals: int((n * 1000.0) + 0.5) / 1000;
etc.
The easy way is to use setprecision:
std::cout << "The total price of your item is $"
<< std::setprecision(2) << total_price ...
This will occasionally get things wrong. A better solution is to use an improved rounding function that is not a part of the standard (e.g., How does Excel successfully Rounds Floating numbers even though they are imprecise?). An even better solution is to implement your own fixed point arithmetic algorithm so as to avoid this problem entirely. You can still go one better than that: Use a fixed point arithmetic package that someone else has already written and tested to the nth degree.
You should use Iomanip's setprecision:
setprecision reference
When you output with cout, use the setprecision() operator.
cout << fixed;
cout << setprecision(2) << sale_price*(tax_rate/100);
Do
cout.precision(2);
cout << fixed;
before you output the floats.