// Arup Guha
// 7/31/02
// This program asks the user information about buying a car, and prints
// out a payment plan for the user.
// Known "bug": Depending on how much payments are, sometimes the chart
//              doesn't print out the last column tabbed correctly.
#include <iostream.h>

bool printplan(double value, double rate, double payment);

void main() {

  double cost, rate, payment;

  // Set up output format for decimal numbers.
  cout.setf(ios::showpoint);
  cout.setf(ios::fixed);
  cout.precision(2);

  // Ask user for necessary information.
  cout << "Enter the total cost of your car." << endl;
  cin >> cost;
  cout << "Enter the interest rate of your loan." << endl;
  cin >> rate;
  cout << "Enter your monthly payment." << endl;
  cin >> payment;

  // Print out the chart.
  if (!printplan(cost, rate, payment))
    cout << "Sorry, your monthly payment isn not big enough." << endl;
}

// This function prints out a payment plan for a car based on its input
// parameters.
bool printplan(double value, double rate, double payment) {

  double monrate = rate/12; // Compute monthly interest rate.
  
  // Determine whether monthly payment is big enough or not.
  if (value*monrate >= payment)
    return false;

  // Start simulating the payments.
  int month = 1;
  double amtpaid = 0;
  cout << "Month\tPayment\tMoney Left to pay" << endl;
  
  while (value > 0) {

    // Calculate new value owed after one more month.
    value = (1+monrate)*value - payment;

    // Deal with last month, calculating actual last payment.
    if (value < 0) {
      payment = value + payment;
      value = 0;
    }

    amtpaid += payment; // Keep track of total paid thus far.

    // Output a line of the payment chart.
    cout << month << "\t$" << payment << "\t$" << value << endl;
    month++;
  }

  // Print out total paid for the car.
  cout << "Total amount paid = $" << amtpaid << endl;
  return true;
}
