Thread: repost: still stuggling w/for loop

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    Unhappy 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...

    [email protected]

  2. #2
    Unregistered
    Guest

    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;
    }
    }

  3. #3
    Unregistered
    Guest

    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;

  4. #4
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    rewrite your program.
    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

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    11

    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

  6. #6
    Ryce
    Guest

    ill try...

    #include <iostream.h>

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

    cout << "Enter Daily Value: ";
    cin >> dValue;

  7. #7
    Ryce
    Guest

    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.

  8. #8
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    learning2program: your program works if you change input and total to double.

  9. #9
    Unregistered
    Guest
    like i said, after i tested it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-10-2001, 12:00 PM