// Elaine Torrez                          CS1A                         Chapter 6, P.374, #15
// *****************************************************************************************
// *                                       POPULATION                                       *
// *--------------------------------------------------------------------------------------- *
// * This program calculates and displays the projected size of a population over a given   *
// * number of years, based on the starting size, annual birth rate, and annual death rate. *
// * The formula used to find the new population each year is:                              *
// *                                                                                       *
// *            N = P + (B * P) - (D * P)                                                   *
// *                                                                                       *
// * where N = new population, P = current population,                                      *
// *       B = birth rate, and D = death rate.                                              *
// *--------------------------------------------------------------------------------------- *
// * INPUT                                                                                  *
// *   startPop : starting population size (>= 2)                                           *
// *   birthRate: annual birth rate (non-negative, as a decimal)                            *
// *   deathRate: annual death rate (non-negative, as a decimal)                            *
// *   years    : number of years to project (>= 1)                                         *
// *--------------------------------------------------------------------------------------- *
// * OUTPUT                                                                                 *
// *   Displays the population size for each projected year.                                *
// *****************************************************************************************
 
#include <iostream>
#include <iomanip>
#include <limits>  // for input validation
using namespace std;
 
// ---------------- Function Prototype ----------------
double calcPopulation(double prevPop, double birthRate, double deathRate);
 
// ---------------------- MAIN -------------------------
int main()
{
    double startPop, birthRate, deathRate, newPop;
    int years;
 
    cout << fixed << setprecision(0);
 
    // ---------------------- INPUT ----------------------
    cout << "Enter the starting population size: ";
    cin >> startPop;
    while (cin.fail() || startPop < 2)
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "ERROR: Starting population must be at least 2. Re-enter: ";
        cin >> startPop;
    }
 
    cout << "Enter the annual birth rate (as a decimal, e.g., 0.03 for 3%): ";
    cin >> birthRate;
    while (cin.fail() || birthRate < 0)
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "ERROR: Birth rate cannot be negative. Re-enter: ";
        cin >> birthRate;
    }
 
    cout << "Enter the annual death rate (as a decimal, e.g., 0.01 for 1%): ";
    cin >> deathRate;
    while (cin.fail() || deathRate < 0)
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "ERROR: Death rate cannot be negative. Re-enter: ";
        cin >> deathRate;
    }
 
    cout << "Enter the number of years to project: ";
    cin >> years;
    while (cin.fail() || years < 1)
    {
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "ERROR: Number of years must be at least 1. Re-enter: ";
        cin >> years;
    }
 
    // -------------------- PROCESSING -------------------
    cout << "\nPopulation Projection:\n";
    cout << "------------------------------\n";
    newPop = startPop;
 
    for (int i = 1; i <= years; i++)
    {
        newPop = calcPopulation(newPop, birthRate, deathRate);
        cout << "Year " << setw(2) << i << ": " << newPop << endl;
    }
 
    return 0;
}
 
// ---------------- Function Definition ----------------
double calcPopulation(double prevPop, double birthRate, double deathRate)
{
    // Formula: N = P + (B * P) - (D * P)
    return prevPop + (birthRate * prevPop) - (deathRate * prevPop);
}