Click here to Skip to main content
11,567,143 members (49,328 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ Homework
Hello Developers. !!
My code isn't working as the way i want, , ,
why the COUT statement is not executed. . .

Any Suggestions. ??

#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 20-Nov-12 2:28am
ctype.h1.8K
Comments
Scenic Design at 20-Nov-12 8:30am
   
i think there should be curly brackets within if . . .
But I want some more explanation . . .
Milind_T at 20-Nov-12 8:40am
   
Yes, you need curly bracket and leave last line out. See below
Richard MacCutchan at 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?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

if (isEmployer == 'E' && hasLocation=='L' && hasSalary > 50000)
             cout << "You Are Allowed To Get The Loan" << endl;
  Permalink  
Comments
Scenic Design at 20-Nov-12 8:34am
   
yes it works but what I'm doing wrong in my code ???
i don't want AND operator too ...
Milind_T at 20-Nov-12 8:36am
   
Check below in my code.
Philippe Mori at 21-Nov-12 23:08pm
   
else only apply to previous if... so if either first 2 conditions are not verified, it will display nothing.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

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
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
  Permalink  
v3
Comments
Scenic Design at 20-Nov-12 8:52am
   
Thanks . . .
Argonia at 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 .
Scenic Design at 20-Nov-12 10:00am
   
Argonia >> Well Explained >> Thanks
Marcus Kramer at 21-Nov-12 10:09am
   
+5. I just fixed the source indentation a bit to make it read easier.
Milind_T at 21-Nov-12 13:18pm
   
Thanks Marcus :-)
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

If you really dislike the && operator try this:

 
	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.
  Permalink  
Comments
Philippe Mori at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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".
  Permalink  
Comments
Marcus Kramer at 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)

  Print Answers RSS


Advertise | Privacy | Mobile
Web04 | 2.8.150624.2 | Last Updated 21 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100