943,928 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 1646
  • C RSS
Apr 2nd, 2005
0

plzzzzzzzzzzzzz help me

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Junior Poster
some one is offline Offline
129 posts
since Apr 2005
Apr 2nd, 2005
0

Re: plzzzzzzzzzzzzz help me

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");
Reputation Points: 7
Solved Threads: 1
Junior Poster in Training
harshchandra is offline Offline
68 posts
since Nov 2004
Apr 2nd, 2005
0

Re: plzzzzzzzzzzzzz help me

no one can answer me :eek:
thanx any way :cry:
Reputation Points: 10
Solved Threads: 0
Junior Poster
some one is offline Offline
129 posts
since Apr 2005
Apr 2nd, 2005
0

Re: plzzzzzzzzzzzzz help me

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 2nd, 2005
0

Re: plzzzzzzzzzzzzz help me

:cheesy: thank you harshchandra :cheesy:
Reputation Points: 10
Solved Threads: 0
Junior Poster
some one is offline Offline
129 posts
since Apr 2005
Apr 2nd, 2005
0

Re: plzzzzzzzzzzzzz help me

Quote 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Apr 2nd, 2005
0

Re: plzzzzzzzzzzzzz help me

thanx narue and next time i will be patient i promise
Reputation Points: 10
Solved Threads: 0
Junior Poster
some one is offline Offline
129 posts
since Apr 2005
Apr 2nd, 2005
0

Re: plzzzzzzzzzzzzz help me

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
Reputation Points: 10
Solved Threads: 0
Junior Poster
some one is offline Offline
129 posts
since Apr 2005
Apr 2nd, 2005
0

Re: plzzzzzzzzzzzzz help me

If you do not mind can you explain your code Mr.narue
Reputation Points: 10
Solved Threads: 0
Junior Poster
some one is offline Offline
129 posts
since Apr 2005
Apr 2nd, 2005
0

Re: plzzzzzzzzzzzzz help me

^^^lol@Mr. Narue


What's up Na'?

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


Quote 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.
Reputation Points: 11
Solved Threads: 0
Junior Poster
Fasola is offline Offline
188 posts
since Jan 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: streams?
Next Thread in C Forum Timeline: Please tell me to write TSR's for windows enviorment





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC