| | |
My "if" is not good
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
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 |
#include adobe ansi api append array arrays asterisks binarysearch changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork framework frequency function getlasterror givemetehcodez global grade gtkgcurlcompiling hacking hardware highest histogram include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list lists locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opensource overwrite owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing scripting segmentationfault sequential socket socketprograming standard string systemcall testing threads turboc unix user voidmain() wab windows.h windowsapi






