Looping issue - HELP PLEASE

I'm doing a program for my class, Here's the objective:

Write a program that will predict the size of a population of organisms. The program should ask the user for the starting number of organisms, their average daily population increase (as a percent), and the number of days they will multiply. A loop should display the size of the population each day.

I can't seem to figure out how to make the variable "new_population" change after every day; it remains constant. Can anyone help?

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
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
using namespace std;

int main()
{
int start_num = 0;
double avg_daily_increase = 0.0;
int num_days = 0;
double new_population_percentage = 0.0;
double new_population = 0.0;


cout << "Enter # of Organisms: ";
cin >> start_num;

while(start_num < 2)
{
cout << "ERROR, # of Organisms must be > 2.\n";
cout << "Enter # of Organisms: ";
cin >> start_num;
}


cout << "Enter Daily Population Increase: "; 
cin >> avg_daily_increase;

while (avg_daily_increase < 0)
{
cout << "ERROR, Daily Population Increase must be > 0.\n";
cout << "Enter Daily Population Increase: ";
cin >> avg_daily_increase;
}


cout << "Enter # of Days: ";
cin >> num_days;

while(num_days < 1)
{
cout << "ERROR, # of Days must be > 1.\n";
cout << "Enter # of Days";
cin >> num_days;
}


cout <<"Day	 Population\n"; //chart
cout <<"--------------------------\n"; //chart breaker line


new_population_percentage = (start_num * avg_daily_increase) / 100;
new_population = new_population_percentage + start_num;

for(int x = 1; x <= num_days; x++)
{
cout << x << "\t\t" << new_population << endl;
}

system("pause");
return 0;
}
Follow the logic. Look at the code and ask yourself "what is the computer going to do here?".

Take a look at this:

1
2
3
4
5
6
7
8
9
// here you set new_population
new_population = new_population_percentage + start_num;

// then after that, you loop 'num_days' times
for(int x = 1; x <= num_days; x++)
{
cout << x << "\t\t" << new_population << endl;  // but in your loop all you do is print the same
  // new_population over and over again
}


If you want new_population to change each time that loop runs, then you need to change it inside the loop.
Thanks for the help, i got it!
Last edited on
Topic archived. No new replies allowed.