• 10-19-2011
cullman
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
• 10-19-2011
Salem
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?
• 10-20-2011
cullman
Quote:

Originally Posted by Salem
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;

}

• 10-20-2011
Salem
Why not just
pennies *= 2;
• 10-20-2011
cullman
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
• 10-20-2011
Alexandre
Just initialize it outside of your loop ? I don't see the problem.
• 10-20-2011
cullman
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
• 10-20-2011
cullman
I just did pennies =( pennies + 1)/100 to get the amount of pennies that day in dollar form