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;
}
//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>
usingnamespace 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;
}
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;
}