I need help!

Hello guys. I am super new to programming, and need some help with an assignment im working on for my college class. We are making a program that determines how long it will take for one town to grow larger than the other given an initial population and a growth rate for each town. Town A must have a greater population than Town B, however the growth rate for Town A must be smaller than Town B's growth rate. So my program is supposed to output how long it takes Town B to become larger than Town A, population wise, and their populations. I hope this makes sense to you guys. If you need anymore info, please just let me know.

Here is what I have so far. Yes it is still very rough. I need help figuring out how to output how many years it took for Town B to become large. I also am not sure why my equations for the population growth arent working. Oh, and I am using Microsoft Visual C++ 2010, if that makes a difference. Thanks guys.

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
61
62
63
64
65
66
67
68
  #include <iostream>

using namespace std;

int main()
{
	int popA, popB, totalPopA, totalPopB, rateA, rateB;
	



	cout << "Enter Population of Town A: "; 
	cin >> popA;
	cout << "Enter Population of Town B (must be greater than Town A): ";
	cin >> popB;
	cout << endl;

	if (popA < popB)
	{
		cout << "Enter Growth Rate for Town A (must be greater than rate for Town B): ";
		cin >> rateA;
		cout << "Enter Growth Rate for Town B: ";
		cin >> rateB;

			if (rateA < rateB)
				{
					cout << "**Invalid Input**" << endl;
					cout << endl;

					system("Pause");
					return 0;
				}
				
			else if (rateA > rateB)
					{

						while (totalPopA < totalPopB)
						
							totalPopA = popA + ((rateA / 100) * popA);
							totalPopB = popB +((rateB / 100) * popB);




							cout << "Town A = " << totalPopA << endl;
							cout << "Town B = " << totalPopB << endl;

					}

	}

	else if (popA >= popB)
	{
		cout << "**Invalid Input**" << endl;
		cout << endl;

		system("Pause");
		return 0;

	}



	system("Pause");
	return 0;


}
Last edited on
Nobody can help me?:(
Really... anything would help:)
while (totalPopA < totalPopB)

you compared the value in totalPopA and totalPopB but haven't set it to any value yet, this will cause random data to be assigned to the variable

try to initialize first totalPopA and totalPopB to 0 in the declaration.
Last edited on
closed account (D80DSL3A)
Other problems:
1) Due to choice of int data type, rateA / 100 = 0.
Use type double instead for your variables.

2) The while loop at line 38 only contains the next line in its body. Use {} to enclose the 2 statements.

3) popA, popB never change. This gives an inaccurate result. Work with just popA and popB. Lose the totalPop variables.
eg.
1
2
3
4
5
6
while (popA < popB)
{
        popA = popA + ((rateA / 100) * popA);
	popB = popB +((rateB / 100) * popB);
        // increment a counter here to find the number of years for popA to be >= to popB
}
Well here is what i've got changed. But I'm still having trouble including an increment. Im still very new to this, and I cant think of a way to add an increment to a while loop. I tried a for loop, but since im using two variables instead of just "i" for example, I cant get that to work. And including brackets around the two statements in while loop keeps the cout statements from outputting. And including the cout statements in brackets makes the statements execute infinitely with something like 1.#INF... Its really frustrating.

But here is what ive changed anyway.

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
61
62
63
64
65
66
67
68
69
70
#include <iostream>

using namespace std;

int main()
{
	double popA, popB, rateA, rateB;

	cout << "Enter Population of Town A: "; 
	cin >> popA;
	cout << "Enter Population of Town B (must be greater than Town A): ";
	cin >> popB;
	cout << endl;

	if (popA < popB)
	{
		cout << "Enter Growth Rate for Town A (must be greater than rate for Town B): ";
		cin >> rateA;
		
		cout << "Enter Growth Rate for Town B: ";
		cin >> rateB;
		
		cout << endl;

			if (rateA < rateB)
				{
					cout << "**Invalid Input**" << endl;
					cout << endl;

					system("Pause");
					return 0;
				}
				
			else if (rateA > rateB)
					{
						while (popB >= popA)
						
						
							popA += popA * popA / 100;
							popB += popB * popB / 100;
						

							cout << "Town A = " << popA << endl;
							cout << "Town B = " << popB << endl;

						
						
							
					}

	}

	else if (popA >= popB)
	{
		cout << "**Invalid Input**" << endl;
		cout << endl;

		system("Pause");
		return 0;

	}

	


	system("Pause");
	return 0;


}
Last edited on
Common on guys. Please. This online class is killing me...
closed account (D80DSL3A)
You changed the formulas to:
1
2
popA += popA * popA / 100;// what happened to rateA ?
popB += popB * popB / 100;

