| | |
plzzzzzzzzzzzzz help me
![]() |
•
•
Join Date: Apr 2005
Posts: 129
Reputation:
Solved Threads: 0
hi every 1 i am new and Ihope that you would help me I have to write a program that will allow you to enter if you enter the password without using strcmp function and here is my try
Code tags added. -Narue
plz answer me as herry as u can
thanx
C Syntax (Toggle Plain Text)
#include <iostream> #include <cstdio> using namespace std; bool password(); int main() { if( password()) cout<< "logged on"<<"\n"; else cout<< "access deniled"<<"\n"; return 0; } bool password() { char s[80]={"pass"}; cout<< "enter password"<<"\n"; gets(s); if( s=="pass") { cout<<"invalid password"<<"\n"; return false; } return true; }
plz answer me as herry as u can
thanx
•
•
Join Date: Nov 2004
Posts: 68
Reputation:
Solved Threads: 1
one method can be that u match each and every character of the string user had given in the input.....
code can be like this ---
gets(string);
i = 0,count=0;
while(string[i] != '\0')
{ if(string[i]==str[i])
count++;
i++;
}
if(count==strlen(str))
printf("Access granted");
else
printf("acess denied");
code can be like this ---
gets(string);
i = 0,count=0;
while(string[i] != '\0')
{ if(string[i]==str[i])
count++;
i++;
}
if(count==strlen(str))
printf("Access granted");
else
printf("acess denied");
>if( s=="pass")
If we could do this, why would we need strcmp?
>gets(s);
gets is evil. There's no way to make it safe, so you would do well to forget it even exists.
Let's get you started off in the right direction. Unless you're a stubborn C programmer who can't see the benefit of C++ I/O, use iostreams for you input and output:
strcmp is just a loop that goes over both strings and terminates early if they're not equal at any point:
>no one can answer me
Be patient and someone competent will happen by. Sometimes this takes days.
If we could do this, why would we need strcmp?
>gets(s);
gets is evil. There's no way to make it safe, so you would do well to forget it even exists.
Let's get you started off in the right direction. Unless you're a stubborn C programmer who can't see the benefit of C++ I/O, use iostreams for you input and output:
C Syntax (Toggle Plain Text)
#include <cstdlib> #include <iostream> using namespace std; bool is_valid ( const char *pass ); int main() { char pass[80]; cout<<"Password: "; if ( !cin.getline ( pass, sizeof pass ) ) { cerr<<"Input error"<<endl; return EXIT_FAILURE; } if ( is_valid ( pass ) ) cout<<"Logged in"<<endl; else cout<<"Invalid password"<<endl; return EXIT_SUCCESS; }
C Syntax (Toggle Plain Text)
bool is_valid ( const char *pass ) { const char *sys_pass = "pass"; while ( *pass == *sys_pass ) { if ( *pass == '\0' ) break; ++pass; ++sys_pass; } return *pass == *sys_pass; }
Be patient and someone competent will happen by. Sometimes this takes days.
In case you were wondering, yes, I do hate you.
•
•
Join Date: Jan 2005
Posts: 188
Reputation:
Solved Threads: 0
^^^lol@Mr. Narue
What's up Na'?
I'm back again, my well runneth dry and I'm thristy for more knowledge
What's up Na'?
I'm back again, my well runneth dry and I'm thristy for more knowledge

•
•
•
•
Originally Posted by Narue
>if( s=="pass")
If we could do this, why would we need strcmp?
>gets(s);
gets is evil. There's no way to make it safe, so you would do well to forget it even exists.
Let's get you started off in the right direction. Unless you're a stubborn C programmer who can't see the benefit of C++ I/O, use iostreams for you input and output:
strcmp is just a loop that goes over both strings and terminates early if they're not equal at any point:#include <cstdlib> #include <iostream> using namespace std; bool is_valid ( const char *pass ); int main() { char pass[80]; cout<<"Password: "; if ( !cin.getline ( pass, sizeof pass ) ) { //What does "!" do in !cin.getline...check private messages and I'll tell you why I ask cerr<<"Input error"<<endl; //what does cerr do? return EXIT_FAILURE; //Where is EXIT_FAILURE returned to? } if ( is_valid ( pass ) ) cout<<"Logged in"<<endl; else cout<<"Invalid password"<<endl; return EXIT_SUCCESS; }
>no one can answer mebool is_valid ( const char *pass ) { const char *sys_pass = "pass"; while ( *pass == *sys_pass ) { if ( *pass == '\0' ) //I know what \n means, but what does \0 mean? break; ++pass; ++sys_pass; } return *pass == *sys_pass; }
Be patient and someone competent will happen by. Sometimes this takes days.
![]() |
Other Threads in the C Forum
- Previous Thread: streams?
- Next Thread: Please tell me to write TSR's for windows enviorment
Views: 1557 | Replies: 9
| Thread Tools | Search this Thread |
Tag cloud for C
api array arrays binary binarysearch c++ c/c++ char character cheating code coke command conversion convert copy database decimal directory download dude dynamic ebook error exec factorial fgets file fork function functions getline givemetehcodez grade graphics hardware homework i/o input insert int integer lazy line linked linkedlist linux list lists loop malloc matrix memory multi mysql no-effort numbers output path pointer pointers problem process program programming programs read recursion recursive recv recvblocked reverse scan scanf sdl shape single socketprograming socketprogramming sockets sorting spoonfeeding stdin string strings strtok structures student system turbo-c turboc txt unix user variable visualstudio voidmain() whythiscodecausesegmentationfault win32 windows






