Calculator Advice

Trying to make more efficient

Page 1 of 1

0 Replies - 724 Views - Last Post: 20 November 2009 - 05:58 PM Rate Topic: -----

#1 Lwrclass   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 17-November 09

Calculator Advice

Posted 20 November 2009 - 05:58 PM

Alright, so I have my code that meets requirements but it seems a bit too bulky to me. I have been trying to implement classes and strings in order to make it more compact but I can't seem to get it to work properly when I alter it. I was also trying to add a pure virtual function to this design but that did not work out too well for me either :) I was hoping that you guys could offer some suggestions as to how I can make this code more efficient. Thank you for any advice,

#include <cmath>																			//Libraries
#include <string>
#include <iostream>
#include <stdlib.h>
#include <iomanip>
#include <limits>

using namespace std;

void clearError(){
	cin.clear();																// Clear the error
			cin.ignore(numeric_limits<streamsize>::max(), '\n');						// Clear the input buffer
			cout << "Invalid input. Enter a valid number: ";
}

bool runAgain = true;																		  //runAgain for loop option at end of program.

char display(double amountOfLoan, int years, double interestRate){

	char choice = 'Y';

	double monthlyPrinciple;																  //Monthly principal
	double monthlyInterest;																	  //Monthly interest
	double balance;																			  //Balance of loan
	double monthRate = interestRate / (12 * 100);											  //convert interest rates
	double monthTerm = years * 12;															  //converts years to months			  
	double mortgage = amountOfLoan * (monthRate / (1-(pow((1 + monthRate), - monthTerm))));	  //mortgage Calculation
	cout << fixed;																			  //Format Decimals
	cout << setprecision(2);

	system("cls");																				//Clear the screen
	cout << "For a Loan Amount of $" << amountOfLoan << " with a term of " << years << " years" << "\nand an interest rate of " << interestRate << "%" << endl;
	cout << endl;						  
	cout << "The Monthly Mortgage Payment Will Be: $" << mortgage << " per month." << endl;
	cout << endl; 
	cout << "*******************************************************************************" << endl;
	cout << "\tThe Amortization Schedule for These Payments is Displayed Below" << endl;
	cout << "*******************************************************************************" << endl;
	cout << endl;


	int paymentNumber = 0;																	 //Resets payment number to zero for loop
	int displayLine = 0;																	   //Sets line display
	int counter;																			 //Counter to run inside loop		   

	for(counter = 1; counter <= monthTerm; counter++)									 // Sets initial count to 1 then runs math in the loop to display values
	{
		monthlyInterest = amountOfLoan * monthRate;									 //Complete calculations through the loop
		monthlyPrinciple = mortgage - monthlyInterest;
		balance = amountOfLoan - monthlyPrinciple;
		if (balance < 0)			  
			balance = 0;
		amountOfLoan = balance;		  
		paymentNumber++;				  


		if (displayLine == 0)														//Amortization Header
		{				   
			cout << fixed;
			cout << setprecision(2);
			cout << "Payment Number" << '\t' << "Principal Paid" << '\t' << "InterestPaid" << '\t' << "Remaining Balance" << endl;
			cout << "______________" << '\t' << "______________" << '\t' << "_____________" << '\t' << "_________________" << endl;
			cout << endl;		
		}
		cout.width(9);		  
		cout << paymentNumber << '\t' << "$" << monthlyPrinciple << '\t' << '\t' << "$" << monthlyInterest << '\t' << '\t' << "$" << balance << endl;
		displayLine++;


		if (displayLine == 12)													// Restricts the screen display to designated number of lines
		{
			system ("pause");
			displayLine = 0;
			system ("cls");
		}

	}																		// End loop


	return choice;
}

int main(){																			//Begin main function

	while (runAgain)																	//Start loop
	{  

		double amountOfLoan;																//Loan amount
		int years;																			//Loan Term
		double interestRate;																//Interest Rate

		system("cls");																	//Clear the screen  
		cout << "\t\t\tC++ Mortgage Calculator\n\n";

		cout << endl << "Please enter the Principle Amount of the Loan: $";				//Accept input
		while (!(cin >> amountOfLoan))
		{
			clearError();
		}

		cout << endl << "Please enter the Interest Amount of the loan: ";				 //Accept input
		while (!(cin >> interestRate))
		{
			clearError();
		}

		cout << endl << "Please enter the Term (In Years) of the Loan: ";				 //Accept input
		while (!(cin >> years))
		{
			clearError();
		}

		char quit;																			 //declaration for character variable
		quit = ' ';																			 //initialize quit variable

		while (quit != '2') {

			char option;																		 //declaration for character variable

			cout<<"\nPlease choose <1> to Display Table or <2> to Quit or <3> to start over: ";	//Allow user to see table, quit, or start over
			cin >> option;
			switch( option ){
		 case '1': quit = display(amountOfLoan,years,interestRate); break;
		 case '2': {return 0;}
		 case '3': {return main();}
		 default: cout <<"invalid Option! Please choose a valid option listed: " <<endl;

			}
		}
	}
	return 0;
}																					 //End Main



Is This A Good Question/Topic? 0
  • +

Page 1 of 1