| | |
My "if" is not good
![]() |
•
•
Join Date: Aug 2004
Posts: 140
Reputation:
Solved Threads: 2
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
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
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.
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.
•
•
Join Date: Mar 2004
Posts: 1,620
Reputation:
Solved Threads: 50
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
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
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:
C Syntax (Toggle Plain Text)
#include <iostream.h> #include <stdio.h> int main() { char pass[12]; char ans[4]="neo"; cout<<"Enter password: "; gets (pass); int flag=1, i; for (i=0; i<4; i++) { if (ans[i]!=pass[i]) { flag=0; break; } } if (flag==1) cout<<"ini"; else cout<<"wrong"; return 0; }
![]() |
Similar Threads
- Introducing "Me" (Community Introductions)
- ABIT IC7 G "CMOS Checksum error" and "CPU has been changed or is unworkable" (Windows NT / 2000 / XP)
- google "keyword" question (Search Engine Optimization)
- "Help" will not launch on Mac OS X 10.3.4 (OS X)
- Any body who's "tired" of "trying" programs to remove anything... (Viruses, Spyware and other Nasties)
- System 32 at startup and "dwn"/"down" pop up at startup (Viruses, Spyware and other Nasties)
- Outlook 2003 Inbox "From" Question (Windows NT / 2000 / XP)
Other Threads in the C Forum
- Previous Thread: Tutorial on Hardware in C
- Next Thread: valid or not
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency function givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






