repost: still stuggling w/for loop

• 02-19-2002
learnin2program
repost: still stuggling w/for loop
Hello... I am working on a For Loop Problem.. I have tried modifying it in many ways for it to work - but it still doesn't ... any help would be appreciated..
The problem: To make a For Loop that asks for an initial amount input.. ex. \$.01. It will double that amount every day for 30 days... 3 columns need to be displayed - the day - the input - the total...
ex. day input total
1 .01 .01
2 .02 .03
3 .04 .07
4 .08 .15 etc...

This is the code I have so far:
#include<iostream.h>
#include<math.h>

main()

{
double daynumber, amountentered=0, depositamount = 0, totalbal = 0, numberofday\
s;
int maxnumberofdays = 30;
cout<<"Please enter input amount"; cin>>amountentered;
cout<<"Day\tDeposit\tTotal\n";
for(numberofdays = 1; numberofdays<=maxnumberofdays; numberofdays++)
double amountentered = depositamount; // making the deposit amount the sames\
as amount entered
depositamount = (2*depositamount);

cout<<numberofdays<<"\t"; cout<<depositamount<<"\t";
cout<<totalbal<<"\t";
amountentered += amountentered;
return 0;
}

When I run the program it asks for the input amount... if I put in .01 as an example the output is:
Day Deposit Total
31 0 0

Any help would be appreciated...

marie10913@aol.com
• 02-19-2002
Unregistered
try this, and let me know if it works
int main( )
{
int day = 0, input = 0, total = 0;

cout << "Enter daily value" << endl;
cin >> input;

for( ; day < 30; day++ )
{
cout << "Day: " << day+1;
cout << "Input: " << input;
cout << "Total: " << (total+=input);

input *= 2.0;
}
}
• 02-19-2002
Unregistered
me again!!
it does works, just replace the variable declarations at the top with doubles not ints, ie

double day = 0, input = 0, total = 0;
• 02-19-2002
ygfperson
get the input for your program: amountentered
print your 3 column titles(including a newline)
first number is the count variable
second variable is 2 to the (n-1) power
third is the total
it can get confusing to try to sort it out. when it does i often just rewrite that section. try this:

Code:

```double var2=input; //input is the amount entered double var3=var2; //print titles, variables at day one for (int count=2;count<max_days+1;count++) {  var2*=2;  var3+=var2;  //print these variables out }```
x*=y means x=x*y
x+=y means x=x+y

you should be able to figure out the input/output parts
• 02-19-2002
learnin2program
repost: still stuggling with for loop
I tried the suggestion and modified the code a little to make it more readable... The code I tried was:
#include<iostream.h>
#include<math.h>

int main( )
{
int day = 0, input = 0, total = 0;

cout << "Enter daily value" << endl;
cin >> input;

for( ; day < 30; day++ )
{
cout << day+1<<"\t";
cout << input<<"\t";
cout << (total+=input)<<"\t\n";

input *= 2.0;
}
}

The output was
Day Input Total
1 0 0
2 0 0
3 0 0
4 0 0
etc

Any other suggestions would be appreciated
• 02-19-2002
Ryce
ill try...
#include <iostream.h>

int main()
{
float dValue, last, total;

cout << "Enter Daily Value: ";
cin >> dValue;
• 02-19-2002
Ryce
ill try...
#include <iostream.h>

int main()
{
float dValue, last=0, total=0;

cout << "Enter Daily Value: ";
cin >> dValue;
for(int i=0;i<30;i++)
{
dValue += last;
total += dValue;
cout << i+1 <<".) " << last << " " << total << endl;
}

return 0;
}

dont have a compiler so cant test but it should work.
• 02-19-2002
ygfperson
learning2program: your program works if you change input and total to double.
• 02-19-2002
Unregistered
like i said, after i tested it :)