i got c++ off amazon, no solutions are provided for the end of chapter exercises
my problem is with getting larger numbers to be only two decimal places. i know about setprecision and how it works for the smaller numbers but when i enter a large number for days worked it doesn't output correctly.
my current thought is to make some if statements and if the number is in a certain range use the if statement to increase the set precision to allow two decimals. is this the correct way of thinking. Example output is given to see what im talking about below the code
#include <iostream>
#include<iomanip>
using namespace std;
/*
*
*/
int main()
{
double pay = .01, days, totalPay = 0.0;
int dollars, quarters, dimes, nickels, pennies;
cout << "Enter the number of days you worked to find out how many pennies you get." << endl;
cout << "Days worked: ";
cin >> days;
while ((days < 1) || (days > 31))
{
cout << "\nPlease enter a number between 1 and 31." << endl;
cout << "Days worked: ";
cin >> days;
}
for (int loopPay = 1; loopPay <= days; loopPay++)
{
cout << "Day " << loopPay << " " << pay << endl;
totalPay += pay;
pay = pay*2;
}
// this is where the problem is
cout << "\nTotal Pay is $" << setprecision(4) << totalPay << endl;
return 0;
}
small number like 10
Enter the number of days you worked to find out how many pennies you get.
Days worked: 10
Day 1 0.01
Day 2 0.02
Day 3 0.04
Day 4 0.08
Day 5 0.16
Day 6 0.32
Day 7 0.64
Day 8 1.28
Day 9 2.56
Day 10 5.12
Total Pay is $10.23
large number like 31 ( i left out all the days except the total cuz its long but its the same as above except output 31 times and correctly incremented)
Total Pay is $2.147e+07