C++

frantic.^

Famous
Joined
May 29, 2008
Messages
472
Edit: Oh and I forgot to mention...this is what the actual assignment is:

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%.)


**************************************
Hi. My program runs, there are no errors or warnings during run time. However, after i supply the 4 inputs. The while loop fails to initiate (I think-since there is no way to know this for sure), which leads me to believe I'm making a logical error somewhere in my code. And yes, this is a rough draft of the program, feel free to hate (as long as you give me good programming advice that will help me improve).

Mainly, what am I doing wrong with my while loop?
Did i leave out an important #include?
Did i leave anything else critical out?

I just want my calculations to run :crying:

#include <iostream>
#include <iomanip>

using namespace std;


int years = 0;
double populationA;
double populationB;
double growthRateA;
double growthRateB;
double convertRateA = growthRateA / 100;
double convertRateB = growthRateB / 100;
double newMembersA;
double newMembersB;

int main()
{
cout << "--------------------------------------------------------------------------------" << endl;
cout << "The population of a town A is less than the population of town B." << endl;
cout << "However, the poulation of town A is growing faster" << endl;
cout << "than the population of town B." << endl;
cout << "This program outputs after how many years the poulation" << endl;
cout << "of town A will be greater than or equal to the population of town B" << endl;
cout << "and the populations of both the towns at that time." << endl;
cout << "--------------------------------------------------------------------------------" << endl;
cout << endl;

cout << "Please enter the population of town A and it's growth rate: ";
cin >> populationA >> growthRateA;

cout << endl << "Thank you." << endl << endl << "Now please enter the population of town B and it's growth rate: ";
cin >> populationB >> growthRateB;

while (populationA < populationB)
{
if (0 < growthRateA && growthRateA < 1)
{
newMembersA == populationA * growthRateA;
populationA += newMembersA;
}
else if (1 <= growthRateA)
{
newMembersA == populationA * convertRateA;
populationA += newMembersA;
}
else
{
cout << "Invalid growth rate for town A.";
break;
}

if (0 < growthRateB && growthRateB < 1)
{
newMembersB == populationB * growthRateB;
populationB += newMembersB;
}
else if (1 <= growthRateB)
{
newMembersB == populationB * convertRateB;
populationB += newMembersB;
}
else
{
cout << "Invalid growth rate for town B.";
break;
}

years += 1;
}

if (populationA >= populationB)
{
cout << populationA << populationB << years << endl;
}

return 0;
}
 
Last edited:
I'll drag it into my environment and see what happens. BTW, if you absolutely need to track a program, why not force it to output to console (cerr; iostream) at stages (say right before the whlie loop, then inside the loop, et.al.)?
 
Alright, after having dinner and returning to this, I must say... please make it more readable next time.

Code:
#include <iostream>
#include <Windows.h> //my compiler does not halt when running the program unless forced to. in the spirit of not adding unnecessary values, I use Sleep instead.
using std::cout;
using std::cin;
using std::endl;

int main(){
	int years = 0;
	double populationA, populationB, newMembersA, newMembersB, growthRateA, growthRateB;

	cout << "--------------------------------------------------------------------------------n";
	cout << "The population of a town A is less than the population of town B.n";
	cout << "However, the poulation of town A is growing fastern";
	cout << "than the population of town B.n";
	cout << "This program outputs after how many years the poulationn";
	cout << "of town A will be greater than or equal to the population of town Bn";
	cout << "and the populations of both the towns at that time.n";
	cout << "--------------------------------------------------------------------------------n";
	cout << endl;

	cout << "Please enter the population of town A and it's growth rate: ";
	cin >> populationA >> growthRateA;

	cout << endl << "Thank you." << endl << endl << "Now please enter the population of town B and it's growth rate: ";
	cin >> populationB >> growthRateB;

	double convertRateA = growthRateA/100.00, convertRateB = growthRateB/100.00; //this needs to go after the intialization (in this case, user input)

	while (populationA < populationB){
		if (growthRateA > 0 && growthRateA < 1){
			newMembersA = populationA * growthRateA; //you had been using the logical equality operator (==) instead of the assignment (=) operator. Be wary.
			populationA += newMembersA;
		}

		else if (growthRateA >= 1){ 
			newMembersA = populationA * convertRateA; //again, == instead of =.
			populationA += newMembersA;
		}

		else{
			cout << "Invalid growth rate for town A.";
			break;
		}

		if (0 < growthRateB && growthRateB < 1){
			newMembersB = populationB * growthRateB; //once more.
			populationB += newMembersB;
		}

		else if (1 <= growthRateB){
			newMembersB = populationB * convertRateB; //once more.
			populationB += newMembersB;
		}

		else{
			cout << "Invalid growth rate for town B.";
			break;
		}

		years++; //changed.
	}

	if (populationA >= populationB){
		cout << populationA << populationB << years << endl;
	}

	Sleep(10000);//Sleeps (suspends) the program for 10000 milliseconds.
	return 0;
}
// some thoughts- make sure your loop can terminate! that while loop possibly an infinite loop (10, 1, 20, 2 for instance).