instead of using what I suggested:
1
2
popA = popA + ((rateA / 100) * popA);
popB = popB +((rateB / 100) * popB);

and that is the reason for the infinite loop.
Your formulas would work if the rates were used:
1
2
popA += popA * rateA / 100;// these are OK too
popB += popB * rateB / 100;

When you get these formulas right try wrapping the lines in {} again. It should work.
To find the time, declare a variable int timeInYears = 0; near the top of your program.
Then include this line in the while loop: ++timeInYears;.
Last edited on
Wow, that was a terrible mistake that I did not catch. Thank you fun for pointing that out :) I will give that stuff a try and see what happens. Thank you.

Oh, and I changed the formulas around because it looks a lot cleaner, and thats what my instructor likes to see:) Gotta make the instructors happy... For example, I made a basic calculator last week. It ran and did what it was supposed to do. However, he told me there was a simpler, more efficient way to do the same thing. He took a lot of points off, and it mad me mad; but I guess that's part of learning this stuff.
Last edited on
closed account (D80DSL3A)
I totally agree that the new assignments look cleaner.
They could be cleaned up even more. If you were to divide the rates by 100 right after they are read:
1
2
cin >> rateA;
rateA /= 100.0;

Then you could write:
popA += popA * rateA;
or even
popA *= 1 + rateA;
Its funny you mention that, because I think I tried that, but something didnt work right. But at the time, there were a lot of problems with my code... so yeah. And here is what I have after its been corrected and cleaned up.

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
61
62
63
64
65
66
67
68
69
#include <iostream>

using namespace std;

int main()
{
	double popA, popB, rateA, rateB;
	int timeInYears;

	cout << " Enter Population of Town A: "; 
	cin >> popA;

	cout << " Enter Population of Town B (must be greater than Town A): ";
	cin >> popB;

	cout << endl;

	if (popA < popB)
	{
		cout << " Enter Growth Rate for Town A: ";
		cin >> rateA;
		
		cout << " Enter Growth Rate for Town B (must be less than Town A rate): ";
		cin >> rateB;
		
		cout << endl;

			if (rateA < rateB)
			{
				cout << " **Invalid Input: The Town A rate must be > the Town B rate**" << endl;
				cout << endl;

				system("Pause");
				return 0;
			}
				
					else if (rateA > rateB)
					{

						while (popB > popA)				
						{
							popA += popA * rateA / 100;
							popB += popB * rateB / 100;

							++timeInYears;

						}
							cout << "After " << timeInYears << " years, the population of Town A will be "; 
							cout << popA << ", and the population of Town B will be " << popB << "." << endl;
							cout << endl;

					}

	}

	else if (popA >= popB)
	{
		cout << "**Invalid Input: The Town B population must be > the Town A population**" << endl;
		cout << endl;

		system("Pause");
		return 0;

	}

	system("Pause");
	return 0;

}


So the only thing I am having problems with now, are the number outputs. When I start entering really large numbers, the output appears to be in something similar to scientific notation. Unless you know that notation, it'll be hard for your average person to read it. So I guess what I'm asking, is how to get it to output the actual number...

Again, thank you for your help.
Instead of using doubles, use other data types. I believe there is one named long double or signed double,something like that but search it up here on c++
Well I figured out how to get rid of the decimal, and the scientific notation using...

1
2
	cout.setf(ios::fixed);
	cout.precision(0);


But now its not rounding at all. I have read a lot on rounding, but there are so many recommendations that its overwhelming. Not to mention, a lot of them make it seem like Im gonna have to change a lot of my code to use 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>

using namespace std;

