Hoping someone can help. I'm able to compile with no error, I'm not finding any syntax errors but when I run this, it crashes. Debug segfaults on launch. Full disclosure, this is homework. I'm not asking for someone to code this, just look at my problem and my existing code and maybe point me toward what I did that broke this so badly?
Question: You found an exciting summer job for five weeks. It pays, say, $15.50 per hour. Suppose that the total tax you pay on your summer job income is 14%. After paying the taxes you spend 10 % of your net income to buy new clothes and other accessories for the next school year and 1% to buy school supplies. After buying clothes and school supplies, you use 25% of the remaining money to buy savings bonds. For each dollar you spend to buy savings bonds, your parents spend $0.50 to buy additional savings bonds for you. Write a program that prompts the user to enter the pay rate for an hour and the number of hours you worked each week. The program then outputs the following:
a. Your income before and after taxes from your summer job.
b. The money you spend on clothes and other accessories.
c. The money you spend on school supplies.
d. The money you spend to buy savings bonds.
e. The money your parents spend to buy additional savings bonds for you.
Code:
// Libraries defined
#include <iomanip>
#include <iostream>
using namespace std;
//Main function
int main ()
{
//Input variables
double hourlyrate;
double hweek1;
double hweek2;
double hweek3;
double hweek4;
double hweek5;
//Output variables
double beforetax;
double netincome;
double clothmoney;
double suppliesmoney;
double moneyonbonds;
double additionalbonds;
double remain;
//This statement takes care of the decimal places
cout << fixed << showpoint << setprecision(2);
//Input from user
cout << "Enter your hourly rate: " << hourlyrate;
cin >> hourlyrate;
cout << "Week 1: " << hweek1;
cin >> hweek1;
cout << "Week 2: " << hweek2;
cin >> hweek2;
cout << "Week 3: " << hweek3;
cin >> hweek3;
cout << "Week 4: " << hweek4;
cin >> hweek4;
cout << "Week 5: " << hweek5;
cin >> hweek5;
//Mathematics
beforetax = hourlyrate * (hweek1 + hweek2 + hweek3 + hweek4+
hweek5) ;
netincome = beforetax - beforetax * 0.14;
clothmoney = netincome * 0.1;
suppliesmoney = netincome * 0.01;
remain = netincome - clothmoney - suppliesmoney;
moneyonbonds = remain * 0.25;
additionalbonds = static_cast<double>(moneyonbonds) * .50;
//Output to user
cout << endl << "Income before tax = $" << beforetax << endl
<< "Net income = $" << netincome << endl << "Money for clothes/accesories = $"
<< clothmoney << endl << "Money for supplies = $"<< suppliesmoney << endl
<< "Money for saving bonds = $" << moneyonbonds << endl
<< "Additional saving bonds money = $" << additionalbonds;
return 0;
}
I received this error
cout << "Enter your hourly rate: " << hourlyrate;
You try to output the variable before you initialize it.
This is probably unintentional.
The next line is
cin >> hourlyrate
It is the same for every variable. You should initialize them or not output them.
Are you sure about this:
cout << "Enter your hourly rate: " << hourlyrate;
cin >> hourlyrate;
cout << "Week 1: " << hweek1;
cin >> hweek1;
cout << "Week 2: " << hweek2;
cin >> hweek2;
cout << "Week 3: " << hweek3;
cin >> hweek3;
cout << "Week 4: " << hweek4;
cin >> hweek4;
cout << "Week 5: " << hweek5;
cin >> hweek5;
I think that you wanted:
cout << "Enter your hourly rate: ";
cin >> hourlyrate;
instead of:
cout << "Enter your hourly rate: "<< hourlyrate;
cin >> hourlyrate;
Related
I have already checked all the other questions but I just can't fix it.. I am a nuub at coding.
I don't know why it says it needs a while or where to put it, and it gives the wrong answer for the LOCS function also is there anything i can do about the default pointer warning. this is just a start i will be extending this later so it would be a big help and i have tried while and closing brackets everywhere lol
Btw if anyone can tell me how I can use the input as decision as you can see I am using 1 and 2, but If I could use permanent and casual that would be great.
// Calculate an employee's weekly salary
// Use a do while loop, and an if else statement to have user input data and display the correct values
#include <iostream>
using namespace std;
void main()
{
//Declaring the constant variables
const double BONUS_RATE = 5.0;
//Declaring the variables
int hours;
int sales;
int Status;
string permanent, casual, Name, status, result, employee;
double rate, sale_bonus, netPay, gross;
//set decimal point to 2 positions
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Display name
cout << "Calculate an employee's weekly salary\n\n";
//Do while loop to get hourly rate
do {
cout << "Enter employee name: ";
cin >> Name;
cout << "Please enter 1 if employee is permanent or 2 if casual: ";
cin >> Status;
Status = 0;
while (Status < 1 || Status > 2);
if (Status = 1)
{
cout << "Permanent employees have a fixed salary of $1000 per week" << endl;
sales = 0;
(sales < 1 || sales > 10);
cout << "If any please enter how many sales employee made this week:";
cin >> sales;
sale_bonus = sales * BONUS_RATE;
netPay = 1000 + sale_bonus;
cout << endl;
cout
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
else if (Status = 2) {
cout << "Casual employee's hourly rate is $15";
rate = 15.00;
cout << endl;
//while loop for hours
hours = 0;
while (hours < 1 || hours > 60)
{
cout << "Please enter how many hours you have worked this week:" << endl;
cout << "Minimum hours is 1" << endl;
cout << "Maximum hours are 60" << endl;
cout << "Enter hours worked: ";
cin >> hours;
}
//while loop for bonus
sales = 0;
while (sales < 1 || sales > 10)
{
cout << "Please enter how many sales you made this week:";
cin >> sales;
}
//Calculate pay
gross = hours * rate;
sale_bonus = sales * BONUS_RATE;
netPay = gross + sale_bonus;
//Display the results
cout << endl
<< "Hourly Rate: \t" << rate << endl
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
}
}
#include <iostream>
using namespace std;
void main()
{
//Declaring the constant variables
const double BONUS_RATE = 5.0;
//Declaring the variables
int hours;
int sales;
int Status;
string permanent, casual, Name, status, result, employee;
double rate, sale_bonus, netPay, gross;
//set decimal point to 2 positions
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Display name
cout << "Calculate an employee's weekly salary\n\n";
//Do while loop to get hourly rate
do {
cout << "Enter employee name: ";
cin >> Name;
cout << "Please enter 1 if employee is permanent or 2 if casual: ";
cin >> Status;
} while (Status < 1 || Status > 2); // add while after do block with }
if (Status == 1)// use '==' equality check not '=' .
{
cout << "Permanent employees have a fixed salary of $1000 per week" << endl;
sales = 0;
(sales < 1 || sales > 10);
cout << "If any please enter how many sales employee made this week:";
cin >> sales;
sale_bonus = sales * BONUS_RATE;
netPay = 1000 + sale_bonus;
cout << endl;
cout
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
else if (Status == 2) { // use '==' equality check not '=' .
cout << "Casual employee's hourly rate is $15";
rate = 15.00;
cout << endl;
//while loop for hours
hours = 0;
while (hours < 1 || hours > 60)
{
cout << "Please enter how many hours you have worked this week:" << endl;
cout << "Minimum hours is 1" << endl;
cout << "Maximum hours are 60" << endl;
cout << "Enter hours worked: ";
cin >> hours;
}
//while loop for bonus
sales = 0;
while (sales < 1 || sales > 10)
{
cout << "Please enter how many sales you made this week:";
cin >> sales;
}
//Calculate pay
gross = hours * rate;
sale_bonus = sales * BONUS_RATE;
netPay = gross + sale_bonus;
//Display the results
cout << endl
<< "Hourly Rate: \t" << rate << endl
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
}
I have made the suitable changes :
Use == to check equality instead of = operator!
Syntax of do while : do { //code... } while(condition).
The while must follow the do after closing the block!
Your Mistake
the main mistake is you have not close the do while loop, ask in comment for more clarification!
PS. I recommend you first copy my code and then run, and then analyse the issue!
Use this code this will work
#include
using namespace std;
void main()
{
//Declaring the constant variables
const double BONUS_RATE = 5.0;
//Declaring the variables
int hours;
int sales;
int Status;
string permanent, casual, Name, status, result, employee;
double rate, sale_bonus, netPay, gross;
//set decimal point to 2 positions
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
//Display name
cout << "Calculate an employee's weekly salary\n\n";
//Do while loop to get hourly rate
while(1){
cout << "Enter employee name: ";
cin >> Name;
cout << "Please enter 1 if employee is permanent or 2 if casual: ";
cin >> Status;
Status = 0;
while (Status < 1 || Status > 2);
if (Status = 1)
{
cout << "Permanent employees have a fixed salary of $1000 per week" << endl;
sales = 0;
(sales < 1 || sales > 10);
cout << "If any please enter how many sales employee made this week:";
cin >> sales;
sale_bonus = sales * BONUS_RATE;
netPay = 1000 + sale_bonus;
cout << endl;
cout
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
else if (Status = 2) {
cout << "Casual employee's hourly rate is $15";
rate = 15.00;
cout << endl;
//while loop for hours
hours = 0;
while (hours < 1 || hours > 60)
{
cout << "Please enter how many hours you have worked this week:" << endl;
cout << "Minimum hours is 1" << endl;
cout << "Maximum hours are 60" << endl;
cout << "Enter hours worked: ";
cin >> hours;
}
//while loop for bonus
sales = 0;
while (sales < 1 || sales > 10)
{
cout << "Please enter how many sales you made this week:";
cin >> sales;
}
//Calculate pay
gross = hours * rate;
sale_bonus = sales * BONUS_RATE;
netPay = gross + sale_bonus;
//Display the results
cout << endl
<< "Hourly Rate: \t" << rate << endl
<< "Hours Worked: \t" << hours << endl
<< "Gross Pay: \t" << gross << endl
<< "Net Pay \t" << netPay << endl;
}
}
}
#include<iostream>
#include<string>
#include<iomanip>
using namespace std;
struct EmployeeData
{
string employeeName;
float overtime;
float grossPay;
float hoursWorked;
float hourlyRate;
float statetaxOwed;
float statetaxRate;
float fedtaxOwed;
float fedtaxRate;
float netPay;
float totalgp;
float totalft;
float totalst;
float totalnp;
};
EmployeeData employee[4]; //array of 4 employees
void calculate_stats(EmployeeData& employee);
int main()
{
for (int i = 0; i < 4; ++i)
{
cout << "Please enter Employee #" << (i+1) << "'s" << " name: ";
cin.ignore();
getline(cin, employee[i].employeeName);
cout << "Please enter Employee #" << (i+1) << "'s hours worked: ";
cin >> employee[i].hoursWorked;
cout << "Please enter Employee #" << (i+1) << "'s hourly rate: ";
cin >> employee[i].hourlyRate;
cout << "Please enter Employee #" << (i+1) << "'s Federal Tax Rate: ";
cin >> employee[i].fedtaxRate;
cout << "Please enter Employee #" << (i+1) << "'s State Tax Rate: ";
cin >> employee[i].statetaxRate;
cout << endl;
}
for (int i = 0; i < 4; ++i)
{
calculate_stats(employee[i]);
cout << setprecision(2) << showpoint << fixed;
cout << "Employee #" << (i+1) << "'s name is: " << employee[i].employeeName << endl;
cout << "Employee #" << (i+1) << "'s Gross Pay is: $" << employee[i].grossPay << endl;
cout << "Employee #" << (i+1) << "'s Federal Taxes owed is: $" << employee[i].fedtaxOwed << endl;
cout << "Employee #" << (i+1) << "'s State Taxes owed is: $" << employee[i].statetaxOwed << endl;
cout << "Employee #" << (i+1) << "'s Net Pay is: $" << employee[i].netPay << endl;
cout << endl;
}
cout << "Total Gross Pay: " << employee[4].totalgp << endl; //here is the problem
cout << "Total Federal Tax Owed: " << employee[4].totalft<< endl;
cout << "Total State Tax Owed: " << employee[4].totalft<< endl;
cout << "Total Net Pay: " << employee[4].totalft << endl;
}
void calculate_stats(EmployeeData& employee)
{
if (employee.hoursWorked>40) {
employee.hoursWorked = ((employee.hoursWorked-40) * (1.5)) + 40;
}
else {
employee.hoursWorked = employee.hoursWorked;
}
employee.grossPay = employee.hoursWorked * employee.hourlyRate;
employee.fedtaxOwed = employee.grossPay * (employee.fedtaxRate/100);
employee.statetaxOwed = employee.grossPay * (employee.statetaxRate/100);
employee.netPay = (employee.grossPay - employee.fedtaxOwed- employee.statetaxOwed);
employee.totalgp = employee.totalgp + employee.grossPay;
employee.totalft = employee.totalft + employee.fedtaxOwed;
employee.totalst = employee.totalst + employee.statetaxOwed;
employee.totalnp = employee.totalnp + employee.netPay;
}
I was sure to include that cout block outside of the for-loops but still in the main. Using calculate stats did not change the output for me. The output is giving me all 0.00...they should be complete totals across all 4 Employees (so if the gross pay for all 4 employees is 1000, then the total gross pay should be 4000, which is a grand total which I accounted for in my calculations: employee.totalgp = employee.totalgp + employee.grossPay;. Here is what the output looks like (a snapshot of the wrong display, the rest works fine)...
Total Gross Pay: 0.00
Total Federal Tax Owed: 0.00
Total State Tax Owed: 0.00
Total Net Pay: 0.00
Press any key to continue . . .
How do I fix this? Thank you!
The values are printing out as zero mainly due to luck on your part. Your array, defined as EmployeeData employee[4] allows for access to employee[0] to employee[3]. Accessing employee[4] for your print is accessing memory beyond what was allocated for your array.
Beyond that, you store nothing at the memory location with your code, which is not such a bad thing. You do store each set of data twice in each record:
employee.totalgp = employee.totalgp + employee.grossPay;
employee.totalft = employee.totalft + employee.fedtaxOwed;
employee.totalst = employee.totalst + employee.statetaxOwed;
employee.totalnp = employee.totalnp + employee.netPay;
Each call to calculate_stats is storing the global values in different locations, defeating your attempt to have a cumulative count. You need to define a separate EmployeeData instance to store the accumulated totals, and write the values into that object.
Once you have a new structure in place to store your totals, change the above four lines in your existing calculate_stats method to update the global totals structure.
Okay so I am new to C++ and I really don't understand what I am doing wrong in this program.
I need to have the user input the start and end mileage of a trip and the amount of hours it took. I then need to print the results in miles and kilometers.
My test variables are
start 1230
end 1240.5
hours .12
The results should be
Miles 10.5
mph 87.5
kilometers 16.9
kph 140.8
But that is not what I get.
// Lab 3 Exercise 2
// Calculate MPH (miles Per Hour) and KPH (Kilometers Per Hour).
//
// Program by: Mohamed El-Malah
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
// Have the user enter his start and end mileage
double start_mileage, end_mileage;
cout << "Enter your starting mileage: ";
cin >> start_mileage;
cout << "Enter your end mileage: ";
cin >> end_mileage;
double trip_mileage = end_mileage - start_mileage;
// Have user input the hours the trip took
double total_hours, mph;
cout << "How man hours was the trip: ";
cin >> total_hours;
mph = trip_mileage / total_hours;
// Print the results in Miles and Kilometers
double trip_kilometers, kph;
trip_kilometers = trip_mileage * 1.6;
kph = trip_kilometers / total_hours;
cout << "Total miles " << setprecision(1) << setw(15) << trip_mileage << endl;
cout << " Miles/Hour " << setw(15) << mph << endl;
cout << "Total Kilometers" << setw(10) << trip_kilometers << endl;
cout << " Kilometers/Hour" << setw(10) << kph << endl;
}
Okay so I fixed the problem that I was computing the equations before I had the values.
However I still have a similar problem. My answers are not printed in single decimal points like I need them to be.
EX:
1e+001 instead of 10.5
9e+001 instead of 87.5
Corrected code:
// Lab 3 Exercise 2
// Calculate MPH (miles Per Hour) and KPH (Kilometers Per Hour).
//
// Program by: Mohamed El-Malah
#include <iostream>
#include <iomanip>
using namespace std;
int main ()
{
// Have the user enter his start and end mileage
double start_mileage, end_mileage;
cout << "Enter your starting mileage: ";
cin >> start_mileage;
cout << "Enter your end mileage: ";
cin >> end_mileage;
double trip_mileage = end_mileage - start_mileage;
// Have user input the hours the trip took
double total_hours, mph;
cout << "How man hours was the trip: ";
cin >> total_hours;
mph = trip_mileage / total_hours;
// Print the results in Miles and Kilometers
double trip_kilometers, kph;
trip_kilometers = trip_mileage * 1.6;
kph = trip_kilometers / total_hours;
/**** fixed stream manipulator makes cout not use scientific notation ****/
cout << "Total miles " << fixed << setprecision(1) << setw(15) << trip_mileage << endl;
cout << " Miles/Hour " << setw(15) << mph << endl;
cout << "Total Kilometers" << setw(10) << trip_kilometers << endl;
cout << " Kilometers/Hour" << setw(10) << kph << endl;
}
You had messed with the ordering of variables total_hours and trip_mileage. Make sure you use/calculate the value of the variables after you have taken relevant input from the user, otherwise random values will be used.
Additionally, to make cout not use scientific notation, you must use std::fixed stream manipulator.
See here:
double trip_mileage = end_mileage - start_mileage;
cout << "Enter your starting mileage: ";
cin >> start_mileage;
cout << "Enter your end mileage: ";
cin >> end_mileage;
You're computing your trip mileage before you asked your user for the necessary inputs. Declare the variable but don't do the computation until later:
double trip_mileage;
cout << "Enter your starting mileage: ";
cin >> start_mileage;
cout << "Enter your end mileage: ";
cin >> end_mileage;
trip_mileage = end_mileage - start_mileage;
You make the some mistake again with total_hours. I'll let you figure that one out.
Change order of these lines. Because earlier you are computing mph even before you had input of total_hours. In that case total_hours is assigned a garbage value and your results differ.
double total_hours, mph;
cout << "How man hours was the trip: ";
cin >> total_hours;
mph = trip_mileage / total_hours;
and same about trip_mileage
cout << "Enter your starting mileage: ";
cin >> start_mileage;
cout << "Enter your end mileage: ";
cin >> end_mileage;
double trip_mileage = end_mileage - start_mileage;
So I am trying to learn C++ for a college course and I have to write a program which uses this formula:
Amount = Principal * (1 + Rate/T)^T
Where principal is the balance in savings, rate is the interest rate, and t is the number of times the interest is compounded during a year. According to the book if you type in 4.25 as the interest rate and 12 as the number of times compounded with the principal as 1000.00 then you should get 43.34 as interest and the total amount should be 1043.34. I'm not sure if I am coding it wrong or what but I was wondering if anyone could help me out with any mistakes I might have done! I'm trying to do it on my own for about a day or two now but I have had no luck.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double PRINCIPAL;
double INTEREST_RATE;
double COMPOUND_AMOUNT;
cout << "What is your savings account balance?: " << endl;
cin >> PRINCIPAL;
cout << "What is your annual interest rate?: " << endl;
cin >> INTEREST_RATE;
cout << "How many times has your interest been compounded?: " << endl;
cin >> COMPOUND_AMOUNT;
double amt1 = 1 + (INTEREST_RATE/COMPOUND_AMOUNT);
double AMOUNT = PRINCIPAL * pow(amt1, COMPOUND_AMOUNT);
cout << "Interest Rate: " << INTEREST_RATE << endl;
cout << "Times Compounded: " << COMPOUND_AMOUNT << endl;
cout << "Principal: " << PRINCIPAL << endl;
cout << "Interest: " << INTEREST_RATE * COMPOUND_AMOUNT << endl;
cout << "Amount: " << AMOUNT << endl;
system("pause");
return 0;
}
This is a math error. If you're going to take in interest rates as '4.25' %, you need to divide the interest rate by 100. The code below gave me the amount as 1043.34 when 4.25 is entered as the interest rate.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double PRINCIPAL;
double INTEREST_RATE;
double COMPOUND_AMOUNT;
cout << "What is your savings account balance?: " << endl;
cin >> PRINCIPAL;
cout << "What is your annual interest rate? (in %): " << endl;
cin >> INTEREST_RATE;
INTEREST_RATE /= 100;
cout << "How many times has your interest been compounded?: " << endl;
cin >> COMPOUND_AMOUNT;
double amt1 = 1 + (INTEREST_RATE/COMPOUND_AMOUNT);
double AMOUNT = PRINCIPAL * pow(amt1, COMPOUND_AMOUNT);
cout << "Interest Rate (%): " << INTEREST_RATE * 100 << endl;
cout << "Times Compounded: " << COMPOUND_AMOUNT << endl;
cout << "Principal ($): " << PRINCIPAL << endl;
cout << "Interest ($): " << AMOUNT - PRINCIPAL << endl;
cout << "Amount ($): " << AMOUNT << endl;
system("pause");
return 0;
}
for interest your book is talking about the amount of interest in dollars, i.e. AMOUNT - PRINCIPAL.
I've been teaching myself C++ on and off for a few months and now I'm trying to make a payroll system. Here's my code:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void wageCompute (int, int);
int main()
{
int loopTimes=0;
int empNum=100, workHours, otHours, rate, bPay, otPay, grossPay;
string empName, empPos;
cout << "PAYROLL FOR THE MONTH OF MARCH" << endl;
cout << "Employees: " << empNum << endl;
while (empNum>loopTimes)
{
cout << "NAME: ";
cin.ignore();
getline (cin, empName);
cout << "\nPOSITION: ";
getline (cin, empPos);
cout << "\nHOURS WORKED: ";
cin >> workHours;
cout << "\nWAGE PER HOUR: ";
cin >> rate;
bPay = workHours*rate;
cout << "YOUR BASE PAY IS: " << bPay << endl << endl;
cout << "HOURS WORKED OVERTIME: ";
cin >> otHours;
otPay = (1.5*rate)*otHours;
cout << "\nOVERTIME PAY: " << otPay << endl;
grossPay = bPay + otPay;
cout << "GROSS PAY: " << grossPay << endl;
wageCompute(bPay, grossPay);
loopTimes++;
}
return EXIT_SUCCESS;
}
void wageCompute(int bPay, int grossPay)
{
double rate, dedInsurance, dedMedical, totDeduct, netPay, tax;
if (bPay<10001)
{
rate = 0.05;
}
else if (bPay<15001)
{
rate = 0.1;
}
else if (bPay<20001)
{
rate = 0.15;
}
else
{
rate = .2;
}
tax = bPay*rate;
dedInsurance = bPay*0.05;
dedMedical = bPay*0.01;
totDeduct = tax + dedInsurance + dedMedical;
cout << "TAX: " << tax << endl;
cout << "SSS DEDUCTION: " << dedInsurance << endl;
cout << "Medicare DEDUCTION: " << dedMedical << endl;
cout << "TOTAL DEDUCTIONS: " << totDeduct << endl;
netPay = grossPay - totDeduct;
cout << "NET PAY: " << netPay << endl;
}
The part where it goes wrong is when I input a certain value for the Hours Worked, Wage per Hour and Hours worked overtime. The program checks the basic pay for the suitable amount of tax it should deduct, what I input was 160 for the hours worked, 100 for the wage per hour, and 10 for overtime. I've tried lessening and increasing it and it worked just fine it seems that it's just these combination of numbers is the part where it goes wrong.
A screenshot of the output:
Your question isn't very clear but I suspect that what you are seeing here is a well known limitation of floating point numbers; numbers that are easy to represent exactly in base 10 don't have an exact representation in base 2. One example : 0.1 in base 10 is 0.0001100110011… repeating in base 2; the accuracy of the approximation depends on how many bits one is willing to use to write it.
An alternative approach is to use integer arithmetic with a known precision, so say you want to calculate to the nearest hundredth of a penny (I'm using UK currency here). Represent £1.01 as 10100 and when your finished val / 10000 is the pounds and (val % 10000) / 100 is the pence. If needed you can implement some more complex rules around rounding for the pence.