EDIT: spelling. whoa had some typo's in there.

Oh, while I'm at it. The global variables you were using = bad. Don't give something global scope unless you absolutely have to, or the work-around becomes too complex.

Please use whitespace- I realize its probably the forum that messed your spacing up but its always nice to indent and format for easy reading.

Keep up the good work though, nice to see someone sitting down and trying for a change instead of just asking for the solution.
 
Last edited:
Also move:
double convertRateA = growthRateA / 100;
double convertRateB = growthRateB / 100;

out of the main header and just above the while loop otherwise convertrate a and b are going to be consistently 0 and your loop will be infinite.


beat me Cacophonus, nice!

bear
 
Also move:
double convertRateA = growthRateA / 100;
double convertRateB = growthRateB / 100;

out of the main header and just above the while loop otherwise convertrate a and b are going to be consistently 0 and your loop will be infinite.


beat me Cacophonus, nice!

bear

\o/
 
why not force it to output to console (cerr; iostream) at stages (say right before the whlie loop, then inside the loop, et.al.)?

This is the cardinal rule of debugging.

Print out your variables, see what their value is. Put 'tag" print statements in key places, saying something as simple as "got to this point!" or "Inside while loop". Stuff like that...
 
This is the cardinal rule of debugging.

Print out your variables, see what their value is. Put 'tag" print statements in key places, saying something as simple as "got to this point!" or "Inside while loop". Stuff like that...

I have to admit those helped when I was coding... sometimes ya miss the little tiny things or happen to land a typo in there. I loved C++ too when I was in college...
 
I have to admit those helped when I was coding... sometimes ya miss the little tiny things or happen to land a typo in there. I loved C++ too when I was in college...


i do for a living....
 
how do you find it fun, it looks complicated and boring

Because you can have complete control over it. For people who like creating, that's the best thing you can ask for.

I personally really like HTML/CSS (and I want to learn Javascript, but with school, my brain can only retain so much >.<). I love the fact that I can type in some text, and have complete control over how it is displayed.

I really want to make a website, but I don't really know what to make the content about. >.<
 
Because you can have complete control over it. For people who like creating, that's the best thing you can ask for.

I personally really like HTML/CSS (and I want to learn Javascript, but with school, my brain can only retain so much >.<). I love the fact that I can type in some text, and have complete control over how it is displayed.

I really want to make a website, but I don't really know what to make the content about. >.<

Make it about how awesome I am. Then get a custom url for it "www.bokkchoiisthecoolestguyiknow.hereallyis"
 
how do you find it fun, it looks complicated and boring

I actually really enjoy it. My title is "Senior Software Engineer". I'm the guy the other developers turn to for help.

I've done my fair share of web page development. Always with database back ends (usually oracle, but sometimes mysql).

We have other people at my work who handle that now, allowing me to do more complicated stuff (like writing software that communicates with aircraft while they are flying).

Boring?

Nope.. and..besides, the pay-check is MORE than worthwhile :cool_old:
 

Latest posts

Back
Top