My "if" is not good

Reply

Join Date: Aug 2004
Posts: 140
Reputation: Mahen is an unknown quantity at this point 
Solved Threads: 2
Mahen Mahen is offline Offline
Junior Poster

My "if" is not good

 
0
  #1
Sep 3rd, 2004
hi everyone, hope u r all feeling fine :mrgreen:

this is my program:

#include <stdio.h>
int main()
{
char pass[12];
printf("Enter Password: ");
gets(pass);
if (pass == "neo")
{
printf ("ini");
}
else
printf("Wrong");
return 0 ;
}


when i enter "neo" as password, it does the "wrong" thing
how can i correct it
Reply With Quote Quick reply to this message  
Join Date: Jul 2004
Posts: 15
Reputation: shalin is an unknown quantity at this point 
Solved Threads: 0
shalin's Avatar
shalin shalin is offline Offline
Newbie Poster

Re: My "if" is not good

 
0
  #2
Sep 3rd, 2004
try this:

if(strcmp(pass,"neo")==0)
printf("ini");
else
printf("Wrong");

strcmp() compares two strings if strings are equal then it returns 0;
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 140
Reputation: Mahen is an unknown quantity at this point 
Solved Threads: 2
Mahen Mahen is offline Offline
Junior Poster

Re: My "if" is not good

 
0
  #3
Sep 3rd, 2004
thanks (they told me to lengthen my message)
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: My "if" is not good

 
0
  #4
Sep 3rd, 2004
Shalin is right, what you were comparing was the ADDRESS of pass with the ADDRESS of the quoted literal "neo". They would never be the same.

If you use std::string instead, it defines an '==' operator that does a strcmp() for you, so you may see something like this in some code:

std::string s;
. . .
if (s == "neo")

and that WORKS because s isn't a char pointer but a class with an '==' operator defined.
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 16
Reputation: shouvik is an unknown quantity at this point 
Solved Threads: 0
shouvik's Avatar
shouvik shouvik is offline Offline
Newbie Poster

Re: My "if" is not good

 
0
  #5
Sep 3rd, 2004
try this............

#include <stdio.h>
#include <iostream.h>
int main()
{
char pass[12];
printf("Enter Password: ");
gets(pass);
if (pass == "neo")
cout<<"ini";
else
cout<<"Wrong";
return 0 ;
}
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 436
Reputation: Chainsaw is an unknown quantity at this point 
Solved Threads: 11
Chainsaw's Avatar
Chainsaw Chainsaw is offline Offline
Unprevaricator

Re: My "if" is not good

 
0
  #6
Sep 3rd, 2004
well, that, too would fail. Shalin's answer would be the simplest.
Reply With Quote Quick reply to this message  
Join Date: Jun 2004
Posts: 609
Reputation: freesoft_2000 is an unknown quantity at this point 
Solved Threads: 7
freesoft_2000 freesoft_2000 is offline Offline
Practically a Master Poster

Re: My "if" is not good

 
0
  #7
Sep 3rd, 2004
Hi everyone,
I rewrote your program a bit and seems to work for me

#include <stdio.h>

void main(void)
{
String pass;
printf("Enter Password: ");
scanf("%s",&pass);
if (pass == "neo")
{
printf ("ini");
}
else
{
printf("Wrong");
}
}

I hope this helps you

Yours Sincerely

Richard West
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 1,620
Reputation: kc0arf is a jewel in the rough kc0arf is a jewel in the rough kc0arf is a jewel in the rough 
Solved Threads: 51
Team Colleague
kc0arf kc0arf is offline Offline
Posting Virtuoso

Re: My "if" is not good

 
0
  #8
Sep 3rd, 2004
Hello,

It is a bad idea to use mathematical equations (==) compare strings! This is why there is a standard library to do it... strcmp is the traditional correct way to do it.

Might as well learn the proper way to do it....or that portable code may bomb out on the next compiler release.

If it is worth doing, do it correctly!


Christian
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 23
Reputation: iamboredguy is an unknown quantity at this point 
Solved Threads: 0
iamboredguy's Avatar
iamboredguy iamboredguy is offline Offline
Newbie Poster

Re: My "if" is not good

 
0
  #9
Sep 4th, 2004
Shalin's method is the ideal one. However, if you don't want to use strcmp() for some reason, there's always a manual, lenghtier way to do it:

  1. #include <iostream.h>
  2. #include <stdio.h>
  3.  
  4. int main()
  5. {
  6. char pass[12];
  7. char ans[4]="neo";
  8. cout<<"Enter password: ";
  9. gets (pass);
  10. int flag=1, i;
  11. for (i=0; i<4; i++)
  12. {
  13. if (ans[i]!=pass[i])
  14. {
  15. flag=0;
  16. break;
  17. }
  18. }
  19. if (flag==1)
  20. cout<<"ini";
  21. else
  22. cout<<"wrong";
  23. return 0;
  24. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 140
Reputation: Mahen is an unknown quantity at this point 
Solved Threads: 2
Mahen Mahen is offline Offline
Junior Poster

Re: My "if" is not good

 
0
  #10
Sep 4th, 2004
thanks to you all, really make me feel happy
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC