Question about if/else statements

How do I make my second block execute under the first block?

First Block

1
2
3
4
5
6
7
8
9
10
11
12
13
if ((age >= 18 && creditScore >= 650 && income >= 20000) || (age >= 18 && creditScore >= 650 && assets >= 20000))
{
	if (creditScore >= 650 && creditScore <= 699)
		loan = 10000;
	else if (creditScore >= 700 && creditScore <= 749)
		loan = 20000;
	else if (creditScore >= 650 && creditScore <= 699)
		loan = 30000;
	else if (creditScore >= 650)
		loan = 40000;
}
else 
		cout << "Disqualified for loan";


Second Block

1
2
3
4
5
6
7
8
9
10
	if ((loan >= 10000 && homeowner == 'y') || (loan >= 10000 && assets >= 50000))
	{
		loan += 10000;
		cout << "Qualified for $" << loan;
	}
	else if (loan >= 10000 && homeowner == 'y' && assets >= 50000 && age >= 40)
	{
		loan += 15000;
		cout << "Qualified for $" << loan;
	}
Last edited on
Did you mean you wanted to put the entire Second Block inside the First Block?
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if ((age >= 18 && creditScore >= 650 && income >= 20000) || (age >= 18 && creditScore >= 650 && assets >= 20000))
{
	if (creditScore >= 650 && creditScore <= 699)
		loan = 10000;
	else if (creditScore >= 700 && creditScore <= 749)
		loan = 20000;
	else if (creditScore >= 650 && creditScore <= 699)
		loan = 30000;
	else if (creditScore >= 650)
		loan = 40000;
}
else 
{
          cout << "Disqualified for loan";
          return;
}


Also from your previous post I know that homeowner is already boolean so you shouldn't compare it to a char.
Last edited on
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
	if ((age >= 18 && creditScore >= 650 && income >= 20000) || (age >= 18 && creditScore >= 650 && assets >= 20000))
{
	if (creditScore >= 650 && creditScore <= 699)
		loan = 10000;
	else if (creditScore >= 700 && creditScore <= 749)
		loan = 20000;
	else if (creditScore >= 650 && creditScore <= 699)
		loan = 30000;
	else if (creditScore >= 650)
		loan = 40000;
	if ((loan >= 10000 && homeowner == 'y') || (loan >= 10000 && assets >= 50000))
	{
		loan += 10000;
		cout << "Qualified for $" << loan;
	}
	else if (loan >= 10000 && homeowner == 'y' && assets >= 50000 && age >= 40)
	{
		loan += 15000;
		cout << "Qualified for $" << loan;
	}
}
else 
		cout << "Disqualified for loan";
	return 0;
}

Is this what you mean?
Yeah, thanks guys. @vin, what do you mean I shouldn't compare homeowner to a char? How else would I do it instead of homeowner == 'y'

Also, another issue with my code.
When...
Age = 100
Income = 100000
Assets = 100000
creditScore = 900
homeowner = y
employed = y
My output is loan + 10000 (50000) when it should be loan + 15000 (55000)

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

#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 >= 20000)) // 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 ((loan >= 10000 && homeowner == 'y') || (loan >= 10000 && homeowner == 'Y') || (loan >= 10000 && 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 code is wrong in several places.
+ Line 37
+ Line 49
+ Line 51 : Comparing homeowner to a char
+ Line 56 : Comparing homeowner to a char, but there are more
+ Line 54, 59, 62 : You are printing the same message three times.
cout << "Qualified for $" << loan;

Take your time to fix your code. However, even if the program output is correct, that does not necessarily mean your program is correct.
Last edited on
closed account (48T7M4Gy)
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.
Yeah, thanks guys. @vin, what do you mean I shouldn't compare homeowner to a char? How else would I do it instead of homeowner == 'y'


1
2
if (homeowner  && assets >= 50000 && age >= 40 || homeowner && assets >= 50000 && age >= 40 )
...
closed account (48T7M4Gy)
if (homeowner == true)
Topic archived. No new replies allowed.