linuxman94

example

Oct 6th, 2013
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. // *************************
  2. //HW4prj5.cpp
  3. //
  4. //Author: Evan Anderson
  5. //Created: 9/03/2013
  6. //*************************
  7.  
  8. #include <iostream>
  9. using namespace std;
  10.  
  11. double calcInflationRate(double, double);
  12. double calcFutureCost(double, double);
  13.  
  14. int main() {
  15.     double oldPrice, newPrice, inflationRate, futurePrice; //Define the variables
  16.     char doAgain = 'y';
  17.  
  18.     while(doAgain == 'y' || doAgain == 'Y') {
  19.  
  20.         cout << "Enter the price of the item from 1 year ago: ";
  21.         cin >> oldPrice;
  22.         cout << "Enter the current price of the item: ";
  23.         cin >> newPrice;
  24.  
  25.         inflationRate = calcInflationRate(oldPrice, newPrice);
  26.         futurePrice = calcFutureCost(newPrice, inflationRate);
  27.  
  28.         cout.setf(ios::fixed);
  29.         cout.setf(ios::showpoint);
  30.         cout.precision(1);
  31.         cout << "\nThe inflation rate was calculated to be " << inflationRate * 100 << "%";
  32.         cout.precision(2);
  33.         cout << "\nThe item will cost $" << futurePrice << " in one year";
  34.         cout << "and $" << calcFutureCost(futurePrice, inflationRate) << " in two years";
  35.  
  36.         cout << "\n\nWould you like to repeat the calculation? (y or n): ";
  37.         cin >> doAgain;
  38.     }
  39.         return 0;
  40. }
  41.  
  42. double calcInflationRate(double oldPrice, double newPrice) {
  43.         return ((newPrice-oldPrice) / oldPrice);
  44. }
  45.  
  46. double calcFutureCost(double currentPrice, double inflationRate) {
  47.         return (inflationRate * currentPrice) + currentPrice;
  48. }
Advertisement
Add Comment
Please, Sign In to add comment