Why I can't get the value? Please help!

Hi, I have been trying to calculate the bond price given a certain YTM using class method. Since the YTM and bond price are linked with each other(there is another question asking us to calculate the YTM with a given price),we put price and YTM in "private" and others variables in"public". Actually, this is the format given in my assignment.
I used "getvalue" to get the value of YTM, and used YTM in the formula of bond price calclation. However, the output of the price is infinite, while the output of price is right if I used a specific value of YTM(such as 0.05) in the formula. It means that I didn't successfully get right value of YTM. Can anyone tell me what's wrong with my program? Thanks a lot!!



#include <iostream>
#include <cmath>
using namespace std;

class bond{
public:
bond(){};
~bond(){};
bond(double T0, double C0, double P0, double t0);
void setprice(double Price0);
double getprice();
void setYTM(double YTM0);
double getYTM();
double T,t,C,P;

private:
double Price;
double YTM;
};



bond::bond(double T0, double C0, double P0, double t0)
{
T = T0, C = C0, P = P0, t = t0;
};

void bond::setprice(double Price0){
Price= Price0;
};

void bond::setYTM(double YTM0){
YTM = YTM0;
};

double bond::getprice(){
double P_for_coupon = 0;
for (int i = 1; i <=T; i++){
P_for_coupon = P_for_coupon + C*exp(-(i - t)* YTM);
};
Price = P_for_coupon + P*exp(-(T - t)*YTM);
return Price;
};



int main(){
bond data(4, 5, 100, 0.1);
bond project;
project.setYTM(0.05)
cout << "The Price of the bond is" << data.getprice()<<endl;
return 0;
}
You are missing a semicolon on line 50

project.setYTM(0.05);

Also, you never initialize YTM for data. You do so for project, but you end up asking the program for data's price which requires YTM.

1
2
3
4
5
6
7
8
double bond::getprice(){
double P_for_coupon = 0;
for (int i = 1; i <=T; i++){
P_for_coupon = P_for_coupon + C*exp(-(i - t)* YTM);
};
Price = P_for_coupon + P*exp(-(T - t)*YTM);
return Price;
};


Check the code below, this should work. Set YTM to whatever you need. Or set a default value in the bond class.


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
#include <iostream>
#include <cmath>
using namespace std;

class bond{
public:
	bond(){};
	~bond(){};
	bond(double T0, double C0, double P0, double t0);
	void setprice(double Price0);
	double getprice();
	void setYTM(double YTM0);
	double getYTM();
	double T,t,C,P;

private:
	double Price;
	double YTM;
};



bond::bond(double T0, double C0, double P0, double t0)
{
	T = T0, C = C0, P = P0, t = t0;
};

void bond::setprice(double Price0){
	Price= Price0;
};

void bond::setYTM(double YTM0){
	YTM = YTM0;
};

double bond::getprice(){
	double P_for_coupon = 0;
	for (int i = 1; i <=T; i++){
		P_for_coupon = P_for_coupon + C*exp(-(i - t)* YTM);
	};
	Price = P_for_coupon + P*exp(-(T - t)*YTM);
	return Price;
};



int main(){
	bond data(4, 5, 100, 0.1);	
	bond project;
	project.setYTM(0.05);
	data.setYTM(0.05);
		cout << "The Price of the bond is: " << data.getprice()<<endl;
	return 0;
}
Topic archived. No new replies allowed.