943,660 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1239
  • C++ RSS
Mar 14th, 2009
0

Need help with pennies for pay!!!

Expand Post »
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?
Similar Threads
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Mar 14th, 2009
0

Re: Need help with pennies for pay!!!

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.
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Mar 14th, 2009
0

Re: Need help with pennies for pay!!!

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
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Mar 15th, 2009
0

Re: Need help with pennies for pay!!!

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:

C++ Syntax (Toggle Plain Text)
  1. numDays
  2. dailyPayInCents
  3. 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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Mar 15th, 2009
0

Re: Need help with pennies for pay!!!

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
Reputation Points: 11
Solved Threads: 0
Junior Poster in Training
cassie_sanford is offline Offline
57 posts
since Oct 2008
Mar 15th, 2009
0

Re: Need help with pennies for pay!!!

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:

C++ Syntax (Toggle Plain Text)
  1. for (int day = 0; day < numDays; day++)
  2. {
  3. // adjust money variables and perhaps display something.
  4. }
  5.  
  6. // display total
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Just out of curiosity...
Next Thread in C++ Forum Timeline: How to use VERY Long STL Vectors?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC