my program display nan and 0

my programing only display nan and 0 after i run it and my ecpected value is the monthly amount of loan and total loan i need some help, i have been stuck on this for a week and i have try try to fix it but i cant find out where the problem is, thank you !!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
using namespace std;
#include <iomanip>
#include <cmath>
class loan{
private:
double rates;
double amount;
double payment;
double totalloan;
double percent;
int years;
public:
double annualInterestRate(double);
int NumberOfYears(int);
double loanAmount(double);
double monthlypayment;
double getannualInterestRate(double);
int getNumberOfYears(int);
double getloanAmount(double);
double setannualInterestRate(double);
double setNumberOfYears(int);
double setloanAmount(double);
double getMonthlyPayment(){return ((1 +percent)* amount)/years;}
double getTotalPayment(){return totalloan=payment*years;}
};
double loan::setannualInterestRate(double rates){
return rates;
}
double loan::setNumberOfYears(int years){
return years;
}
double loan::setloanAmount(double amount){
return amount;
}
double loan::getannualInterestRate (double percent) {
return percent;
}
int loan::getNumberOfYears(int years){
return years;
}
double loan::getloanAmount(double money){
return money;
}
int main(){
loan.L;
L.setloanAmount(40000);
L.setannualInterestRate(5.5);
L.setNumberOfYears(15);
cout<<" your monthly payment is "<<L.getMonthlyPayment()<<".";
cout<< "Your total payment is"<< L.getTotalPayment()<<endl;
}

Last edited on
You forgot to update the value of the member variables in your setter functions.
I change the programing a little bit but i have trouble and the program doesnt run!! thank you !!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cmath>
using namespace std;

class Loan
{
public:
  Loan();
  Loan(double newAnnualInterestRate, int newNumberOfYears, double newLoanAmount);
  double getAnnualInterestRate() const;
  int getNumberOfYears() const;
  double getLoanAmount() const;
  void setAnnualInterestRate(double newAnnualInterestRate);
  void setNumberOfYears(int newNumberOfYears);
  void setLoanAmount(double newLoanAmount);
  double getMonthlyPayment() const;
  double getTotalPayment() const;
  static double getMonthlyPayment(double annualInterestRate, int numberOfYears, double loanAmount);
  static double getTotalPayment(double annualInterestRate, int numberOfYears, double loanAmount);

private:
  double annualInterestRate;
  int numberOfYears;
  double loanAmount;
};

Loan::Loan()
{
  annualInterestRate = 5.5;
  numberOfYears = 15;
  loanAmount = 40000;
}

Loan::Loan(double newAnnualInterestRate, int newNumberOfYears, double newLoanAmount) 
{
  annualInterestRate = newAnnualInterestRate;
  numberOfYears = newNumberOfYears;
  loanAmount = newLoanAmount;
}

double Loan::getAnnualInterestRate() const
{
  return annualInterestRate;
}

int Loan::getNumberOfYears() const
{
  return numberOfYears;
}

double Loan::getLoanAmount() const
{
  return loanAmount;
}

void Loan::setAnnualInterestRate(double newAnnualInterestRate)
{
  annualInterestRate = newAnnualInterestRate;
}

void Loan::setNumberOfYears(int newNumberOfYears)
{
  numberOfYears = newNumberOfYears;
}

void Loan::setLoanAmount(double newLoanAmount)
{
  loanAmount = newLoanAmount;
}

double Loan::getMonthlyPayment() const
{
  double monthlyInterestRate = annualInterestRate / 1200;
    return loanAmount * monthlyInterestRate / (1 - (pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)));
}
double Loan::getTotalPayment() const
{
    double monthlyInterestRate=annualInterestRate/1200;
    return (loanAmount * monthlyInterestRate / (1 - (pow(1 / (1 + monthlyInterestRate), numberOfYears * 12)))*180);
}
int main(){
Loan newLoan;
newLoan.getMonthlyPayment(5.5, 15, 40000);
newLoan.getTotalPayment(5.5, 15, 40000;
cout<<" your monthly payment is "<<newLoan.getMonthlyPayment()<<".";
cout<< "Your total payment is"<< newLoan.getTotalPayment()<<endl;
}


error:/tmp/cc6Gfb1a.o: In function `main':
:(.text.startup+0x41): undefined reference to `Loan::getMonthlyPayment(double, int, double)'
:(.text.startup+0x5e): undefined reference to `Loan::getTotalPayment(double, int, double)'
collect2: error: ld returned 1 exit status
Last edited on
Topic archived. No new replies allowed.