Я начинающий, у которого есть некоторые вопросы, касающиеся моей домашней работы для школы.Как увеличить погоду/бесконечный цикл
С моим текущим кодом я застрял в бесконечном цикле, который я предполагаю, потому что мое состояние никогда не выполняется (Pop A больше, чем Pop B), и я не уверен, как двигаться дальше. Я также не уверен, как правильно увеличивать/вычислять годы, необходимые для того, чтобы город А превосходил город B, но это то, что у меня есть до сих пор.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int townA;
int townB;
double growthA;
double growthB;
double finalA;
double finalB;
int years = 0;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
while ((townA <= 0) && (growthA <=0) && (townB > townA) && (growthB < growthA) && (growthB > 0))
{
cout << "Error: Values must be positive, please try again." << endl;
cout << "Enter the population of town A: ";
cin >> townA;
cout << "Enter the growth rate of town A: ";
cin >> growthA;
cout << "Enter the population of town B: ";
cin >> townB;
cout << "Enter the growth rate of town B: ";
cin >> growthB;
cout << endl;
}
years = 0;
while (townA <= townB)
{
finalA = ((growthA/100) * (townA)) + townA;
finalB = ((growthB/100) * (townB)) + townB;
cout << "It took Town A " << years << " years to exceed the population of Town B." << endl;
cout << "Town A " << finalA << endl;
cout << "Town B " << finalB << endl;
}
cout << "\n\n" << endl; // Teacher required us to output it to the screen incase anyone is wondering why I have this block
cout << setw(3) << "Town A" << setw(15) << "Town B" << endl;
cout << setw(3) << growthA << "%" << setw(10) << growthB << "%" << endl;
cout << setw(3) << townA << setw(7) << townB << endl;
cout << "Year" << endl;
cout << "--------------------------------------------" << endl;
return 0;
}
@TonyD, конечно :) –