Homework lab using sentinel loop.

I have this homework problem to do:

The population of a town A is less than the population of town B. However, the population of town A is growing faster than the population of town B. Write a program that prompts the user to enter the population and growth rate of each town. The program outputs after how many years the population of town A will be greater than or equal to the population of town B and the populations of both the towns at that time. (A sample input is: Population of town A = 5000, growth rate of town A = 4%, population of town B = 8000, and growth rate of town B = 2%.)

I was told to use a sentinel loop to get the answer.
However, our book only covers sentinel loops on half a page. So, I am very lost.

Here is 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
49
50
51
52
53
54
55
#include<iostream>

using namespace std;

	int popA, popB; 
	double growthRATEa, growthRATEb, finalA, finalB, growA, growB;



int main()

{
	cout<< "This program will allow you to enter the population from two towns, \n";
		cout<< "A and B. The population of town A should be smaller than town B. You \n";
		cout<< "can then enter a percentage of both towns annual population increase. \n";
		cout<< "When entering the percentage of increase; make town A have a higher \n";
		cout<< "increase rate.\n\n";
		cout<< "The end result will show how many years it will take for the population \n";
		cout<< "of town A to be greater than or equal to the population of town B; and\n";
		cout<< "their total population at that time. \n \n";

		cout<< "Enter the population of town A: ";
		cin>> popA;

		cout<< "Enter the population of town B (must be larger than town A): ";
		cin>> popB;
				if (popA < popB)
				{
					cout<< "Enter a the growth rate of each town A. ";
					cin>> growthRATEa;
					cout<< "Growth rate of town B: ";
					cin>> growthRATEb;
						if (growthRATEa <= growthRATEb)
						{
							cout<< "Your input was invalid. ";
							return 1;
						}
						else if (growthRATEa > growthRATEb)
						{
						

							while (finalA > finalB)
								finalA = ((growthRATEa / 100) * (popA)) + popA;
								finalB = ((growthRATEb / 100) * (popB)) + popB;

							cout<< "Town A: "<< finalA <<endl;
							cout<< "Town B: "<< finalB <<endl;

						}
				}
				else if (popA >= popB)
				{
					cout<< "Your input was invalid. ";
					return 1;
				}


I'm not looking for the answer, but would REALLY appreciate someone that can point me to material to read, or code to look at to see what to do next.

Thank you in advance.
I could be wrong, but I am pretty sure the instructor said to use a sentinel loop.
closed account (D80DSL3A)
I think the while loop qualifies as sentinel since you are looping until a certain sentinal value is attained, as opposed to looping a fixed # of times.

I see some problems with your while loop:

1) Only line 43 will be executed in the while loop. I think you want both lines 43 and 44 to do so.
Enclose all lines for the loop in curly braces. ie:
1
2
3
4
5
while (finalA > finalB)
{
	finalA = ((growthRATEa / 100) * (popA)) + popA;
	finalB = ((growthRATEb / 100) * (popB)) + popB;
}


2) Lines 43 and 44 won't change the values of popA and popB. finalA and finalB will never change values in the loop!

3) finalA and finalB are not initialized before the while loop so the first evaluation of finalA > finalB will give an unpredictable result.

4) I think you want while( finalA < finalB ) since the town A population is the one which is initially smaller. Were you thinking it meant until(finalA > finalB)?

5) Skip the use of finalA, finalB, growA and growB altogether! These last 2 are never even used.
Try this instead:
1
2
3
4
5
6
7
while (popA < popB)
{
	popA = ((growthRATEa / 100) * (popA)) + popA;
	popB = ((growthRATEb / 100) * (popB)) + popB;
        // you might want to increment a variable counting the years here
        // since you are asked to output how long it takes for popA to equal popB
}

This corrects all of the flaws concerning the while loop.
I got MOST of it to work. I really really appreciate your help.
I'm a 36 year old single dad trying to complete my college degree.
Last week I was out of class for an infection in my eye. So I missed all the classes.

Again, I really appreciate your direction.
Topic archived. No new replies allowed.