Need help in loops and switches

0 Jose Ma · April 2, 2015
A reforestation equation expresses this growth as a function of the amount of timber standing and the reforestation rate. For example, if 100 acres are left standing after harvesting and the reforestation rate is 0.05, then 100+0.05 *100 or 105 acres can be reforested at the end of the first year. If nothing is harvested, then at the end of the second year, the number of acres is 105+0.05*105 or 110.25 acres.

2. Prompt the user to select one of the following calculation types:
a. Determine acres that can be harvested, given a regrowth period.
b. Determine regrowth period, given the acres to be harvested.
c. The program shall not allow the acres uncut to be greater than the total number of acres. Verify this both at the user input and during the calculations. An infinite amount of time cannot yield and infinite amount of wood.
3. Print to the screen a table that shows the following:
a. Year
b. Reforestation rate
c. Acres Reforested (i.e. ready for harvest)
4. Program should run multiple times until user selects to end program.
This is my code :

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int main()
{
int years, num, k;
double acres, uncut, rate,forested;
k=1;
while (int k==1)
{

cout << "Enter number of regrowth period (in years): \n";
cin >> years;
cout << "Enter total number of acres: \n";
cin >> acres;
cout << "Enter number of uncut acres: \n";
cin >> uncut;
cout << "Enter rate of reforestation: \n";
cin >> rate;

cout << "Press 1 to determine acres that can be harvested, given a regrowth period";
cout << endl;
cout << "Press 2 to determine regrowth period, given the acres to be harvested";
cout << endl;

cin >> num;

switch(num){
case 1:
acres = (acres)+(rate*acres);
break;
case 2:
forested=forested+(forested*rate);
break;
case 3:
break;

}
cout << "\n\n Reforestation Information \n";
cout << "\n";
cout << " Years Acres Forested \n";

cout.setf(ios::fixed);
cout.precision(2);

int counter(1);

do
{
uncut += uncut*rate;
cout << setw(6) << counter << setw(10) << uncut << endl;
++years;
++counter;
} while ((uncut <= acres) && (years > 0));
}
cout << "Would you like to perform another calculation?";
cout << "If yes enter 1, if not enter 0: ";
cin >> k;

if (k==1)
{

}
else
{
return 0;
}}

I need help making it look like this. ;
Years Acres Forested
1 105.00
2 110.25


Thank you

Post a Reply

Replies

Oldest  Newest  Rating
0 Brian Reed · April 3, 2015
Hello Jose Ma,

I loaded the code and fixed a few errors to make it run;
1- int in while (int k==1) has already been declared by int years, num, k;  so, it should be while ( k==1)
2- after the while test in the do loop a } was added that is not required
3- the switch cases don't do much but assign a new value to a case dependent variable. It looks like the do/while loop is processing only for case 1. Maybe you can put that loop within that case and make a new one for the case for forested=forested+(forested*rate);

Hope this helps you.

Cheers,
Brian 
  • 1

C++

107,344 followers
About

Used in many types of software including music players, video games, and many large scale applications.

Links
Moderators
Bucky Roberts Administrator