| | |
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: 51
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 arrays binarysearch calculate centimeter char cm convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory dynamic feet fflush file floatingpointvalidation fork forloop frequency getlasterror getlogicaldrivestrin givemetehcodez global graphics gtkgcurlcompiling gtkwinlinux hacking hardware highest homework i/o inches incrementoperators intmain() iso km linked linkedlist linux linuxsegmentationfault list locate logical_drives loopinsideloop. match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pattern pdf performance pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprograming socketprogramming stack standard strchr string suggestions test unix urboc user variable voidmain() whythiscodecausesegmentationfault win32api windows.h






