Total is truncating/rounding value, need help preventing that

I have searched the webs and my textbook but I cannot figure out why my total does not come out to $1.55 instead of $1.60. I believe that somewhere in my coding the double is getting stored as an integer? Full disclosure: I really do not understand the truncating issues with floating numbers and the book has exactly 2 paragraphs about it. Help please!

//This program displays a table showing day and pay that calculates
//the amount a person would earn over a period of time if his or her
//salary were five cents the first day, ten cents the second day, and
//so on doubling each day.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int Days, entry = 0; // Days

double Pay = 0.05; // Pay

double Total; // Total Pay

// Get the number of days needed to limit the loop.
cout << "This program calculates the amount a person would earn over\n";
cout << "a period of time if his or her salary were five cents the first day,\n";
cout << "ten cents the second day, and so on doubling each day.\n";
cout << "Please enter the number of days to double pay.\n";
cin >> entry;

// Display the table of Days and Pay.
cout << "\nDays Pay\n";
cout << "-------------\n";

// Calculate the pay for each day and accumulate the total.
for (Days = 1; Days <= entry; Days++)
{
cout << setprecision(2) << fixed << showpoint;
cout << Days << setw(8) << "$ " << Pay << endl;

// Increment the pay.
Pay *= 2;

// Accumluate the pay.
Total = Pay;
}

//Return the total pay value.
cout << "Total" << setw(4) << "$ " << Total << endl;
return 0;
}
Last edited on
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
45
46
47
48
49
//This program displays a table showing day and pay that calculates
//the amount a person would earn over a period of time if his or her
//salary were five cents the first day, ten cents the second day, and
//so on doubling each day.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int Days, entry = 0; // Days

    double Pay = 0.05; // Pay

    // double Total; // Total Pay
    double Total = 0 ; // initialise to zero  // *******************

    // Get the number of days needed to limit the loop.
    cout << "This program calculates the amount a person would earn over\n";
    cout << "a period of time if his or her salary were five cents the first day,\n";
    cout << "ten cents the second day, and so on doubling each day.\n";
    cout << "Please enter the number of days to double pay.\n";
    cin >> entry;

    // Display the table of Days and Pay.
    cout << "\nDays Pay\n";
    cout << "-------------\n";

    // Calculate the pay for each day and accumulate the total.
    for ( Days = 1; Days <= entry; Days++ )
    {
        cout << setprecision( 2 ) << fixed << showpoint;
        //cout << Days << setw( 8 ) << "$ " << Pay << endl;
        cout << setw(5) << Days << " $ " << setw( 8 ) << Pay << '\n';

        // Increment the pay.
        Pay *= 2;

        // Accumluate the pay.
        // Total = Pay;
        Total += Pay ; // ***** add to the Total // ***********************
    }

    //Return the total pay value.
    // cout << "Total" << setw( 4 ) << "$ " << Total << endl;
    cout << "\nTotal $ " << setw( 8 ) << Pay << '\n' ;

    return 0;
}
1) set base total to 0
2) Instead of assigning total, accumulate it: Total += Pay;
3) Move doubling pay after total increment
I've updated it to this, but I still get $1.60 instead of $1.55. How do I prevent the total from rounding up?

//This program displays a table showing day and pay that calculates
//the amount a person would earn over a period of time if his or her
//salary were five cents the first day, ten cents the second day, and
//so on doubling each day.
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int Days, entry = 0; // Initialize Days to Zero

double Pay = .05; // Initialize Pay to .05

double Total = 0; // Initialize Total to Zero

// Get the number of days needed to limit the loop.
cout << "This program calculates the amount a person would earn over\n";
cout << "a period of time if his or her salary were five cents the first day,\n";
cout << "ten cents the second day, and so on doubling each day.\n";
cout << "Please enter the number of days to double pay.\n";
cin >> entry;

// Display the header for table of Day and Pay.
cout << "\nDay Pay\n";
cout << "--------------\n";

// Calculate the pay for each day and accumulate the total.
for (Days = 1; Days <= entry; Days++)
{
cout << setprecision(2) << fixed << showpoint; // Setting decimal points
cout << Days << setw(8) << "$ " << Pay << "\n"; // Setting width and formatting for currency

Total += Pay ; // Accumulate the pay.

Pay *= 2; // Double the pay.
}

cout << "Total" << setw(4) << "$ " << Pay << "\n"; //Return the total pay value with width set and currency.
return 0;
}
cout << "Total" << setw(4) << "$ " << Pay << "\n";
You had it right on your first try. Why did you change it?
Change this line:
cout << "Total" << setw(4) << "$ " << Pay << "\n"; //Return the total pay value with width set and currency.

To print the total
cout << "Total" << setw( 4 ) << "$ " << Total << "\n";

That error (print the last computed Pay instead of the total) came from the code that I had posted.
Please accept my apology.
No worries JLBorges! Thank you for helping! Thank you also to MiiNiPaa! You were both helpful!
Topic archived. No new replies allowed.