I can't get the accumulator right,
It just keeps giving what the pay for the next day would be.
Example: Enter 5 for workDays, get 0.32 for total

Please let me know what it is I need to change.

//This program calculates the total pay earned over x days.
#include <iostream>
#include <iomanip>
using namespace std;

int main()

    double workDays, Day, pay;
    double total = 0.0;

    cout << "How many days will you be working? ";
    cin >> workDays;

    cout << "Day     Pay\n";

    // Finds the pay for each day worked.
    for (Day = 1; Day <= workDays; Day++)
    cout << Day << "\t" << pow(2, Day - 1)* 0.01 << endl;
    cout << setprecision(2) << fixed;

    //Finds the total pay for all days worked.
    pay = pow(2, Day - 1)* 0.01;
    total = total + pay;
    cout << "Your total pay will be $" << total << endl;

    return 0;
4 Years
Discussion Span
Last Post by triumphost

The reason is because at the end of your for-loop, Day will be incremented by 1!
In a for-loop, the initialization variable is always incremented at the end! Thus when your for-loop is finished, Day will have a value of 6 and not 5. The solution would be to just minus 1 from Day at the end of the loop. Maybe right before you cout<<Fixed;

It's either that OR you start your initialization at 0:

    for (Day = 0; Day < workDays; Day++)
        cout << Day + 1 << "\t" << pow(2, Day)* 0.01 << endl;

Edited by triumphost


Thank you, but unfortunately it didn't work :/

Everything the program outputs is correct except for the total it calculates, for the days I enter (5) it gives the total of 0.32 when it should be 0.31. I'm sure I am missing something obvious, but I just can't seem to figure out what.

Here is the output I am getting currently.

"How many days will you be working?" 5
Day 1 Pay 0.01
Day 2 Pay 0.02
Day 3 Pay 0.04
Day 4 Pay 0.08
Day 5 Pay 0.16
"Your total pay will be $0.32"


Well that's easy to fix. You're not keeping track of employee pay in your program.. You're currently just printing it and only keeping track of one Day (A.K.A. the day the user enters). In the for-loop, you simply add up all the pay. As such you get:

    for (Day = 0; Day < workDays; Day++)
        cout << Day + 1 << "\t" << pow(2, Day)* 0.01 << endl;
        pay += pow(2, Day)* 0.01;

Edited by triumphost

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.