Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Developers. !!
My code isn't working as the way i want, , ,
why the COUT statement is not executed. . .

Any Suggestions. ??

C++
#include <iostream>

using namespace std;


int main ()
{
	char isEmployer = 'E';

	char hasLocation = 'L';

	int hasAge = 0;

	int hasSalary = 0;

	cout << "enter details to get loan" << endl;

	cout << "Employer?" << endl;

	cin >> isEmployer;

	cout << "Location?" << endl;

	cin >> hasLocation;

	cout << "Salary" << endl;

	cin >> hasSalary;

	if (isEmployer == 'E')

		if (hasLocation=='L')

			if(hasSalary > 50000)

            cout << "You Are Allowed To Get The Loan" << endl;

		else
			cout << "Not Allowed To Get The Loan" << endl;

		return 0;
}
Posted
Comments
Usman Hunjra 20-Nov-12 8:30am    
i think there should be curly brackets within if . . .
But I want some more explanation . . .
MT_ 20-Nov-12 8:40am    
Yes, you need curly bracket and leave last line out. See below
Richard MacCutchan 20-Nov-12 8:38am    
Works fine for me when I put the correct values in. What are you typing in answer to the questions?

C++
if (isEmployer == 'E' && hasLocation=='L' && hasSalary > 50000)
             cout << "You Are Allowed To Get The Loan" << endl;
 
Share this answer
 
Comments
Usman Hunjra 20-Nov-12 8:34am    
yes it works but what I'm doing wrong in my code ???
i don't want AND operator too ...
MT_ 20-Nov-12 8:36am    
Check below in my code.
Philippe Mori 21-Nov-12 23:08pm    
else only apply to previous if... so if either first 2 conditions are not verified, it will display nothing.
I am not in C++, but the logic is coded wrongly.

Here if isEmployer is E then only it will check hasLocation, Then if HasLocation is L then only it will check HasSalary. Now if HasSalary is > 50000 you will get first string and else it will return second string. But for any thing other then E or L in first two condition it will not go in the third if only
It should have been

C++
if (isEmployer == 'E')
{
   if (hasLocation=='L')
      if(hasSalary > 50000)
         cout << "You Are Allowed To Get The Loan" << endl;
 }
else
   cout << "Not Allowed To Get The Loan" << endl;


Or You can put logical AND
C++
if (((isEmployer == 'E') && (hasLocation=='L') && (hasSalary > 50000))
  cout << "You Are Allowed To Get The Loan" << endl;
else
  cout << "Not Allowed To Get The Loan" << endl;


PS: I am not sure about symbol for logical AND condition in C++

Thanks
Milind
 
Share this answer
 
v3
Comments
Usman Hunjra 20-Nov-12 8:52am    
Thanks . . .
Argonia 20-Nov-12 8:57am    
What about if the isEmployer is 'E' but the hasLocation is not a 'L'? then the cout wont be executed in the first code.
If you use a if-else statement and with no brackets the else statement is for the last added if .
Usman Hunjra 20-Nov-12 10:00am    
Argonia >> Well Explained >> Thanks
fjdiewornncalwe 21-Nov-12 10:09am    
+5. I just fixed the source indentation a bit to make it read easier.
MT_ 21-Nov-12 13:18pm    
Thanks Marcus :-)
If you really dislike the && operator try this:

C++
bool bOK = false;

if (isEmployer == 'E')
    if (hasLocation == 'L')
        if(hasSalary > 50000)
            bOK = true;

if (bOK) cout << "OK..." << endl;
else cout << "Bad..." << endl;


I hope this helps.
 
Share this answer
 
Comments
Philippe Mori 21-Nov-12 23:11pm    
In a simple case like this one, I would prefer to uses &&. But in real life code you might have a lot of code in each block and it might make sense to uses a flag to know what to do later.
In the code it is taken Salary=0 but the condition checked is Salary>5000.So control is going to the statement
cout<<"Not Allowed To Get The Loan".
 
Share this answer
 
Comments
fjdiewornncalwe 21-Nov-12 10:11am    
My 1. You've missed the "cin >> hasSalary;" statement, so your answer in this case is wrong. Sorry.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900