int main()
{
	double popA, popB, rateA, rateB;
	int timeInYears;
	cout.setf(ios::fixed);
	cout.precision(0);

	cout << " Enter Population of Town A: "; 
	cin >> popA;

	cout << " Enter Population of Town B [must be > Town A]: ";
	cin >> popB;

	cout << endl;

	if (popA < popB)
	{
		cout << " Enter Growth Rate for Town A: ";
		cin >> rateA;
		rateA /= 100;
		
		cout << " Enter Growth Rate for Town B [must be < Town A rate]: ";
		cin >> rateB;
		rateB /= 100;

		cout << endl;

			if (rateA < rateB)
			{
				cout << " **Invalid Input: The Town A rate must be > the Town B rate**" << endl;
				cout << endl;

				system("Pause");
				return 0;
			}
				
					else if (rateA > rateB)
					{

						while (popB > popA)				
						{
							popA += popA * rateA;
							popB += popB * rateB;

							++timeInYears;

						}
							cout << " After " << timeInYears << " years, the population of Town A will be "; 
							cout << popA << "," << endl;
							cout << " and the population of Town B will be " << popB << "." << endl;

							cout << endl;

					}

	}

	else if (popA >= popB)
	{
		cout << "**Invalid Input: The Town B population must be > the Town A population**" << endl;
		cout << endl;

		system("Pause");
		return 0;

	}

	system("Pause");
	return 0;

}
Last edited on
Can you point out which part of code it's not rounding? Try using floor(expression)+.5
closed account (D80DSL3A)
What values are you entering and what values are you getting for the results?

timeInYears may have a ridiculous value because you didn't provide an initial value.Declare as
int timeInYears = 0;// the =0 part is critical!!!

You are welcome for the help.
Last edited on
Well I figured it out. I just saw your message on the setting timeInYears to 0, but I already turned the assignment in. But to make myself feel better, I did go back and set it to 0, then ran the program again. I remembered the years it output, then took the = 0 away, and ran it again, and nothing changed. The years output remained the same. So hopefully that won't bite me in the butt later. But here is what I turned in... and yes, I realize the commenting is a little excessive, but I got counted off for not having enough in my last assignment. Anyway, here it is :)

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
 /* Chapter 5 Programming Assignment
  * CSIS 123 Programming Fundamentals
  * Fall 2013
  * This program will find how many years it takes for a town with a
  * small population and a larger growth rate to grow larger than a town
  * with a larger population, but a smaller growth rate. 
 */
  
#include <iostream>

using namespace std;

int main()
{
	double popA, popB, rateA, rateB;
	int timeInYears;
	cout.setf(ios::fixed);
	cout.precision(0);

	cout << endl;

	cout << " Enter the Population of Town A: "; 
	cin >> popA;

	cout << " Enter the Population of Town B [must be > Town A]: ";
	cin >> popB;

	cout << endl;

	if (popA < popB) //This if statement will determine wether or not the entered cin statements meet the requirements to move on in the code.
	{
		cout << " Enter the Growth Rate for Town A: ";
		cin >> rateA;
		rateA /= 100; //Turns the rate from a whole number to a decimal for use in a future equation.
		
		cout << " Enter the Growth Rate for Town B [must be < Town A rate]: ";
		cin >> rateB;
		rateB /= 100;//Turns the rate from a whole number to a decimal for use in a future equation.

		cout << endl;

			if (rateA <= rateB) // This determines if the code meets the requirements to proceed in the code. This specifically outputs the error message, as the rate requirements were not met in order to proceed.
			{
				cout << " **Invalid Input: The Town A rate must be > the Town B rate**" << endl;
				cout << endl;

				system("Pause");
				return 0;
			}
				
					else if (rateA > rateB) //If the rates meet these requirements, then the code will proceed on.
					{

						while (popB > popA) // As long as the population of Town B is greater than Town A, the equations below will execute.
						{
							popA += popA * rateA; //This equation multiplies the rate and the population of Town A, then adds the previously stored value for Town A until the population for Town A becomes larger than Town B. 
							popB += popB * rateB;

							++timeInYears; //Increment value for the year output.

						}
							cout << " After " << timeInYears << " years, the population of Town A will be "; 
							cout << popA << "," << endl;
							cout << " and the population of Town B will be " << popB << "." << endl;

							cout << endl;

					}

	}

	else if (popA >= popB) //If the entered population values do not meet the code requirements, then the code will output the error message, then exit.
	{
		cout << "**Invalid Input: The Town B population must be > the Town A population**" << endl;
		cout << endl;

		system("Pause");
		return 0;

	}

	system("Pause");
	return 0;

}
Last edited on
One tip is you should avoid such long lines of code. Try to keep them under the 80th column of text. You could put your comments on the lines before or after or break them into multiple lines of comments to make your code easier to read with out having to scroll left and right. Horizontal scroll is pretty annoying IMHO.
Topic archived. No new replies allowed.