Cannot get decimal precision to work

I have to make this program for class that takes a plumbers wage and calculates the total repair cost based on several variables/constants. But I cannot get the decimal places to to calculate precise enough ( I need it to go to 2 decimal places for money). When I run the program, instead of giving me some correct answer like $315.50, it will just output the closest thing like $315.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//LUCKENBACH, JUSTIN R.|ASSIGNMENT-1|CS150
//PLUMBER WAGE CALCULATOR
#include <iostream>

using namespace std;

//Insert pretty title

int main()
{
    cout << "--------PLUMBER WAGE CALCULATOR--------" << endl
         << "---------BY JUSTIN LUCKENBACH----------" << endl;


//Determine constant values
    const double MAT_COST = 75.00, RATE = 45.00;

//Determine variables
    int hours;

//Set decimal point to 2 digits
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(2);

//User input variables and calculate output

string name;

    double total, ftotal;

    cout << "How many hours did the plumber work? ";
    cin >> hours;
    total = (RATE * hours);
    cout << "Cost of Materials is $75" << endl;
    cout << "Cost of Labor: $" << total << endl;
    ftotal = (total + MAT_COST);
    cout << "Total: $"  << ftotal << endl;


    return 0;

}
Last edited on
What value are you entering for hours?

Do you realize that hours is an int?

sometimes you have to do stupid shit like multiply the result by 1.00 to get it to add the decimal places.

try total=(rate * hrs) * 1.00

As has been said change hrs to float of double. That might work too lol .....;-)
Last edited on
as hours is an int, and RATE and MAT_COST are whole numbers, there never will be any decimal places in the resultant calculation.

Use:

 
double hours;

Hello WakelessFoil,

Your code is a bit difficult to read and I did not notice this until I ran the program.

cout << "Cost of Materials is $75" << endl;. You could start with something like
cout << "Cost of Materials is $75.00" << endl;.

With a little effort your output could look like this:

--------PLUMBER WAGE CALCULATOR--------
---------BY JUSTIN LUCKENBACH----------

How many hours did the plumber work? 40

Cost of Materials is $  75.00
Cost of Labor:       $1800.00
=============================
Total:               $1875.00


Or something similar.

Andy
Your code is a bit difficult to read and I did not notice this until I ran the program.
1
2
 cout << "Cost of Materials is $75" << endl;. You could start with something like
cout << "Cost of Materials is $75.00" << endl;.



But since there is a const variable for cost of materials that "amount" the const should be used.

cout << "Cost of Materials is $" << MAT_COST << endl;.

But the major problem is the use of type int for the number of hours worked, as already pointed out in several places.
It's a "double hour" thing to do.
When I run the program, instead of giving me some correct answer like $315.50, it will just output the closest thing like $315.

If answer should be $315.50, then total without materials is
$315.50 - $75 = $240.50 and $240.50 / $45/h is about 5.3444444 hours
OR
total being $315.50, hours is $315.50 / $45/h = 7.0111111 hours

Did you really test with something like 5.34 or 7.01?

Then, yes, in the input you read and store only the integer part (the "whole") into the int hour.
Obviously 7*45.00 == 315.00 (approximately)


decimal precision

There is a different issue: floating point values are not "precise". An integer cannot hold a fraction.
Floating point numbers (float and double) have similarly only discrete possible values.

Furthermore, they have limited number of significant digits. While you can have huge number, say 3.14*10^30, adding 4.2 to it has no effect, because the result rounds to same 3.14*10^30.


An old Windows Calculator joke:
Q: What is the difference between Windows 3.11 and Windows 3.1? Should I upgrade?
A: 0
The "Calculator app" in mid 90' did calculate: 3.11 - 3.1 = 0

They had "very fine" floats back then, but the issue is still there. Floats are weird and strange.
By the way, many people do not know even where to find it, but in fact you can download without problems the application now anything, if you know where. ecause I bet a lot on my own. It's certainly not that easy. Here you need to study the process in detail and understand it. You just can't play it, because you can lose everything. But you can specifically practice on good apps. I found here https://playmarket.store the necessary application, there are a lot of them and you can already pick and choose the most convenient.
Registered users can post here. Sign in or register to post.