943,384 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 3749
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 3rd, 2004
0

My "if" is not good

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster
Mahen is offline Offline
144 posts
since Aug 2004
Sep 3rd, 2004
0

Re: My "if" is not good

try this:

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

strcmp() compares two strings if strings are equal then it returns 0;
Reputation Points: 11
Solved Threads: 0
Newbie Poster
shalin is offline Offline
15 posts
since Jul 2004
Sep 3rd, 2004
0

Re: My "if" is not good

thanks (they told me to lengthen my message)
Reputation Points: 10
Solved Threads: 2
Junior Poster
Mahen is offline Offline
144 posts
since Aug 2004
Sep 3rd, 2004
0

Re: My "if" is not good

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.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Sep 3rd, 2004
0

Re: My "if" is not good

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 ;
}
Reputation Points: 11
Solved Threads: 0
Newbie Poster
shouvik is offline Offline
16 posts
since Aug 2004
Sep 3rd, 2004
0

Re: My "if" is not good

well, that, too would fail. Shalin's answer would be the simplest.
Reputation Points: 36
Solved Threads: 11
Posting Pro in Training
Chainsaw is offline Offline
436 posts
since Jun 2004
Sep 3rd, 2004
0

Re: My "if" is not good

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
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Sep 3rd, 2004
0

Re: My "if" is not good

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
Team Colleague
Reputation Points: 121
Solved Threads: 57
Posting Virtuoso
kc0arf is offline Offline
1,629 posts
since Mar 2004
Sep 4th, 2004
0

Re: My "if" is not good

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. }
Reputation Points: 12
Solved Threads: 0
Newbie Poster
iamboredguy is offline Offline
23 posts
since Aug 2004
Sep 4th, 2004
0

Re: My "if" is not good

thanks to you all, really make me feel happy
Reputation Points: 10
Solved Threads: 2
Junior Poster
Mahen is offline Offline
144 posts
since Aug 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Tutorial on Hardware in C
Next Thread in C Forum Timeline: valid or not





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC