Population program

Edited :)
Last edited on
here is your problem
for (year = 1; pop2 < pop1; year++)

"Please Enter the population of town B (must be larger than town A's): ";
Hi, I've put the condition as "pop2 < pop1" since that should be the point at which the loop should logically stop. Isn't it?
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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main()
{
	string city1, city2;
	int pop1, pop2, year = 1;
	double rate1, rate2;

	cout<< "Please enter town A's name: ";
	cin>> city1;

	cout<< "Please enter Town B's name: ";
	cin>> city2;
		
	cout<< "Please Enter the population of town A: ";
	cin>> pop1;
	cout<< "Please Enter the population of town B (must be larger than town A's): ";
	cin>> pop2;
		
	if (pop1 < pop2)
	{
		cout<< "Please enter the growth rate of town A: ";
		cin>> rate1;
		cout<< "Please enter the growth rate of town B (must be less than Town A's): ";
		cin>> rate2;
					
		if (rate1 <= rate2)
		{
			cout<< "ERROR:Growth Rate of Town A must be more than Town B's ";
			cin.ignore(2);
			return 1;
		}
		else if (rate1 > rate2)
		{
			cout << endl << left << setw(7) << "Year" << setw(20) << city1 << city2 << endl;
			while (pop1 < pop2)
			{
				cout << left << setw(7) << year << setw(20) << pop1 << pop2 << endl;
				pop1 += (pop1 * rate1); 
				pop2 += (pop2 * rate2); 
				year++;
			}
			cout << left << setw(7) << year << setw(20) << pop1 << pop2 << endl;
		}
	}
	else
	{
		cout << "ERROR: Town A's population must be less than town B" << endl;
	}

	cin.ignore(2);
	return 0;
}
Last edited on
Thanks Shinigami. However, the numbers seem to be off. The calculation has some error in it.

screenshot:
http://i48.tinypic.com/2h67urq.png
No, it works good. Only you use rate as 0.1, 0.2 or similar. It is your formula :D

pop1 = 100 + 100 * 0.1 = 110
if you use 10
pop1 = 100 + 100 * 10 = 1100
Last edited on
Ya, I just realized that!

instead of making the user enter decimals, I've changed the code to:

pop1 += (pop1 * (rate1/100));
pop2 += (pop2 * (rate2/100));
Shinigami, Thanks a lot for all your help!

I would appreciate if you could edit your post with the code since I don't want to get into trouble with my professor!
Last edited on
Moschops, I'm sorry. I realize it's counter-productive to this forum's mission but I just want to be on the safer side. Even though I came here with my original code (and 90% of it correctly coded), I just don't want to get into any sort of trouble. I hope you understand where I'm coming from.

It should not be a problem as I did not write this code, I only fixed some problems. And You can edit your code by changing < to > in for loop :D. And if you ask for help in forum you most know it will stay here :D
I hope you understand where I'm coming from.


I do understand. I hope you understand where we're coming from; if you're going to do this, please just don't bother asking at all.
Helping and being helped is part of normal life. Therefore you come from school, if collaboration is frowned upon.

Edit:
Albatross wrote:
Good day loyal subjects and complete slaves fellow programmers,

Oh it's a deep joke, right? Like, anyone who you help is your bitch fellow programmer, right?
Last edited on
moschops - I understand. Sorry for any trouble.

Shinigami - Thanks once again buddy.
Topic archived. No new replies allowed.