the assignment:
Write a C++ program to estimate the springtime count of deer in a park for 10 consecutive years.
The population of any given year depends on the previous year's population according to the following calculation:
• If the lowest winter temperature was less than 0 degrees,
the deer population drops by 15%.
• If the lowest winter temperature was between 0 degrees F and 25 degrees F (inclusive),
the deer population drops by 10%.
• If the lowest winter temperature was between 26 degrees F and 30 degrees F (inclusive),
the deer population doesn’t change.
• If the lowest winter temperature was between 31 degrees F and 35 degrees F (inclusive),
the deer population rises by 12%.
• If the lowest winter temperature was higher than 35 degrees F,
the deer population rises by 14%.
what I have so far:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
using namespace std;
int main()
{
int year;
int pop;
int temp;
int x = 0;
cout << "Please enter the starting year.";
cin >> year;
cout << "Please enter the starting population for the deer.";
cin >> pop;
while(x < 10)
{
cout << "What was the lowest winter temperature in " << year << endl;
cin >> temp;
if (temp < 0)
{
pop = pop - (pop * 0.15);
cout << "In "<< year << ", the deer population is " << pop << endl;
}
else if (temp >= 0 && temp <= 25);
{
pop = pop - (pop * 0.10);
cout<< "In " << year << ", the deer population is " << pop << endl;
}
else if (temp >= 26 && temp <= 30);
{
pop = pop;
cout<< "In " << year << ", the deer population is " << pop << endl;
}
else if (temp >= 31 && temp <= 35);
{
pop = pop + (pop * 0.12);
cout<< "In " << year << ", the deer population is " << pop << endl;
}
else (temp > 35);
{
pop = pop + (pop * 0.14);
cout<< "In " << year << ", the deer population is " << pop << endl;
}
x++;
}
return 0;
}
|
Currently, it won't compile and it says "[Error] 'else' without a previous 'if'" for these lines:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
else if (temp >= 26 && temp <= 30);
{
pop2 = pop1;
cout<< "In " << year << ", the deer population is " << pop2 << endl;
}
else if (temp >= 31 && temp <= 35);
{
pop2 = pop1 + (pop1 * 0.12);
cout<< "In " << year << ", the deer population is " << pop2 << endl;
}
else (temp > 35);
{
pop2 = pop1 + (pop1 * 0.14);
cout<< "In " << year << ", the deer population is " << pop2 << endl;
}
|
I tried replacing "else if" and "else" as "if" just to see how the program ran. It was able to loop 10 times and calculate the new population. However, the year does not change.
I'd really appreciate any help to fix these two errors.