• 0

c++ Amortization Library - can't make top down design


Question

I have this assignment to create an amortization library:

 

Amortization is a way to pay off a debt over time with equal payments.
Your assignment is to create both a program and a library to handle this problem.

The first part of the problem is to create an "Amort" Library which has at least 3 functions:

  1. double getPaymentAmount(), which returns the amount of each monthly payment ("rounded" up to the next cent).
  2. double getLoanAmount(), which returns the amount of the entire loan (rounded off to the nearest cent).
  3. int getNumberOfMonths(), which returns the number of monthly payments to be made ("rounded" up to the next integer).

These functions should each take 3 parameters and return a single value.
These functions should not do any I/O!

Given i, the monthly interest rate represented as a decimal (yearly rate  1200),  you can calculate:

  • the monthly payment amount;  if you know the principal and the number of monthly payments ("round" up to next cent),
  • the principal (total amount loaned);  if you know the payment amount and the number of months payments are to be made (round off to nearest cent),
  • the number of months;  if you know the payment amount and the total principal ("round" up to next integer).
The formulae are:
  payment= (1 + imonths * principal * i (1 + imonths - 1
  principal = (1 + i ) months - 1 * payment i * (1 + i ) months
  months = logpayment ) - logpayment - (principal * i ))     log( 1 + i )

These functions need to be compiled into your new "Amort.lib"
Don't forget to create an accompanying "Amort.h" file!
Don't forget that ALL functions must have a separate header block!

You will then write a program that helps a users calculate loan payments. You will present a menu of at least three options:

  1. calculate (P)ayment size
  2. calculate (L)oan size
  3. calculate (N)umber of payments
  4. (Q)uit

The user should be able to select an option using either the number or the upper or lower case letter.

  • If the user selects (1), (P) or (p), prompt for and input:
    • the annual interest rate >= 0, round to the nearest 1/8th point)
    • the size of the loan> 0, round to the nearest cent)
    • the number of payments > ).
  • If the user selects (2), (L) or (l), prompt for and input:
    • the annual interest rate >= 0, round to the nearest 1/8th point)
    • the size of each payment  > (0, round to the nearest cent)
    • the number of payments  > ().
  • If the user selects (3), (N) or (n), prompt for and input:
    • the annual interest rate  >(= 0, round to the nearest 1/8th point)
    • the size of the loan  > (0, round to the nearest cent)
    • the size of each payment  > (0, round to the nearest cent). 
      Do not allow the user to enter a payment size that is less than or equal to an interest-only payment.
  • If the user selects (4), (Q) or (q), the program should exit 

    The program should continually re-prompt and allow the user to re-enter any non-numeric or out-of-range values. It should not allow the user to enter Interest values of less than zero. If zero is entered for interest, the function called should be able to properly deal with it (i.e., don't allow division by zero).
    In none of the other cases should you allow the user to enter values of zero or less.

For all of these inputs, echo back to the user the number the program will actually use (e.g. if the user enters 88.854 for payment, the program should print back: Monthly Payment : $88.85). The program should then display the results in a friendly manner (money values should be printed to two decimal places, percentage to three decimal places, and the number of months should be a whole number).

 Finally, unless the user has selected the quit option, the sequence should start over again from the menu (use a loop).

 

I manage to make my own project with just 1 project and i just realized that ineed to utilize using multiple functions and i manage to get till this far but can't seem to get it working for some reason though:

i make 3 functions with 2 projects each project has Amort.h, 1 project has the Main.c and the other Amort.cpp

 

Amort.h:

double getPaymentAmount(int months, double principal, double interest);
double getPrincipalAmount(int months, double payment, double interest);
int getNumberOfMonths(double payment, double principal, double interest);

Amort.cpp:

#include <stdio.h>
#include <math.h>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip> 

using namespace std;

void calculationAmort(float P, float interestYear, int ny);
void printSch(int i, float P, float monthPaid, float interestPaid, float principalPaid, float newBalance);


void calculationAmort(float P, float interestYear, int ny)
{
float newBalance;
float interestMonth = (interestYear/1200);
float interestPaid, principalPaid, monthPaid;
int i;
int nm=ny*12;

    for(i=1;i<nm;i++)
      {
             
        
        interestPaid = P*interestMonth;//interest paid
        principalPaid = monthPaid-interestPaid; //princial paid
        newBalance = P-principalPaid; //new balance
                  
        printSch(i,P,monthPaid,interestPaid,principalPaid,newBalance); //print amortization table
        P = newBalance;  //update old balance    
             
        }
}
     
void printSch(int i, float P, float monthPaid, float interestPaid, float principalPaid, float newBalance)
{
     
   cout << std::setprecision(5) << fixed; 
   cout<<i<<"\t"<<P<<"\t\t"<<monthPaid<<"\t\t"<<interestPaid<<"\t\t"<<principalPaid<<"\t\t"<<newBalance<<"\n";
     
 }



double getPaymentAmount(int months, double principal, double interest)
{
	
	return (((1 + interest) ^ months)/((1 + interest) ^ months-1)) * principal * interest;
}

double getPrincipalAmount(int months, double payment, double interest)
{
	
	return ((1 + interest) ^ months-1)/(interest * (1 + interest) ^ months)) * payment;
}

