Need help with pennies for pay!!!

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

Need help with pennies for pay!!!

 
0
  #1
Mar 14th, 2009
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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Need help with pennies for pay!!!

 
0
  #2
Mar 14th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

Re: Need help with pennies for pay!!!

 
0
  #3
Mar 14th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help with pennies for pay!!!

 
0
  #4
Mar 15th, 2009
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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 57
Reputation: cassie_sanford is an unknown quantity at this point 
Solved Threads: 0
cassie_sanford cassie_sanford is offline Offline
Junior Poster in Training

Re: Need help with pennies for pay!!!

 
0
  #5
Mar 15th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,819
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help with pennies for pay!!!

 
0
  #6
Mar 15th, 2009
Originally Posted by cassie_sanford View Post
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:

  1. for (int day = 0; day < numDays; day++)
  2. {
  3. // adjust money variables and perhaps display something.
  4. }
  5.  
  6. // display total
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC