Why cant I use the variable "i" from my constructor in other member functions? C++ -


i'm working on creating bank program programming methodology and, reason, can't seem use value assign variable i in constructor in of other member functions. instance, if user input 6 , i calculated 0.005 constructor, value (0.005) not passed makepayment function. instead, i reset zero.

can shed light on this? code below.

  loan_data::loan_data(double p, double n, double i) {     cout << "enter loan amount: $";     cin >> p;     cout << "enter loan length: ";     cin >> n;     cout << "enter credit score: ";     cin >> i;          = / 100;     = / 12;     n = n * 12;     bal = p;         = (p * ((i * pow(1 + i, n)) / (pow(1 + i, n) - 1)));          cout << "a is: " << << endl;         cout << "bal is: " << bal << endl;         cout << "i is: " << << endl; } void loan_data::makepayment(double pay) {         cout << "i is: " << << endl;         cout << "bal is: " << bal << endl;     cout << "enter payment amount: $";     cin >> pay;          cout << "bal is: " << bal << endl;      bal = ((i + 1) * bal) - pay;         cout << "i is: " << << endl;         cout << "bal is: " << bal << endl;         cout << "pay is: " << pay << endl;         cout << "a is: " << << endl;         = pay;         cout << "a is: " << << endl; } 

i = / 100; setting parameter i, not member variable i (assuming such variable exists). parameter hiding member.

you can fix either using this->i (i.e. this->i = / 100), or changing parameter name doesn't collide member variable's name.

but perhaps should rid of parameter, given don't use (cin >> i; nukes whatever value had, making totally pointless pass in first place).


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -