plzzzzzzzzzzzzz help me

Reply

Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

plzzzzzzzzzzzzz help me

 
0
  #1
Apr 2nd, 2005
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
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. bool password();
  5. int main()
  6. {
  7. if( password())
  8. cout<< "logged on"<<"\n";
  9. else
  10. cout<< "access deniled"<<"\n";
  11. return 0;
  12.  
  13. }
  14. bool password()
  15. {
  16. char s[80]={"pass"};
  17.  
  18. cout<< "enter password"<<"\n";
  19.  
  20. gets(s);
  21. if( s=="pass")
  22. {
  23. cout<<"invalid password"<<"\n";
  24. return false;
  25. }
  26.  
  27. return true;
  28. }
Code tags added. -Narue

plz answer me as herry as u can
thanx
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 68
Reputation: harshchandra is an unknown quantity at this point 
Solved Threads: 1
harshchandra harshchandra is offline Offline
Junior Poster in Training

Re: plzzzzzzzzzzzzz help me

 
0
  #2
Apr 2nd, 2005
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");
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: plzzzzzzzzzzzzz help me

 
0
  #3
Apr 2nd, 2005
no one can answer me :eek:
thanx any way :cry:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,311
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: plzzzzzzzzzzzzz help me

 
0
  #4
Apr 2nd, 2005
>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:
  1. #include <cstdlib>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. bool is_valid ( const char *pass );
  7.  
  8. int main()
  9. {
  10. char pass[80];
  11.  
  12. cout<<"Password: ";
  13. if ( !cin.getline ( pass, sizeof pass ) ) {
  14. cerr<<"Input error"<<endl;
  15. return EXIT_FAILURE;
  16. }
  17.  
  18. if ( is_valid ( pass ) )
  19. cout<<"Logged in"<<endl;
  20. else
  21. cout<<"Invalid password"<<endl;
  22.  
  23. return EXIT_SUCCESS;
  24. }
strcmp is just a loop that goes over both strings and terminates early if they're not equal at any point:
  1. bool is_valid ( const char *pass )
  2. {
  3. const char *sys_pass = "pass";
  4.  
  5. while ( *pass == *sys_pass ) {
  6. if ( *pass == '\0' )
  7. break;
  8.  
  9. ++pass;
  10. ++sys_pass;
  11. }
  12.  
  13. return *pass == *sys_pass;
  14. }
>no one can answer me
Be patient and someone competent will happen by. Sometimes this takes days.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: plzzzzzzzzzzzzz help me

 
0
  #5
Apr 2nd, 2005
:cheesy: thank you harshchandra :cheesy:
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 8,311
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 824
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: plzzzzzzzzzzzzz help me

 
0
  #6
Apr 2nd, 2005
Originally Posted by some one
: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.
In case you were wondering, yes, I do hate you.
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: plzzzzzzzzzzzzz help me

 
0
  #7
Apr 2nd, 2005
thanx narue and next time i will be patient i promise
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: plzzzzzzzzzzzzz help me

 
0
  #8
Apr 2nd, 2005
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2005
Posts: 129
Reputation: some one is an unknown quantity at this point 
Solved Threads: 0
some one some one is offline Offline
Junior Poster

Re: plzzzzzzzzzzzzz help me

 
0
  #9
Apr 2nd, 2005
If you do not mind can you explain your code Mr.narue
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 188
Reputation: Fasola is an unknown quantity at this point 
Solved Threads: 0
Fasola Fasola is offline Offline
Junior Poster

Re: plzzzzzzzzzzzzz help me

 
0
  #10
Apr 2nd, 2005
^^^lol@Mr. Narue


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:
#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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum


Views: 1557 | Replies: 9
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC