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");
harshchandra
Junior Poster in Training
68 posts since Nov 2004
Reputation Points: 7
Solved Threads: 1
>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:
#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;
}
strcmp is just a loop that goes over both strings and terminates early if they're not equal at any point:
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;
}
>no one can answer me
Be patient and someone competent will happen by. Sometimes this takes days.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
:cheesy: thank you harshchandra :cheesy:
Fine, be that way. But understand that if you aren't allowed to use strcmp, strlen is probably also not allowed.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401