I am writing a program that is supposed to calculate how much a person earns in a month if the salary is one penny for the first day, two pennies for the second day, four pennies for the third day, and so on with the daily pay doubling each day the employee works. The program should ask the user for the number of days the employee worked during the month and should display a table showing how much the salary was for each day worked, as well as the total pay earned for the month. Here is the code i have so far:

#include <iostream>

using namespace std;

int main()
{
    //Declare variables
   
    int days;
    int input;
    int cents = 1;
    int total =0;

    cout<<"\nenter number of days worked: ";
    cin>>input;
    cin.get();

    for(int x = 0; x < input; x++)
    {
        days = cents;
        total += days;
        cents *= 2;
    }

    cout<<"\ntotal pay is :"<<total<<" cents";

    for(double x = 0; x < input; x++)
    {
        cout << "\nday"<<x+1<<" = "<<days<<" cents";
    }
    
    
    system("pause");
    return 0;
}

Can someone help me figure out what i am doing wrong?

Recommended Answers

All 5 Replies

If you need any doubles, they would be cents and total. You only need one loop, with an integer index counting the days. Take another stab at it and think it through step by step.

I tried to get rid of that loop but i cant seem to figure out how to display the days with penny pay in a table and to increment the number of pennies each day

Working it out on paper will give you a better idea of how to do it. Your input is the number of days, you have a variable called days, so read the input into that variable. Having a variable called input is too vague. Does the number of days change? No. The day may change, but the number of days doesn't. Decide what each variable represents and name it accordingly.

for(int x = 0; x < input; x++)
    {
        days = cents;
        total += days;
        cents *= 2;
    }

    cout<<"\ntotal pay is :"<<total<<" cents";

Why would days be equal to cents?

Again, input is not a good name. Replace it with days, or better yet, numDays. cents is a better variable name than input, as is total, but I think you should use variable names like:

numDays
dailyPayInCents
totalPayInCents

One, it is much easier to follow, and two, I think using those variable names will make it much easier to figure out where everything goes.

Okay now i'm confused....each time i run my loop i want it to go from day1..day 2...day 3...with the total number of cents doubling each time....my for statements are very squirly

Okay now i'm confused....each time i run my loop i want it to go from day1..day 2...day 3...with the total number of cents doubling each time....my for statements are very squirly

It won't do that because you double cents every time, then assign day to equal cents. If you want day to increment by one each time, have it be the loop counter variable:

for (int day = 0; day < numDays; day++)
{
     // adjust money variables and perhaps display something.
}

// display total
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.