C++ How to prevent program from rounding?

Archived from the original Sajha.com — preserved as posted, replies can no longer be added here.
Start a New Discussion
Archived Post

All, I need to understand why following program keeps rounding the total hours worked. I keep getting 1.6000 instead of 1.5500. So, curious as to how can prevent from this code from rounding the total result. Can anyone assist regarding this? I really appreciate any input. #include <iostream> #include <cmath> #include <iomanip> using namespace std; int main() { int day, entries; double PAY_RATE = 0.0250; double sum = 0; cout << "Please enter the number of days you will be working? "; // Requesting user input for number of days cin >> entries; cout << '\n'; cout << "Day" "\t\t" "Pay""\n"; // Tabbed Display format cout << "=======================\n"; for (day = 1; day <= 2; day++) // Using for loop to accomplish given task { PAY_RATE = (PAY_RATE * 2.00); cout << fixed << showpoint << setprecision(5); cout << day << "\t\t" << PAY_RATE << endl; } for (day = 3; day <= entries; day++) // Loop Method in place for correct output { PAY_RATE = (PAY_RATE * 2); cout << fixed << showpoint << setprecision(4); //Formatting output to 4 decimal places cout << day << "\t\t" << PAY_RATE << endl; } cout << '\n'; sum = (sum + PAY_RATE); // Calculating total pay cout << "Your total pay is "; cout << fixed << showpoint << setprecision(4) << (sum + PAY_RATE) << endl; // Displays the total cout << '\n'; return 0; } Last edited: 09-Oct-14 09:20 AM

user01 · Oct 9, 2014 9:12 AM · 248 views

2 Replies

When you run this code input 5. 

user01 · Oct 9, 2014 9:15 AM

#include #include #include using namespace std; int main() { int day, entries; double PAY_RATE = 0.0250; double sum = 0.0; cout > entries; cout << '\n'; cout << "Day" "\t\t" "Pay""\n"; // Tabbed Display format cout << "=======================\n"; for (day = 1; day <= 2; day++) // Using for loop to accomplish given task { PAY_RATE = (PAY_RATE * 2.00); cout << fixed << showpoint << setprecision(5); cout << day << "\t\t" << PAY_RATE << endl; sum = sum + PAY_RATE; } for (day = 3; day <= entries; day++) // Loop Method in place for correct output { PAY_RATE = (PAY_RATE * 2); cout << fixed << showpoint << setprecision(4); //Formatting output to 4 decimal places cout << day << "\t\t" << PAY_RATE << endl; sum = sum + PAY_RATE; } cout << '\n'; cout << "Your total pay is "; cout << fixed << showpoint<< setprecision(4) << sum << endl; // Displays the total cout << '\n'; return 0; }

prankster · Oct 9, 2014 10:21 AM

This conversation is preserved exactly as it was on the original Sajha.com and can't accept new replies.

Start a New Discussion