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:
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
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;
}
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
this is what he expects the program to do......
the formula ( uncut_area + (forest_rate * uncut_area)
using the numbers 10000 total area, 100 left uncut, .05 forest rate
u get 100 + (.05 * 100) which is 105 which equals how much will be left after 1 year
then 105 + (.05 *100) which is 110.25 is what is left after 2 yrs
then 110.25 + (.05*110.25) = 115.7625
an you continue until X number of years has passed ( years is the variable for years)
after 20 years have passed the answer is supposed to be like........635.33? i dont get it i get 105. then 110, then 115 without the decimals.........
werid to use a for loop i know.............i figured out i need some way to tell the lloop to take the number from each year an add it to the previous until X number of years has passed.........then output the answer as area_forested........