c++ calculating deer population for consecutive 10 years

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.
Last edited on
Hi,

remove the semi- colons and the end of the else if statements, never have a semi colon before a compound statement (code in braces)

:+)
Some other things I noticed:

an else cannot have a condition. change it to else if An else is supposed to catch bad input.

Try to avoid having using namespace std;, it's not a problem now, but it will cause problems in the future. Google it. Just use std:: before each std thing.

Always initialise your variables to something, even if you get input for them straight away. This is a defensive measure and will save you one day.

Have meaningful names for your variables - it's a misnomer to abbreviate everything, IMO. If you name variables and functions really well, the code should read like telling a story.

pop needs to be double

Avoid having magic numbers like 10 or 26 in your code. make them const variables instead:

const unsigned int StudyDurationYears = 10;

and refer to that variable in the code.

Last edited on
Thank you so much for catching the semi colon and general advices!
Would having const variables fix the year issue?
Would having const variables fix the year issue?


No, the year doesn't change because you don't assign a new value to it.
Oh...that just made me realize I'm an idiot.
Thank you so much for your help
Topic archived. No new replies allowed.