Thread: Adding pennies together every day they add together HELP

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    10

    Adding pennies together every day they add together HELP

    Hi this is a homework assignment, and I dont want anybody to just give it away because I really want to learn my self but I am so stumped.. I understand I need too add the first number of pennies together but I am so confused on how to do this, would i use 2 variables to get this and add them together? But if i did this I dont think it would work in one cout statement since Day 1 should start with 1 penny... so thats why I was thinking I needed to use pennies++; but I have no clue somebody please point me in the right direction here is the code so far
    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
     
    int main()
    {
        int totalDays = 0, pennies = 0, days = 1, pay;
        do{
            cout << "How many days have you worked? ";
            cin >> totalDays;
            
            if(totalDays < 1)
                cout << endl << "Pick up some hours please...re-enter." << endl 
                     << endl;
        }while(totalDays < 1);
        
        cout << setw(5) << left << endl << "Day #" << setw(17) << right << "Pay" << endl
             << "------------------------" << endl;
        
        while(days <= totalDays)
        {      
             pennies += days;
             cout << setw(5) << left << "Day " << days++ << setw(17) << right << pennies;
             
             
             cout << endl;
             
        }
     
    system("pause");
    return 0;
    }
    it should be 1 penny for day 1
    2 pennies for day 2
    4 pennies for day 3
    8 pennies for day 4 because 1+2+4 plus the 1 penny for that day...and this makes me think like the pennies for all those days have to have seperate variables but I know for sure that thats not what im suppose to do...so confused
    Last edited by cullman; 10-19-2011 at 09:03 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    2 pennies for day 2
    4 pennies for day 3
    8 pennies for day 4
    So are you supposed to be printing powers of two?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Quote Originally Posted by Salem View Post
    2 pennies for day 2
    4 pennies for day 3
    8 pennies for day 4
    So are you supposed to be printing powers of two?
    thats what i was thinking at first but it doesnt work...

    NEVERMIND! before i posted this i wanted to try it out a couple times and i got it!!! i made the counter start at -1 so it would go to 0 and 1
    so power to zero makes 1 penny and then power of 2 and so on.. heres the code!

    Code:
    #include <iostream>
    #include <cmath>
    #include <iomanip>
    using namespace std;
     
    int main()
    {
        int totalDays = 0, days = 1; 
        double pennies,pay, x=-1;
        do{
            cout << "How many days have you worked? ";
            cin >> totalDays;
            
            if(totalDays < 1)
                cout << endl << "Pick up some hours please...re-enter." << endl 
                     << endl;
        }while(totalDays < 1);
        
        cout << setw(5) << left << endl << "Day #" << setw(17) << right << "Pay" << endl
             << "------------------------" << endl;
        
        while(days <= totalDays)
        {     
             x++;
             pennies = pow(2, x);
             cout << setw(5) << left << "Day " << days++ << setw(17) << right << pennies;
             
             
             cout << endl;
             
        }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Why not just
    pennies *= 2;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Because day 1 wouldnt start with penny unless i set penny to 1 and did two seperate cout statements but then the rest of the program wouldnt work because i needed to add up all the pennies and cout total earned

  6. #6
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Just initialize it outside of your loop ? I don't see the problem.

  7. #7
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    Well i got it now but i dont understand what you mean initialize it out of the loop, i did initialize and declare all my variables in the beginning

  8. #8
    Registered User
    Join Date
    Oct 2011
    Posts
    10
    I just did pennies =( pennies + 1)/100 to get the amount of pennies that day in dollar form

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. adding to a set
    By Suchy in forum C++ Programming
    Replies: 4
    Last Post: 11-28-2006, 12:53 AM
  2. Adding!
    By Legacycx in forum C# Programming
    Replies: 2
    Last Post: 02-17-2005, 04:12 PM
  3. adding a VB gui
    By DMaxJ in forum C++ Programming
    Replies: 2
    Last Post: 02-10-2003, 09:35 AM
  4. adding to C++
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 01-06-2003, 01:02 PM
  5. VS .NET .lib adding
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 07-24-2002, 04:43 PM