954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

plzzzzzzzzzzzzz help me

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

some one
Junior Poster
129 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

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
 

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

some one
Junior Poster
129 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

>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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

:cheesy: thank you harshchandra :cheesy:

some one
Junior Poster
129 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 
: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
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

thanx narue and next time i will be patient i promise

some one
Junior Poster
129 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

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

some one
Junior Poster
129 posts since Apr 2005
Reputation Points: 10
Solved Threads: 0
 

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

some one
Junior Poster
129 posts since Apr 2005
Reputation Points: 10
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 :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.

Fasola
Junior Poster
188 posts since Jan 2005
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You