int getNumberOfMonths(double payment, double principal, double interest)
{
	
	return (log(payment)-log(payment-(principal*interest)))/log(1+i);
}

Main.C:

#include <stdio.h>
#include <math.h>
#include "Amort.h"


char menu;
double principal, payment;
double annualRate, roundedRate;
int months;

int main(void)
{
	printf("1. Calculate (P)ayment size ");
	printf("2. Calculate (L)oan size ");
	printf("3. Calculate (N)umber of payments ");
	printf("4. (Q)uit ");
	printf("Select a menu");
	scanf_s("%c", &menu, 1);

	switch(menu)
	{
		case '1':
		case 'P':
		case 'p':
			printf("Enter the annual interest rate: ");
			scanf_s("%lf", &annualRate, 1);
			printf("Enter the size of the loan: ");
			scanf_s("%lf", &principal, 1);
			printf("Enter the number of payments: ");
			scanf_s("%d", &months, 1);
			printf("Payment is %.21f \n", getPaymentAmount(months, principal, roundedRate));
			break;

		case '2':
		case 'L':
		case 'l':
			printf("Enter the annual interest rate: ");
			scanf_s("%lf", &annualRate, 1);
			printf("Enter size of each payment: ");
			scanf_s("%lf", &payment, 1);
			printf("Enter the number of payments: ");
			scanf_s("%d", &months, 1);
			printf("Principal is %.21f \n" , getPrincipalAmount(months, payment, roundedRate));
			break;

		case '3':
		case 'N':
		case 'n':
			printf("Enter the annual interest rate: ");
			scanf_s("%lf", &annualRate, 1);
			printf("Enter size of the loan: ");
			scanf_s("%lf", &payment, 1);
			printf("Enter the size of each payment: ");
			scanf_s("%d", &months, 1);
			printf("Number of month is %.21f \n" , getNumberOfMonths(months, payment, roundedRate));
			break;

		case '4':
		case 'Q':
		case 'q':
			fclose;
	}


	//getLoanAmount(asdfsadf,asdfasdf);
	

}




//menu


//int main(int argc, char *argv[])
//{
//    float P,Iy,Im;
//    int n;
//    cout<<"Enter amount of loan:";
//    cin>>P;
//    cout<<"Enter interest rate per year:";
//    cin>>Iy;
//    cout<<"Enter number of years:";
//    cin>>n;
//    cout<<"=======================================================\n";
//    cout<<"\t\t\tAmortization schedule\t\t\t\n";
//    cout<<"=======================================================\n";
//    
//    calculationAmort(P,Iy,n);
//  
//    
//    
//    system("PAUSE");
//    return EXIT_SUCCESS;
//}


for the comment it's because i tried using those thing with one project and it work but when it gets to multiple files can't seem to get it working

Link to comment
Share on other sites

4 answers to this question

Recommended Posts

  • 0

i am trying to make it work using multiple thing

 

s (Amort.c, Amort.h, Main.c)

with

 

a single project with no addition files i am able to get it working but not with these multiple files

it always say 2 failed when i compile and this is the most recent one

 

Main.C

#include <stdio.h>
#include <math.h>
#include "Amort.h"


char menu;
double principal, payment;
double annualRate, roundedRate;
int months;

