cant figure out whats wrong with this ....

This is a discussion on cant figure out whats wrong with this .... within the C++ Programming forums, part of the General Programming Boards category; i cant seem to figure out whats wrong....i have narrowed it down to this loop for (double i=1; i<years+1;++i) { ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    cant figure out whats wrong with this ....

    i cant seem to figure out whats wrong....i have narrowed it down to this loop

    for (double i=1; i<years+1;++i)
    {
    area_forested = (uncut_area + (forest_rate * uncut_area)*i)
    }

    its supposed to input X number of years (years), the rate of reforestation(forest_rate), the area left uncut(uncut_area).
    And then determine what the area forested will be after X years has passed.

    when i enter i year it output 105 which is correct, but when i enter 2 yrs it output 110.00 and it should be 110.25, it seems to drop off all the decimals? I have set all my variables to double.
    took out the set percission an then there was no decimal? I tested each section of my code an all is working fine except this for loop part? i get correct answers for years 1-9 without the decimal?

    if it helps the desk check i am using is 20years,rate .05,total area 10,000 , and uncut area 100. i was told the answer should be something like 635.33?

    ......any ideas?.......i even confussed my prof. haha.....any suggestions will be greatlly welcomed

  2. #2
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    what are you using to print the values?

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    this is what i put for output(for testing)

    cout << endl;
    cout << area_forested << endl;
    cout << i << endl;

    i just have that to print(output) the data because it kept giving the wrong answer i wanted to narrow it down, an found that this is where the problem is.........the for loop

  4. #4
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    try this.

    cout << endl;
    cout.precision(8);
    cout << area_forested << endl;
    cout << i << endl;

  5. #5
    Unregistered
    Guest
    make sure all your calculations are in float, otherwise some
    decimals will be dropped. Don't set precision on any of the
    calculations either. Keep everything float until your
    ready to print.

  6. #6
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    i followed both of you guys ideas but......

    when i changed all to Float..........( all of the variables)
    an took off the set precission for some reason it took off the decimals?.......
    an i used the cout ideas you suggested an nope.....
    this is the entire code.......maybe i am over looking something.....or maybe C++ is just actting werid.......

    #include<iostream.h>
    #include<iomanip.h>
    #include<math.h>

    // initializations........

    float area_number, // The number of the forested area.
    percent_forested, // The percent of forested area left.
    total_area, // The total size of the area.
    uncut_area, // The amount of uncut area .
    forest_rate, // The reforestation rate.
    area_forested, // The forested area left after one year.
    years; // The number of years.


    void main(void)
    {

    // prompt the user to enter the data.....
    cout << "This program will calculate what percent of the Total land\n";
    cout << "will be forested after X number of years have passed.\n";
    cout << "\n";
    cout << "Please enter the area number of the land you\n";
    cout << "to calculate data for.\n";
    cin >> area_number;
    cout << "Please enter the Total area(size) of the land.\n";
    cin >> total_area;
    cout << "Please enter the amount of Uncut land that is left.\n";
    cin >> uncut_area;
    cout << "Please enter the rate of Reforestation in decimal form.\n";
    cin >> forest_rate;
    cout << "After how many years of Reforestation do you wish to calculate data for?\n";
    cout << "Please enter the number of years.\n";
    cin >> years;

    // Set cout's ios flags and precision.
    // cout.setf(ios::fixed | ios::showpoint);
    // cout.precision(3);

    // calculate the data entered by the user, using for loop.

    for (float i=1; i < years+1 ; ++i)
    {
    area_forested = (uncut_area + (forest_rate * uncut_area)* i);

    }

    // display the calculated data to the user.

    cout << endl;
    cout.precision(8);
    cout << area_forested << endl;
    cout << i << endl;
    }

  7. #7
    Has a Masters in B.S.
    Join Date
    Aug 2001
    Posts
    2,267
    honestly im stumped... its just not doing the math right, i'll try it in CodeWarrior later,but im not sure the compiler's the problem...

    i'll PM Salem maybe he can figure this one out cause i sure as hell missed whatever it is...

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Code:
    for (float i=1; i < years+1 ; ++i)
    {
    area_forested = (uncut_area + (forest_rate * uncut_area)* i);
    }
    For all the good that this loop does, you may has well have gotten rid of it and typed
    Code:
    area_forested = (uncut_area + (forest_rate * uncut_area)* years );
    I rather suspect that you meant to modify one or more of the existing variables inside the loop - As it stands, uncut_area and forest_rate are constants.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    the loop is supposed to be used for.......

    the for loop he wanted us to use for the years i guess........to guess make sure we can...........it works but drops off the decimals for some odd reason

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Well tell me what 4 numbers I should type in (several examples), and the output you would expect.

    And why are area_number and total_area unused (you read them in, but don't use them).
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    here is an example

    area _number just to help the user know what area they are entering data for ( its supposed to be made for a person who knows little bout computers)

    total_area is used to determine the percent forested ( i took that part out when i was trying to find which part of the code didnt work, but will add it once this is working )

    example1
    area number : 45
    total area - 10,000
    forested area left - 100
    years - 20

    i need to input all those an it should figure out how much area will be left forested after X years.............using this formula
    area_forested = uncut_area + (forest_rate * uncut_area)
    an the take all that time the number of years....

    hope that helps.....i was told the answer for that is like 635 or 625.33? ?

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Code:
    Please enter the area number of the land you
    to calculate data for.
    45
    Please enter the Total area(size) of the land.
    10000
    Please enter the amount of Uncut land that is left.
    100
    Please enter the rate of Reforestation in decimal form.
    0.1                        <<<<< You forgot this one
    After how many years of Reforestation do you wish to calculate data for?
    Please enter the number of years.
    20
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    sorry bnout that :(

    this is my wholoe CODE as is so far ( if u have any ideas as to where a FOR loop would be best let me know)


    #include<iostream.h>
    #include<iomanip.h>
    #include<math.h>

    // initializations........

    double area_number, // The number of the forested area.
    percent_forested, // The percent of forested area left.
    total_area, // The total size of the area.
    uncut_area, // The amount of uncut area .
    forest_rate, // The reforestation rate.
    area_forested, // The forested area left after one year.
    years; // The number of years.


    void main(void)
    {

    // prompt the user to enter the data.....
    cout << "This program will calculate what percent of the Total land\n";
    cout << "will be forested after X number of years have passed.\n";
    cout << "\n";
    cout << "Please enter the area number of the land you\n";
    cout << "to calculate data for.\n";
    cin >> area_number;
    cout << "Please enter the Total area(size) of the land.\n";
    cin >> total_area;
    cout << "Please enter the amount of Uncut land that is left.\n";
    cin >> uncut_area;
    cout << "Please enter the rate of Reforestation in decimal form.\n";
    cin >> forest_rate;
    cout << "After how many years of Reforestation do you wish to calculate data for?\n";
    cout << "Please enter the number of years.\n";
    cin >> years;

    // Set cout's ios flags and precision.
    // cout.setf(ios::fixed | ios::showpoint);
    // cout.precision(3);

    // calculate the data entered by the user, using for loop.

    for (int i=1; i < years+1 ; ++i)
    {
    area_forested = (uncut_area + (forest_rate * uncut_area)* i);

    }

    // display the calculated data to the user.

    cout << endl;
    cout.precision(8);
    cout << area_forested << endl;
    cout << i << endl;
    }

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,533
    Like I said before, the loop is irrelevant, since it only depends on the final value of i (which will be the same value as years).

    Since all your example input (except perhaps forest_rate - which you still haven't stated what a relevant value would be), you have the following

    Code:
    area_forested = (uncut_area + (forest_rate * uncut_area)* years );
    Now, uncut_area and years are integer values (100 and 20 say).

    The only way you'll get a fraction is if forest_rate * uncut_area has a fractional component (printing this intermediate result might help).
    But then you multiply this by years, so even if you have a fraction ending in say 0.2, you will lose that when you multiply by years.

    Try
    Code:
    for (int i=1; i < years+1 ; ++i) {
        double temp = forest_rate * uncut_area;
        area_forested = (uncut_area + temp * i);
        cout << i << " " << temp << endl;
    }
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  15. #15
    Registered User
    Join Date
    Sep 2001
    Posts
    25

    thanks but :(

    thanks for trying..........but i think i am outta luck it dont make sense to use a For loop.......... i tried what u posted but all it out out was
    20
    1 5
    2 5
    3 5
    etc all the way to
    20 5 ?
    i dunno ill try ask him tues when its due if he dont helps ill take F an do if my way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help! I cannot figure out what I'm doing wrong here!
    By chsindian595 in forum C Programming
    Replies: 2
    Last Post: 08-02-2008, 03:18 AM
  2. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM
  3. please help, i can't figure out what's wrong
    By Leeman_s in forum C++ Programming
    Replies: 1
    Last Post: 06-05-2002, 09:13 PM
  4. What is wrong here?????
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 05-23-2002, 10:51 AM
  5. What did i do wrong? Code inside
    By The Rookie in forum C Programming
    Replies: 2
    Last Post: 05-18-2002, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21