#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//function prototype
double calcPayment (double, double, int);

int main()
{
	//declare variables
	double carPrice = 0.0;
	double rebate = 0.0;
	double creditRate = 0.0;
	double dealerRate = 0.0;
	int term = 0;
	double creditPayment = 0.0;
	double dealerPayment = 0.0;
	char another = 'Y';

		do
		{
			//get input items
			cout << "Car price: ";
			cin >> carPrice;
			cout << "Rebate:";
			cin >> rebate;
			cout << "Credit union rate:  ";
			cin >> creditRate;
			cout <<"Dealer rate:  ";
			cin >>dealerRate;
			cout << "Term in years:  ";
			cin >> term;

			if (creditRate >=1)
				creditRate = creditRate / 100;
			//end if
			if (dealerRate >=1)
				dealerRate = dealerRate /100;
			//end if

			//call function to calculate payments
			creditPayment = calcPayment (carPrice - rebate,creditRate / 12, term * 12);
			dealerPayment = calcPayment (carPrice, dealerRate / 12, term * 12);

			//display payments
			cout <<fixed << setprecision (2) << endl;
			cout << "Credit union payment :  $"
				<<creditPayment <<endl;
			cout << "Dealer payment :  $"
				<<dealerPayment << endl;

			cout << endl << "Calculate more payments (y/n): ";
			cin >> another;
			cout << endl;
		}  while (toupper(another) == 'Y');

		return 0;
}  //end of main function

//*****function definitions*****
double calcPayment (double prin, double monthRate, int months)
{
	//calculates and returns a monthly payment
	double monthPay = 0.0;
	monthPay = prin * monthRate / (1-pow(monthRate + 1, -months));
	return monthPay;
}  //end of calcPayment function

Dani AI

Generated

As and pointed out, the display function belongs where the program currently prints the payments: call it after the payment values are calculated and before the loop asks whether to continue. Two clean placement options work equally well:

  • Define the display function above main (no prototype needed).
  • Or declare a prototype above main and put the definition after main (keeps main near the top for readability).

Keep the function focused on presentation only (do not re-calculate). Pass small built-in types by value and longer text by const&. A small, different example signature and usage:

void printPaymentLine(const std::string& label, double amount);

int main() {
    // after computing amounts...
    printPaymentLine("Credit union", credit_amount);
    printPaymentLine("Dealer", dealer_amount);
}

Implement formatting inside the function but restore stream state so other output is unaffected:

void printPaymentLine(const std::string& label, double amount) {
    std::ios_base::fmtflags f = std::cout.flags();
    std::cout << std::fixed << std::setprecision(2)
              << label << " monthly: $" << amount << '\n';
    std::cout.flags(f);
}

Design notes: prefer a single small function per responsibility (one that prints one labeled line, or a single displayPayments that prints both). For larger projects, declare prototypes in a header and put definitions in a .cpp file. If you get a compiler error like "displayPayment was not declared in this scope," add the prototype above main or move the definition above main.

Recommended Answers

All 2 Replies

After briefly looking at ye' code.. I would guess a displayPayment () would probably take place of everything going on between line #47 and #52.

You should put the displayPayment function around line 47. Take the print statements and wrap them in the function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.