c++ mortgage with inheritance

trying to write a simple program that will figure a mort payment but this program is not compiling ... I use Codeblockers and it shows me where the errors are but not sure how to fix this to run ...it shows I have 6 errors .... the first in line 75 and the void main statement ...... I am open to suggestions, if anyone has any..
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
//name class type

class Payment
{
private:
float loan;
float rate;
float years;
float payment;
float term;
float pay;
float tm;

public:
float getLoan(void);
float getRate(void);
float getYears(void);
void setData(float, float, float);
float calcTerm(void);
float calcPayment();

};

// SetData copies the argument to private members
// loan, rate, years

void Payment::setData(float l, float r, float y)
{
loan = l;
rate = r;
years = y;
}

// getLoan returns the value in the private member loan amt.

float Payment::getLoan(void)
{
return loan;
}

// getRate returns the value in the private member rate amt.

float Payment::getRate(void)
{
return rate;
}

// getYears returns the value in the private member years.

float Payment::getYears(void)
{
return years;
}


float Payment::calcPayment()
{
payment = (loan * rate / 12.0 * term) / (term - 1.0);
return pay;

}

float Payment::calcTerm()
{

term = pow((1 + rate / 12.0), 12.0 * years);
return tm;
}


void main (void)
{

Payment mortgage;
float mtg_loan;
float mtg_rate;
float mtg_years;


cout << "Loan amount: $";
cin >> mtg_loan;
cout << "Annual Interest Rate (Enter .12 for 12%): ";
cin >> mtg_rate;
cout << "Years of loan: ";
cin >> mtg_years;
mortgage.setData(mtg_loan, mtg_rate, mtg_years);
//mortgage.setDataoth(mtg_payment);

//cout <<mortgage.getLoan() <<endl;
cout << "Here is your data:\n" << endl << endl;
cout <<"Loan amount: $" <<mortgage.getLoan() <<endl;
cout <<"Annual Interest Rate: " << mortgage.getRate() <<endl;
cout <<"Years of loan: " <<mortgage.getYears() << endl;
//cin >> mortgage.getYears() <<endl;
//what I want to do
//cout << "Monthly payment: $" << payment << endl;
cout << "Monthly payment: $" << mortgage.pay << endl;
//what I want to do
////cout << "Total Pay Back: $" << (payment*years*12) << endl;
cout <<"Total Pay Back: $" << (mortgage.pay*mortgage.getYears*12) <<endl;



}
Last edited on
Please post the errors exactly as they appear in your development environment.

Also the problem on line 75 is that in a C++ program main() must be defined to return an int, not void, and you should return an int from this function.

You should also find and consistently use an indentation style you like. This will make your program much easier to read. http://en.wikipedia.org/wiki/Indent_style

I removed the main void line and put in int main() on line 107

now I get this only for errors in the program

U:\calcMortgage.cpp|77|error: expected unqualified-id before '{' token

||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s))===|

i'm on someone else's machine but i see a few things wrong with this with only a quick glance. for example:
-
1
2
3
4
5
6
7
8
9
10
11
12
13
float Payment::calcPayment()
{
payment = (loan * rate / 12.0 * term) / (term - 1.0);
return pay;

}

float Payment::calcTerm()
{

term = pow((1 + rate / 12.0), 12.0 * years);
return tm;
}

In both of these methods you are NOT returning the values you have calculated.

- cout <<"Total Pay Back: $" << (mortgage.pay*mortgage.getYears*12) <<endl;
You need to call functions by ending the call in brackets, which you are not doing in some of your calls, e.g. getYears().


EDIT: I don't see any class inheritance, so i'm not sure why it's in your thread title.
Last edited on
mutexe

sorry-let me elaborate - 1st programming class with no programming experience here so I am very unclear what you are referring to ..please correct me if I am wrong in my thoughts here ....
so it should be...." return payment " instead of "return pay" ? ( and return term instead of return tm)

also as far as your brackets to end the get Years... are you saying this should read like this?

