My brain has just switched off on me... have an easy program to write for a C++ class that involves a for loop with a running total, but my brain has shut down on what needs to be put into the for loop to complete the program.

The program needs to calculate how much a person would earn over a period of time (days) if his salary is one penny the first day, two the second, four the third, and continues to double each day. Program asks how many days for work, displays a table showing how much he earned each day, and how much total pay at the end of the period. Output needs to be displayed in a dollar amount, not number of pennies. There must be input validation to not accept a number less than one for days worked.

Here's where i'm at so far..

#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
    int days;
    double total = 0.0;
    cout << "How many days are you working? ";
    cin >> days;
    while (days < 1)
    {
    cout << "Please enter a valid number of days to work. ";
    cin >> days;
    }
    for (int count = 1;count <= days;count++)
    {
    }
return 0;
}

you should use

for(int day = 0; day < days; day++)

then just add 2^day (which is pow(day,2) in c++) to a running total.

Dave

Thanks, that's gotten me started.. but it also brings up something which i thought i knew how to do... presently the code is spitting out the total only, not what was made each day (day 1, 1 cent - day 2, 2 cents - day 3, 4 cents - etc) and i can't seem to figure out how to go about doing that at all...

inside the loop, just output
cout << "Current day: " << pow(day, 2);

I've tried that, but it miscalculates the days... here's the code, and the resulting output.

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
    int days;
    double total = 0.0;
    cout << "How many days are you working? ";
    cin >> days;
    while (days < 1)
    {
    cout << "Please enter a valid number of days to work. ";
    cin >> days;
    }
    for (int day = 1;day <= days;day++)
    {
        total = total + pow(day,2.0);
        cout << "Day: " << day << " Daily Salary " << pow(day,2.0) << " Total: " << total << endl;
    }
return 0;
}

How many days are you working? 5
Day: 1 Daily Salary 1 Total: 1
Day: 2 Daily Salary 4 Total: 5
Day: 3 Daily Salary 9 Total: 14
Day: 4 Daily Salary 16 Total: 30
Day: 5 Daily Salary 25 Total: 55

oops, oops, I told you wrong, its pow(2,day), not pow(day,2)

Still not quite right... that modification changed the total output, but not the daily salary one to be correct...

How many days are you working? 5
Day: 1 Daily Salary 1 Total: 2
Day: 2 Daily Salary 4 Total: 6
Day: 3 Daily Salary 9 Total: 14
Day: 4 Daily Salary 16 Total: 30
Day: 5 Daily Salary 25 Total: 62

Day 1 should read Daily Salary: 1 Total: 1
Day 2 should read Daily Salary: 2 Total: 3
and so on..

for (int day = 1;day <= days;day++)
    {
// I just declared a new var to avoid calling pow two times
        int current= pow(day-1,2.0)+1;
        total = total + current;
        cout << "Day: " << day << " Daily Salary " << current << " Total: " << total << endl;
    }

Giving the following output:

Day: 1 Daily Salary 1 Total: 1
Day: 2 Daily Salary 2 Total: 3
Day: 3 Daily Salary 5 Total: 8
Day: 4 Daily Salary 10 Total: 18
Day: 5 Daily Salary 17 Total: 35

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.