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

#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;
}

Code tags added. -Narue

plz answer me as herry as u can
thanx

Recommended Answers

All 9 Replies

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 != '\0')
{ if(string==str)
count++;
i++;
}
if(count==strlen(str))
printf("Access granted");
else
printf("acess denied");

no one can answer me :eek:
thanx any way :cry:

>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.

:cheesy: thank you harshchandra :cheesy:

: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.

thanx narue and next time i will be patient i promise

can I ask you narue why you are angree I did not see you are code when I thanks harshchandra any way I am sorry if i have done any thing that would make you mad of me

If you do not mind can you explain your code Mr.narue

^^^lol@Mr. Narue


What's up Na'?

I'm back again, my well runneth dry and I'm thristy for more knowledge :D

>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 ) ) { //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;  
}

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' ) //I know what \n means, but what does \0 mean?
      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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.