Advertisement
Guest User

Jackbens C++ Town Growth Calc

a guest
Jul 7th, 2013
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.14 KB | None | 0 0
  1. // This program is meant to calculate and output population growth
  2. // information for two different towns based on user input.
  3. // It takes the population and growth rate of each town
  4. // and outputs how many years until the population of
  5. // the smaller town will be greater than the other
  6. // as well as the population of both towns at that time.
  7. //**************************************************************
  8.  
  9. #include <iostream>
  10. #include <iomanip>
  11. #include <string>
  12.  
  13. using namespace std;
  14.  
  15. int main()
  16. {
  17.  
  18.     string townA, townB;
  19.     int popA, popB, lead, year=0;
  20.     double rateA=0.00, rateB=0.00;
  21.  
  22.     cout << "Please enter the name of the first town"
  23.          << " and press enter: " << endl;
  24.  
  25.     getline (cin, townA);
  26.  
  27.     cout << "\nEnter the name of the second town: " << endl;
  28.  
  29.     getline (cin, townB);
  30.  
  31.     cout << "\nEnter the population & growth rate for "
  32.          << townA << " separated by space." << endl;
  33.  
  34.     cin >> popA >> rateA;
  35.  
  36.     cout << "\nEnter the population & growth rate for "
  37.          << townB << " separated by space." << endl;
  38.  
  39.     cin >> popB >> rateB;
  40.  
  41.     if (popA == popB && rateA == rateB)
  42.     {
  43.         cout << "\nBoth populations match & will grow equally."
  44.              << endl;
  45.         cout << townA << " Population: " << popA << " "
  46.              << "Growth Rate: " << rateA << "%\n"
  47.              << townB << "Population: " << popB << " "
  48.              << "Growth Rate: " << rateB << "%\n" << endl;
  49.  
  50.         system("Pause");
  51.  
  52.         return 0;
  53.     }
  54.  
  55.     if (popA == popB && rateA > rateB)
  56.     {
  57.         cout << "\nBoth populations match, but will grow differently."
  58.              << endl;
  59.         cout << townA << " Population: " << popA << " "
  60.              << "Growth Rate: " << rateA << "%\n"
  61.              << townB << " Population: " << popB << " "
  62.              << "Growth Rate: " << rateB << "%\n" << endl;
  63.  
  64.       system("Pause");
  65.  
  66.       return 0;
  67.     }
  68.  
  69.     else if (popA == popB && rateA < rateB)
  70.             {
  71.         cout << "\nBoth populations match, but will grow differently."
  72.              << endl;
  73.         cout << townA << " Population: " << popA << " "
  74.              << "Growth Rate: " << rateA << "%\n"
  75.              << townB << " Population: " << popB << " "
  76.              << "Growth Rate: " << rateB << "%\n" << endl;
  77.  
  78.       system("Pause");
  79.  
  80.       return 0;
  81.     }
  82.    
  83.     if (popB > popA && rateA == rateB)
  84.     {
  85.         cout << "\nAny population disparity will grow indefinitely."
  86.              << endl;
  87.         cout << townA << " Population: " << popA << " "
  88.              << "Growth Rate: " << rateA << "%\n"
  89.              << townB << " Population: " << popB << " "
  90.              << "Growth Rate: " << rateB << "%\n" << endl;
  91.  
  92.       system("Pause");
  93.  
  94.       return 0;
  95.     }
  96.  
  97.     if (popA > popB && rateA > rateB)
  98.     {
  99.         cout << "\nAny population disparity will grow indefinitely."
  100.              << endl;
  101.         cout << townA << " Population: " << popA << " "
  102.              << "Growth Rate: " << rateA << "%\n"
  103.              << townB << " Population: " << popB << " "
  104.              << "Growth Rate: " << rateB << "%\n" << endl;
  105.  
  106.       system("Pause");
  107.  
  108.       return 0;
  109.     }
  110.  
  111.     if (popB > popA && rateB > rateA)
  112.     {
  113.         cout << "\nAny population disparity will grow indefinitely."
  114.              << endl;
  115.         cout << townA << " Population: " << popA << " "
  116.              << "Growth Rate: " << rateA << "%\n"
  117.              << townB << " Population: " << popB << " "
  118.              << "Growth Rate: " << rateB << "%\n" << endl;
  119.  
  120.       system("Pause");
  121.  
  122.       return 0;
  123.     }
  124.    
  125.     if (popA > popB)
  126.     {
  127.         lead = 1;
  128.     }
  129.  
  130.     do
  131.     {
  132.         popA += (popA * (rateA / 100));
  133.         popB += (popB * (rateB / 100));
  134.         year++;
  135.     }
  136.     while (popA < popB);
  137.  
  138.     do
  139.     {
  140.         popA += (popA * (rateA / 100));
  141.         popB += (popB * (rateB / 100));
  142.         year++;
  143.     }
  144.     while (popA < popB);
  145.  
  146.     if (lead == 1)
  147.         {
  148.             cout << "\nThe population of " << townB <<
  149.             " will eclipse " << townA << " in: " << endl;
  150.             cout << year << " year(s).\n";
  151.  
  152.             cout << "\nAt that time " << townA << "'s population "
  153.                 << "will be:\n" << popA << " and "
  154.                 << townB << "'s population: " << popB << endl;
  155.         }
  156.     else
  157.     {
  158.         cout << "\nThe population of " << townA <<
  159.             " will eclipse " << townB << " in: " << endl;
  160.         cout << year << " year(s).\n";
  161.  
  162.         cout << "\nAt that time " << townA << "'s population "
  163.              << "will be:\n" << popA << " and "
  164.              << townB << "'s population: " << popB << endl;
  165.     }
  166.  
  167.     cin.ignore(2);
  168.  
  169.     system("Pause");
  170.  
  171.     return 0;
  172.  
  173. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement