my program display 0 after run after i enter the loan money, rates and year

my program display 0 after run after i enter the loan money, rates and year
thank you for your helps
please point out my errors, i am a beginning C++programer:

#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(double);
double getTotalPayment(double);
};
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;
}
double loan::getMonthlyPayment(double payment){
return payment=((1 +percent)* amount)/years;
}
double loan::getTotalPayment(double totalloan){
return totalloan=payment*years;
}
int main()
{
int years;
double annualInterestRate;
double loanAmount;
double payment;
double totalloan;
cout<<" Enter the loan Amount";
cin>>loanAmount;
cout<<" Enter the annuak interest rate of the loan:";
cin>>annualInterestRate;
cout<<" Enter the number of years for the loan";
cin>>years;
cout<<" your monthly payment is "<<payment<<".";
cout<< "Your total payment is"<< totalloan<<endl;
}
Last edited on
Please, use the code tags. See http://www.cplusplus.com/articles/jEywvCM9/

What do you do after reading the three values, but before showing the two values?
my program display 0 after run after i enter the loan money, rates and year
thank you for your helps
please point out my errors, i am a beginning C++programer:

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
#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(double);
double getTotalPayment(double);
};
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;
}
double loan::getMonthlyPayment(double payment){
return payment=((1 +percent)* amount)/years;
}
double loan::getTotalPayment(double totalloan){
return totalloan=payment*years;
}
int main()
{
int years;
double annualInterestRate;
double loanAmount;
double payment;
double totalloan;
cout<<" Enter the loan Amount";
cin>>loanAmount;
cout<<" Enter the annuak interest rate of the loan:";
cin>>annualInterestRate;
cout<<" Enter the number of years for the loan";
cin>>years;
cout<<" your monthly payment is "<<payment<<".";
cout<< "Your total payment is"<< totalloan<<endl;
}
closed account (1vD3vCM9)
For some reason my respond didn't register.

You need to read about classes again.
You need to make an object of the class, like:
 
loan myLoan;


And then to access the myLoan's variables, you need to do like so:

 
std::cin >> myLoan.payment;

Although that will be a problem, as your variables are private. Make them public or make public functions to set them.
i come out with new things can you help me to check thak 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
#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;
}



error: In member function 'double loan::getloanAmount(double)':
44:11: error: a function-definition is not allowed here before '{' token
51:1: error: expected '}' at end of input
closed account (1vD3vCM9)
Just read the error mate!
You forgot the } at the end of the function before main.
Indentation of code often helps to see details.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int loan::getNumberOfYears(int years){
  return years;
}

double loan::getloanAmount(double money){
  return money;
  int main(){ // error: attempt to define int main(){/*code*/} inside loan::getloanAmount()
    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;
  }
// error: the closing brace of "getloanAmount()" is missing 

Quite many editors do have an indentation command. If they have, it should do systematic indentation.
Hello, I add the } back but there is another error uppear in the program, how to i fix it ??
error: In function 'int main()':
46:5: error: expected unqualified-id before '.' token
47:1: error: 'L' was not declared in this scope
Thank you very much for helping me!!

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;
}
loan.L;
Would you declare an integer variable like this?
int.L;
loan.L;

You can't have a dot between a type and a var name. Should be loan L;
BTW. L is not a good var name. Better use sth. like MyLoan

my program run but the output doesnt seems right :
the output: your monthly payment is -nan.Your total payment is0
I want the output to be monthly loan and total loan ( 40000loan,5.5rates,15yrs)
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;
}
What does (for example) your function double load::setloanAmount(double amount) actually do?

You do call it with value 40000.
What should logically happen?
What does actually happen, exactly?
It let the user cin the value and run the program
and the getloanAmount will display the result, but which part do my program go wrong.
You do say that this code:
1
2
3
4
double loan::setloanAmount( double amount )
{
  return amount;
}

lets the user cin the value and run the program


Are you absolutely sure?
Topic archived. No new replies allowed.