Author Topic: Inflation Calculator  (Read 5521 times)

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
Inflation Calculator
« on: November 23, 2005, 07:19:40 AM »
Code: [Select]

/*
    Title           :   INFLATION CALCULATOR
    FileName        :   inflation.cpp
    Date Created    :   November 23, 2005
    Author          :   LF Cervantes
*/
#include <iostream>

using namespace std;

int main()
{
    float currentCost, inflation;
    int years;
   
    cout << " INFLATION CALCULATOR \n\n" ;
    cout << "Current item cost  (P)    :  ";
    cin >> currentCost;
    cout << "Inflation rate (%)        :  ";
    cin >> inflation;
    cout << "Projection period (years) :  ";
    cin >> years;
   
    float projectedCost = currentCost;
    int elapsed = 1;
    while ( elapsed <= years )
    {
        projectedCost *= (1 + inflation/100);
        elapsed++;
    }
    cout << "\n\nProjected cost  (P)       :  "
         << projectedCost << endl;
   
    system("PAUSE");
    return 0;
}
Analyze. Design. Develop. Debug. Deploy. Maintain.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Inflation Calculator
« Reply #1 on: November 26, 2005, 10:11:13 PM »
Hello all I am back from vacation.

Do you intend to calculate inflation on a yearly basis?  Perhaps one could give the formula for continuous compounding.  But then again if the code example is about loops then that would defeat the point ;).

Louie, do you discuss about error handling with cin in the class?
Gillius
Gillius's Programming http://www.gillius.org/

louiecerv

  • Moderator
  • User
  • *****
  • Posts: 85
re: Error Handling
« Reply #2 on: November 27, 2005, 10:05:50 AM »
Hi Gillius!

This course is for beginners with little or absolutely no background on programming.  We are just getting past the basics of C++, the programming structures - sequence, branching and looping.  

Yes, I agree that error handling should be introduced early.  Although in our book, (Problem Solving with C++ 4th ed) Savitch choose to discuss structured exception handling much later presumably to emphasize skills for efficient and error-free coding that rely less on these safety features.  

Of course other errors must be handled effectively even without exception handling.  For instance the cin statement could potentially return the wrong data from the keyboard (type mismatch error). My idea is to use a simple do-while loop that checks if the input is within the allowed range.  Perhaps you have other similar methods.  

Thanks for pointing it out though. I will emphasize simple error-handling in the next session.   :D
Analyze. Design. Develop. Debug. Deploy. Maintain.

Gillius

  • Administrator
  • User
  • *****
  • Posts: 147
    • http://www.gillius.org/
Inflation Calculator
« Reply #3 on: November 27, 2005, 04:52:42 PM »
Using a do/while loop would put the program in an infinite loop depending on what you do.  For example:

Code: [Select]
int someNumber;
do {
  cout << "Enter a non-negative integer: ";
  cin >> someNumber;
  if ( someNumber < 0 )
    cout << "Please enter a non-negative integer." << endl;
} while ( someNumber < 0 );


There is some error checking here, but what if the user types "H" at the prompt?  The program will never end after that point.  I realize though that error handling of this sort is probably a little more pedantic than needed for this course at this time.
Gillius
Gillius's Programming http://www.gillius.org/