Issue with if/else statement

Sooo, whenever my input hits the first and second if statement, my outcome is loan += 10k when it should be 15k.

I was told that "The 10K condition is weaker than the 15K condition, i.e. if the 15K condition is true then so is the 10K condition. So you want to test for the 15K condition first. Otherwise you never reach the 15K condition."

I'm not quite sure what this means, could someone give me a step in the correct direction?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
	if (loan >= 10000) // If loan is greater than or equal to 10000... (Reqs. for bonus)
	{
		if (homeowner == 'y' && assets >= 50000 && age >= 40 || homeowner == 'Y' && assets >= 50000 && age >= 40 ) // Reqs. for 15000 bonus
		{
			loan += 15000;
			cout << "Qualified for $" << loan;
		}
		else if ((homeowner == 'y') || (homeowner == 'Y') || (assets >= 50000)) // Reqs. for 10000 bonus
		{
		loan += 10000;
		cout << "Qualified for $" << loan;
		}
		else 
		cout << "Qualified for $" << loan;
	}


WHOLE PROGRAM

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
#include <iostream>
#include <string>
using namespace std; 

 
bool promptYN(string  msg)
{    
   char entered; 
   cout << msg << endl; // in front
   cin >> entered; 
   return entered == 'y' || entered == 'Y';
} 
 
int promptInt(string  msg) 
{ 
   int number; 
   cout  << msg << endl; // in front
   cin >> number; 
   return number; 
} 
 
int  main() 
{ 
   bool homeowner, employed; 
   int  age, income, assets, creditScore, loan;
   loan = 0; 
   // Do not rearrange this prompt order. 
   age = promptInt("Age: "); 
   income = promptInt("Income: "); 
   assets = promptInt("Assets: "); 
   creditScore = promptInt("Credit Score: "); 
   homeowner = promptYN("Home Owner [yn]: "); 
   employed = promptYN("Employed [yn]: ");  

   // BEGINNING OF YOUR CODE 
if ((age >= 18 && creditScore >= 650 && income >= 20000) || (age >= 18 && creditScore >= 650 && assets >= 30000)) // Reqs. for loan
{
	if (creditScore >= 650 && creditScore <= 699) // If credit score is between those numbers, 10000
		loan = 10000;
	else if (creditScore >= 700 && creditScore <= 749) // If credit score is between those numbers, 20000
		loan = 20000;
	else if (creditScore >= 750 && creditScore <= 799) // If credit score is between those numbers, 30000
		loan = 30000;
	else if (creditScore >= 800) // If credit score is between those numbers, 40000
		loan = 40000;


	if (loan >= 10000) // If loan is greater than or equal to 10000... (Reqs. for bonus)
	{
		if (homeowner == 'y' && assets >= 50000 && age >= 40 || homeowner == 'Y' && assets >= 50000 && age >= 40 ) // Reqs. for 15000 bonus
		{
			loan += 15000;
			cout << "Qualified for $" << loan;
		}
		else if ((homeowner == 'y') || (homeowner == 'Y') || (assets >= 50000)) // Reqs. for 10000 bonus
		{
		loan += 10000;
		cout << "Qualified for $" << loan;
		}
		else 
		cout << "Qualified for $" << loan;
	}
}
else 
		cout << "Disqualified for loan";

   // END OF YOUR CODE
   return 0; 
}

I just see that you have not fixed any of the problems we pointed out.
kemort wrote:
If homeowner is boolean then it can only take values of true (1) or false (0). A char value for homeowner would not be appropriate. Comparing homeowner to a char probably always returns false, or even worse, undefined behaviour.

ASCII code for y is 121. Non-zero numbers are true. I don't think that's the problem bc it still adds up. If that's not the case, I'm not sure how it would determine whether y or n is t or f.
ASCII code for y is 121. Non-zero numbers are true. I don't think that's the problem bc it still adds up. If that's not the case, I'm not sure how it would determine whether y or n is t or f.


No, a boolean cannot store a value more than 1.
Forget the value 121.

Thanks, Sakura and kemort. You guys were right. I managed to fix it and I'm getting the output I want. Now, I have to test more cases!
Show us your new code so that we can confirm you have made progress.
I changed homeowner == 1.

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

#include <iostream>
#include <string>
using namespace std; 

 
bool promptYN(string  msg)
{    
   char entered; 
   cout << msg << endl; // in front
   cin >> entered; 
   return entered == 'y' || entered == 'Y';
} 
 
int promptInt(string  msg) 
{ 
   int number; 
   cout  << msg << endl; // in front
   cin >> number; 
   return number; 
} 
 
int  main() 
{ 
   bool homeowner, employed; 
   int  age, income, assets, creditScore, loan;
   loan = 0; 
   // Do not rearrange this prompt order. 
   age = promptInt("Age: ");
   income = promptInt("Income: "); 
   assets = promptInt("Assets: "); 
   creditScore = promptInt("Credit Score: "); 
   homeowner = promptYN("Home Owner [yn]: "); 
   employed = promptYN("Employed [yn]: ");  

   // BEGINNING OF YOUR CODE 
if ((age >= 18 && creditScore >= 650 && income >= 20000) || (age >= 18 && creditScore >= 650 && assets >= 30000)) // Reqs. for loan
{
        if (creditScore >= 650 && creditScore <= 699) // If credit score is between those numbers, 10000
                loan = 10000;
        else if (creditScore >= 700 && creditScore <= 749) // If credit score is between those numbers, 20000
                loan = 20000;
        else if (creditScore >= 750 && creditScore <= 799) // If credit score is between those numbers, 30000
                loan = 30000;
        else if (creditScore >= 800) // If credit score is between those numbers, 40000
                loan = 40000;

        if (loan >= 10000) // If loan is greater than or equal to 10000... (Reqs. for bonus)
        {
                if (homeowner == 1 && assets >= 50000 && age >= 40) // Reqs. for 15000 bonus
                {
                        loan += 15000;
                        cout << "Qualified for $" << loan;
                }
                else if ((homeowner == 1) || (assets >= 50000)) // Reqs. for 10000 bonus
                {
                loan += 10000;
                cout << "Qualified for $" << loan;
                }
                else 
                cout << "Qualified for $" << loan;
        }
}
else 
                cout << "Disqualified for loan";

   // END OF YOUR CODE
   return 0; 
}
Your program is now correct. However, some of your code is not very efficient.
You need to fix a couple of things (in other words, tweaks or something like that).
Last edited on
I'm happy with the state it is in now. This took a long time for me to solve, but it was a good learning experience. Thanks, Sakura!
Yes, I'm happy you are able to solve the assignment.
A couple of things :
- homeowner == 1 (To be more precise, it should be homeowner == true)
- if (loan >= 10000) is redundant.
- You only need to output cout << "Qualified for $" << loan; once. Since the person is already qualified for loan.

Hope this helps.

Topic archived. No new replies allowed.