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.

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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
//Shelby Teele
//steele@cnm.edu
//STeeleP4

#include <iostream>
#include <string>

#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 <iostream>
#include <string>
#include <cmath>
#include <sstream>

#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: $" <<monthlyPayment<<".\n"
			<<"The total amount you have payed over the life\n"
			<<"of the loan is: $"<<totalAmountPaid<<".\n"
			<<"The total amount of the interest paid is: $\n"
			<<totalInterest<<".\n";
		ss.setf(ios::fixed);
			ss.precision(2);
		string MortCalc = ss.str();
		return MortCalc;
	}
//This is all of the functions and calculations.
#include <iostream>
#include <string>
#include <sstream>
#include <cmath>
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. 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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;
	  
	}
	

我觉得你不能在判断三个条件的时候都用 validate一个变量来标示,比如说Principal变量不满足条件,那么validate是false ,但interest满足的了条件 ,validate 又被设置为true了,那么此函数最后返回的validate到底是true 还是 false? 恐怕是只有三种条件都满足的时候才能返回true,如果有一个不满足都是false。

sorry ,my English is not so good !
Last edited on
我使用谷歌翻译来回答,我不会讲中文。一些验证,我想是假的,因为如果他们是假的,那么布尔是假的,它会返回错误信息,我做了。不过,我想真实的,当我运行调试时,由于某种原因,我的CINS不正常的价值观保持为零不管我进入。这是非常令人沮丧的。

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.
1
2
3
4
5
6
    //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 (||).

1
2
3
4
5
6
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...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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.