cout<<"Total Pay Back:$"<<(mortgage.pay*getYears()*12<<endl;


either way I still get the error of the unqualified -id for line 76 now (since I moved everything) and the class listed is payments with what type of payment listed as mortgage payment for the inherit question. Again please correct me if I mis understood or I am interpreting this incorrectly
Last edited on
no need to say sorry.
Let's ignore my comments for the moment.

With your amendments can you post and updated copy of your code please? i.e. the code you have now that you have replaced void main with int main.

ta.
Thank you. Sure: here is the current text..added the changes I "thought" you were suggesting and the brackets too.......feeling at this point I have the program I just need to figure out how to fix it so I have the output that I am looking for once a user inputs the requested info.

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
//name class type



class Payment
{
private:
float loan;
float rate;
float years;
float payment;
float term;
float pay;
float tm;

public:
float getLoan(void);
float getRate(void);
float getYears(void);
void setData(float, float, float);
float calcTerm(void);
float calcPayment();

};

// SetData copies the argument to private members
// loan, rate, years

void Payment::setData(float l, float r, float y)
{
loan = l;
rate = r;
years = y;
}

// getLoan returns the value in the private member loan amt.

float Payment::getLoan(void)
{
return loan;
}

// getRate returns the value in the private member rate amt.

float Payment::getRate(void)
{
return rate;
}

// getYears returns the value in the private member years.

float Payment::getYears(void)
{
return years;
}


float Payment::calcPayment()
{
payment = (loan * rate / 12.0 * term) / (term - 1.0);
return payment;

}

float Payment::calcTerm()
{

term = pow((1 + rate / 12.0), 12.0 * years);
return term;
}

{

Payment mortgage;
float mtg_loan;
float mtg_rate;
float mtg_years;


cout << "Loan amount: $";
cin >> mtg_loan;
cout << "Annual Interest Rate (Enter .12 for 12%): ";
cin >> mtg_rate;
cout << "Years of loan: ";
cin >> mtg_years;
mortgage.setData(mtg_loan, mtg_rate, mtg_years);
//mortgage.setDataoth(mtg_payment);

//cout <<mortgage.getLoan() <<endl;
cout << "Here is your data:\n" << endl << endl;
cout <<"Loan amount: $" <<mortgage.getLoan() <<endl;
cout <<"Annual Interest Rate: " << mortgage.getRate() <<endl;
cout <<"Years of loan: " <<mortgage.getYears() << endl;
//cin >> mortgage.getYears() <<endl;
//what I want to do
//cout << "Monthly payment: $" << payment << endl;
cout << "Monthly payment: $" << mortgage.pay << endl;
//what I want to do
////cout << "Total Pay Back: $" << (payment*years*12) << endl;
cout <<"Total Pay Back: $" << (mortgage.pay*mortgage.getYears()*12) <<endl;

int main();

}
I removed the main void line and put in int main() on line 107

Why did you remove the void main() line and then move int main() to line 107 instead of just replacing void with int in the first place?

All C++ programs must have a main() function that looks like:
1
2
3
4
5
6
int main()
{
   // The code that belongs in main()!

   return 0;
}


There are other forms of main() that allow parameters but all forms of main() must return an int.
When I do what you are suggesting jlb, just switch int main () for the void main (void) line- I still get the same error of..

U:\calcMortgage.cpp|76|error: expected unqualified-id before '{' token

||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s))===

"
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
//name class type



class Payment
{
private:
float loan;
float rate;
float years;
float payment;
float term;
float pay;
float tm;

public:
float getLoan(void);
float getRate(void);
float getYears(void);
void setData(float, float, float);
float calcTerm(void);
float calcPayment();

};

// SetData copies the argument to private members
// loan, rate, years

void Payment::setData(float l, float r, float y)
{
loan = l;
rate = r;
years = y;
}

// getLoan returns the value in the private member loan amt.

float Payment::getLoan(void)
{
return loan;
}

// getRate returns the value in the private member rate amt.

float Payment::getRate(void)
{
return rate;
}

// getYears returns the value in the private member years.

float Payment::getYears(void)
{
return years;
}


float Payment::calcPayment()
{
payment = (loan * rate / 12.0 * term) / (term - 1.0);
return payment;

}

