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

if this is the situation, i suggest that you try something like putting an equal sign on your days>=31. if the while loop is true then it will just continue to execute the commands inside the braces so 31 is > not >= so 31 is left out. just a suggestion. havent tried to edit your codes.

What happens if you change

... << setprecision(2) << ...

to

... << fixed << setprecision(2) << ...

?

What happens if you change

... << setprecision(2) << ...

to

... << fixed << setprecision(2) << ...

?

like magic the problem goes away!!!!

why is that. i am new to c++. i am going to google and see if i can find anything in the index of my book but obviously it somehow says only look at whats after the decimals. is the what the definition of fixed is or is it something else?


influence points for you, lol.

By default C++ prints floating-point numbers in scientific notation if they are so large that doing so will provide more compact output than fixed-point notation. By sending "fixed" to the output stream, you are asking it to print in fixed-point notation (i.e. with a decimal point at a fixed place in each output number) regardless of value.

If you replace "fixed" by "scientific," it will print in scientific notation regardless.

By default C++ prints floating-point numbers in scientific notation if they are so large that doing so will provide more compact output than fixed-point notation. By sending "fixed" to the output stream, you are asking it to print in fixed-point notation (i.e. with a decimal point at a fixed place in each output number) regardless of value.

If you replace "fixed" by "scientific," it will print in scientific notation regardless.

Thanks for the info, i didn't know either of those things and now i do as well as why.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.