# cant figure out whats wrong with this ....

Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last
• 10-18-2001
ii3ejoe
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
:confused:
• 10-18-2001
no-one
what are you using to print the values?
• 10-18-2001
ii3ejoe
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
• 10-18-2001
no-one
try this.

cout << endl;
cout.precision(8);
cout << area_forested << endl;
cout << i << endl;
• 10-19-2001
Unregistered
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
• 10-19-2001
ii3ejoe
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;
}
• 10-19-2001
no-one
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...
• 10-19-2001
Salem
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.
• 10-20-2001
ii3ejoe
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-21-2001
Salem
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).
• 10-21-2001
ii3ejoe
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? ?
• 10-21-2001
Salem
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```
• 10-21-2001
ii3ejoe
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;
}
• 10-21-2001
Salem
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; }```
• 10-21-2001
ii3ejoe
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 :)
Show 80 post(s) from this thread on one page
Page 1 of 2 12 Last