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;
}
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:















