### I need help again...

Well, I did manage to finish the last one, but I need help again! I'm learning how to use functions and there's a bool function that's giving me trouble. The program is separated into 3 different files, so I'll copy and paste all of them.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153`` ``````//Shelby Teele //steele@cnm.edu //STeeleP4 #include #include #include "MortCalc.h" using namespace std; int main() { string answer; Header(); do { double principal = 0; double interest = 0; int years = 0; AskForPrin(); AskInterest(); AskYears(); bool validate = ValidateNums(principal, interest, years); if(validate == true) { Calculation(principal, interest, years); } else { cout << "Sorry, that information is not usable.\n" << "Please enter another.\n"; } cout << "Would you like to calculate another mortgage? y or n\n"; cin>>answer; }while(answer == "y"); cout << "Thank you for using the Mortgage Calculator! Goodbye!\n"; return 0; } //This is the driver. #include #include #include #include #include "MortCalc.h" using namespace std; void Header() { cout << "My name is Shelby Teele.\n" << "Welcome to the C++ Mortgage Calculator!\n" << "This program will help calculate your monthly mortgage\n" <<"payment.\n"; } double AskForPrin() { double principal = 0; cout << "How much are you borrowing?\n" << "Please, no more than \$500,000.\n"; cin>> principal; return principal; } double AskInterest() { double interest =0; cout << "What is your monthly interest rate?\n" <<"Please enter single digits, such as 5,\n" <<"for the interest rate, no more than 10.\n"; cin >> interest;cout <<"%\n"; return interest; } int AskYears() { int years = 0; cout << "How many years would you like to borrow for?\n" << "15, 25, or 30?\n"; cin>> years; return years; } bool ValidateNums(double principal, double interest, int years) { bool validate = true; //This is that tricky bool if(principal <=0 || principal > 500000) { validate= false; } if(interest > 0.00 || interest <= 10.00) { validate = true; } if(years != 15 || years != 25 || years != 30) { validate = false; } return validate; } string Calculation(double principal, double interest, int years) { int q = 12; double i = interest/100.00; double p = principal; int n = years; int x = -(n*q); double y = 1 +(i/q); double z = pow(y, x); double monthlyPayment = (p*i)/q*(1-z); double totalAmountPaid = monthlyPayment * (n*q); double totalInterest = totalAmountPaid - p; stringstream ss; ss <<"Your monthly payment is: \$" < #include #include #include using namespace std; void Header(); double AskForPrin(); double AskInterest(); int AskYears(); bool ValidateNums(double principal, double interest,int years); string Calculation(double principal, double interest, int years); //and this is from my .h file. ``````
 ``12345678910111213141516171819`` ``````bool ValidateNums(double principal, double interest, int years) { bool validate = true; //This is that tricky bool if(principal <=0 || principal > 500000) { validate= false; } if(interest > 0.00 || interest <= 10.00) { validate = true; } if(years != 15 || years != 25 || years != 30) { validate = false; } return validate; } ``````

sorry ,my English is not so good !
Last edited on

sorry, I don't speak Chinese, and Google Translate probably made this not make any sense.
oh，let me try my English ,the Google translation of Chinese is not very accurate. Of course ,my English here is with the help of Google translation.

I think you can not judge three if statements using only one variable validate .Such as ,if the principal does not meet the condition "principal <=0 || principal > 500000" ,the validate is false, but if the interest meets the condition "interest > 0.00 || interest <= 10.00" ,the validate is true , in this case you can not know whether the
"principal <=0 || principal > 500000" is true or not .only three if statements is true ,the function returns true,one of the three is false and then the function returns false, right ?

You know what I mean ?
Two things that I see:

You are returning a value from your functions AskForPrin() AskInterest() & AskYears() but you don't assign the return value to anything. So you pass the initial value (0) into your calculation functions.

`if(years != 15 || years != 25 || years != 30)` Think about this statement. If years==15 it is not equal to 25 or 30.
Right. So, how should I go about assigning them? Also, I have it set up that particular way because the only choices for years are supposed to be 15, 25, and 30. It's not supposed to accept anything else. And orchard, I think I get you, but principal, interest, and years are parts of 3 different functions that do pass through that one validate bool. It doesn't make much sense to me either, but that's how my teacher wanted it and somehow (most likely how norm described it), it messed up. But I really want to find out where I went wrong so I can never do it again.
 ``123456`` `````` //double principal = 0; //double interest = 0; //int years = 0; double principal = AskForPrin(); double interest = AskInterest(); int years = AskYears();``````

Only one of the three conditions has to be true. If years == 15 it is not equal to 25 so the if condition is met and validate will be set to false. You should using the logical and operator (&&) not logical or (||).

 ``123456`` ``````if( (principal > 0 && principal <= 5000) && (interest > 0.00 && interest <= 10.00) && (years == 15 || years == 25 || years == 30) ) validate = true; else validate = false;``````

Is this ok?

I agree with norm b, you have to put the functions return value to variable as norm b said.
you can solve it like this...
 ``12345678910111213141516`` ``````bool ValidateNums(double principal, double interest, int years) { if(principal <=0 || principal > 500000) { return false; } if( !(interest > 0.00 || interest <= 10.00) ) { return false; } if(years != 15 || years != 25 || years != 30) { return false; } return true; }``````
Topic archived. No new replies allowed.