float Payment::calcTerm()
{

term = pow((1 + rate / 12.0), 12.0 * years);
return term;
}
int main()
{

Payment mortgage;
float mtg_loan;
float mtg_rate;
float mtg_years;


cout << "Loan amount: $";
cin >> mtg_loan;
cout << "Annual Interest Rate (Enter .12 for 12%): ";
cin >> mtg_rate;
cout << "Years of loan: ";
cin >> mtg_years;
mortgage.setData(mtg_loan, mtg_rate, mtg_years);
//mortgage.setDataoth(mtg_payment);

//cout <<mortgage.getLoan() <<endl;
cout << "Here is your data:\n" << endl << endl;
cout <<"Loan amount: $" <<mortgage.getLoan() <<endl;
cout <<"Annual Interest Rate: " << mortgage.getRate() <<endl;
cout <<"Years of loan: " <<mortgage.getYears() << endl;
//cin >> mortgage.getYears() <<endl;
//what I want to do
//cout << "Monthly payment: $" << payment << endl;
cout << "Monthly payment: $" << mortgage.pay << endl;
//what I want to do
////cout << "Total Pay Back: $" << (payment*years*12) << endl;
cout <<"Total Pay Back: $" << (mortgage.pay*mortgage.getYears()*12) <<endl;



}

Last edited on
I don't see that error when I compile your code. The only error message I receive are:
main.cpp||In function ‘int main()’:|
main.cpp|17|error: ‘float Payment::pay’ is private|
main.cpp|101|error: within this context|
main.cpp|17|error: ‘float Payment::pay’ is private|
main.cpp|104|error: within this context|


Also if you used some kind of indentation you might be able to read your program easier:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <iostream>
#include <iomanip>
#include <math.h>  // This should be <cmath>

using namespace std;

//name class type
class Payment
{
   private:
      float loan;
      float rate;
      float years;
      float payment;
      float term;
      float pay;
      float tm;

   public:
      float getLoan(void);
      float getRate(void);
      float getYears(void);
      void setData(float, float, float); 
      float calcTerm(void);
      float calcPayment();

};

// SetData copies the argument to private members
// loan, rate, years

void Payment::setData(float l, float r, float y)
{
   loan = l;
   rate = r;
   years = y;
}

// getLoan returns the value in the private member loan amt.

float Payment::getLoan(void)
{
   return loan;
}

// getRate returns the value in the private member rate amt.

float Payment::getRate(void)
{
   return rate;
}

// getYears returns the value in the private member years.

float Payment::getYears(void)
{
   return years;
}


float Payment::calcPayment()
{
   payment = (loan * rate / 12.0 * term) / (term - 1.0);
   return payment;

}

float Payment::calcTerm()
{

   term = pow((1 + rate / 12.0), 12.0 * years);
   return term;
}

int main()
{
   Payment mortgage;
   float mtg_loan;
   float mtg_rate;
   float mtg_years;


   cout << "Loan amount: $";
   cin >> mtg_loan;
   cout << "Annual Interest Rate (Enter .12 for 12%): ";
   cin >> mtg_rate;
   cout << "Years of loan: ";
   cin >> mtg_years;
   mortgage.setData(mtg_loan, mtg_rate, mtg_years);
   //mortgage.setDataoth(mtg_payment);

   //cout <<mortgage.getLoan() <<endl;
   cout << "Here is your data:\n" << endl << endl;
   cout <<"Loan amount: $" <<mortgage.getLoan() <<endl;
   cout <<"Annual Interest Rate: " << mortgage.getRate() <<endl;
   cout <<"Years of loan: " <<mortgage.getYears() << endl;
   //cin >> mortgage.getYears() <<endl;
   //what I want to do
   //cout << "Monthly payment: $" << payment << endl;
   cout << "Monthly payment: $" << mortgage.pay << endl;
   //what I want to do
   ////cout << "Total Pay Back: $" << (payment*years*12) << endl;
   cout <<"Total Pay Back: $" << (mortgage.pay*mortgage.getYears()*12) <<endl;

   // You really should also return an int from this function.
   return 0;
}


Thank you for the suggestion on the indentation.

I did change the math.h to cmath in line 3
What are you suggesting at line 105 " return int; " instead of the return 0 ?
I meant return an int, like 0, or maybe some other value.
I think now you're in a good position to address my comments from the other day.

For example:
what are you trying to acheive on this line:

cout << "Monthly payment: $" << mortgage.pay << endl;

You cannot access your private pay member variable like this. You should write an accessor (or 'getter') method and declare it in your public section with your other methods:

1
2
3
4
float Payment::getPay() 
{
    return pay;
}


Then your line of code becomes this:
[code]cout << "Monthly payment: $" << mortgage.getPay() << endl;[/code]
Last edited on
Topic archived. No new replies allowed.