int main(void)
{
	printf("1. Calculate (P)ayment size ");
	printf("2. Calculate (L)oan size ");
	printf("3. Calculate (N)umber of payments ");
	printf("4. (Q)uit ");
	printf("Select a menu");
	scanf_s("%c", &menu, 1);

	
	switch(menu)
	{
		case '1':
		case 'P':
		case 'p':
			printf("Enter the annual interest rate: ");
			scanf_s("%lf", &annualRate, 1);
			printf("Enter the size of the loan: ");
			scanf_s("%lf", &principal, 1);
			printf("Enter the number of payments: ");
			scanf_s("%d", &months, 1);
			printf("Payment is %.21f \n", getPaymentAmount(months, principal, roundedRate));
			break;

		case '2':
		case 'L':
		case 'l':
			printf("Enter the annual interest rate: ");
			scanf_s("%lf", &annualRate, 1);
			printf("Enter size of each payment: ");
			scanf_s("%lf", &payment, 1);
			printf("Enter the number of payments: ");
			scanf_s("%d", &months, 1);
			printf("Principal is %.21f \n" , getPrincipalAmount(months, payment, roundedRate));
			break;

		case '3':
		case 'N':
		case 'n':
			printf("Enter the annual interest rate: ");
			scanf_s("%lf", &annualRate, 1);
			printf("Enter size of the loan: ");
			scanf_s("%lf", &payment, 1);
			printf("Enter the size of each payment: ");
			scanf_s("%d", &months, 1);
			printf("Number of month is %.21f \n" , getNumberOfMonths(months, payment, roundedRate));
			break;

		case '4':
		case 'Q':
		case 'q':
			fclose;
	}

	

}

Amort.C

#include <stdio.h>
#include <math.h>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <iomanip> 

using namespace std;

void calculationAmort(float P, float interestYear, int ny);
void printSch(int i, float P, float monthPaid, float interestPaid, float principalPaid, float newBalance);

double RoundedPct(double interestMonth) 
{ 
 double Eighths[] = {0.0, .125, .25, .375, .5, .625, .75, .875, 1.0}; 
 int wholePct; 
 double fracPct; 
 int i; 
 
 wholePct = (int)(100.0 * interestMonth); 
 fracPct = 100.0 * interestMonth - wholePct; 
 
 for (i = 0; i < 8; i++)  {
 if (Eighths[i] <= fracPct && fracPct <= Eighths[i + 1])
 { 
 if (fracPct - Eighths[i] <= Eighths[i + 1] - fracPct) 
 fracPct = Eighths[i]; 
 else fracPct = Eighths[i + 1]; 
 break; 
 } 
 } 
 
 interestMonth = (wholePct + fracPct)/100.0; 
 return interestMonth; 
} 

void calculationAmort(float P, float interestYear, int ny)

{
float newBalance;
float interestMonth = (interestYear/1200);
float interestPaid, principalPaid, monthPaid;
int i;
int nm=ny*12;

    for(i=1;i<nm;i++)
      {
             
        
        interestPaid = P*interestMonth;//interest paid
        principalPaid = monthPaid-interestPaid; //princial paid
        newBalance = P-principalPaid; //new balance
                  
        printSch(i,P,monthPaid,interestPaid,principalPaid,newBalance); //print amortization table
        P = newBalance;  //update old balance    
             
        }
}
     
//void printSch(int i, float P, float monthPaid, float interestPaid, float principalPaid, float newBalance)
//{
//     
//   cout << std::setprecision(5) << fixed; 
//   cout<<i<<"\t"<<P<<"\t\t"<<monthPaid<<"\t\t"<<interestPaid<<"\t\t"<<principalPaid<<"\t\t"<<newBalance<<"\n";
//     
// }



double getPaymentAmount(int months, double principal, double interest)
{
	double Payment; 
	return Payment = (pow (1 + interest, months))/(pow (1 + interest, months-1)) * principal * interest;
}

double getPrincipalAmount(int months, double payment, double interest)
{
	double Principal;
	return Principal = (pow (1 + interest, months-1))/(pow (interest * (1 + interest, months))) * payment;
}

int getNumberOfMonths(double payment, double principal, double interest)
{
	int Months;
	return Months = (log(payment)-log(payment-(principal*interest)))/log(1+interest);
}

can you tell me what's the prob kept telling me there are 2 failed when buildsolution

Link to comment
Share on other sites

  • 0

I managed to compile the code successfully after fixing a few errors.  i am working from the code you posted in your second post in this thread.

 

first problem, you're probably compiling as C code,since your file extensions are .c like Main.C. verify in the settings that it is compiling as C++ code,because you are using C++ headers like cstdlib.h.

 

second mistake, towards the bottom of main.c, you are not using fclose properly.you need to fix that.

  
case '4': 
case 'Q': 
case 'q': 
            fclose; 
 

you are using monthPaid variable without initializing it first.

principalPaid = monthPaid-interestPaid; //princial paid 
you have the printSch() function commented out,but its being used in the code. uncomment it,or else you will get a unresolved external symbol error.

and finally, theres a problem with the second use of the pow function, it doesn't only take 1 argument. you have some confusion with your parenthesis

return Principal = (pow(1 + interest, months - 1)) / (pow(interest * (1 + interest, months))) * payment;
  • Like 1
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.