I want to compare my password with the stored password but it don't compare correctly. WHY?
Please someone correct it.

#include <iostream>
      #include <string.h>
      #include <conio.h>
      #include <cstdlib> 
 
      using namespace std;
   
      int main()   {
      string my = "programming ";
      string pass = "";
      int s;
      int a;
      
      cout << "Enter password: ";
     
      do   {
           
     // backspace not allowed
      a = _getch();
      
      pass += char(a);
      
      cout << "*";
      
      }while( a != 13 );
   
      cout << endl << "Password is: " << pass << endl;
      cerr << " \n My: " << my << " \n ";
      //s =  strlen(pass);
      //cerr << " Size: " << s;
      //if ( strcmp (    pass ,    my ) == 0 ) {
        if ( pass == my ) {
         cerr << "\n\n \t Password Comfirmed";     
      } else {
           cerr << "\n\n \t Not Confirmed";   
          }
      system("pause");
      
      return 0;
   
      }
mvmalderen commented: 28 posts!! Still no code tags! -2

Recommended Answers

All 8 Replies

I would try using the compare function of the string class. I believe that your test pass == my is testing that your string objects are the same objects rather than whether their contents are the same. Also make sure you are using code tags when you post.

Try looking at your program again. Look at it closely when you type your password. I dont mean your typing it wrong, but when you press enter. Another star is added.. ex:
password: **** (hello)
password: hello ***** is wrong.. I'm sorry I cant explain it so well. You can't use enter just like you can't use backspace. I believe.
Sorry if that is wrong.

commented: Your statement about the ENTER-key was correct, you're the actual thread solver ! +10

This program is real rubbish, old style headers, conio, half of the code is commented out, system("pause") (don't use it), etc...
My recommendation is: migrate to standard C++ first, they really should make it a forum rule...

BTW, Does it really hurt you to use code tags:
1) in the Rules you were asked to read when you registered
2) in the text at the top of this forum
3) in the announcement at the top of this forum titled Please use BB Code and Inlinecode tags
4) in the sticky post above titled Read Me: Read This Before Posting
5) any place CODE tags were used
6) Even on the background of the box you actually typed your message in

(as already discovered by valinux):
If you still really want to fix your code, then you should add a check before adding the character to the variable, otherwise there will be an ENTER in the pass-string where you compare the real password to, always resulting in a Non Confirmed message, a simple check might look like this: if( a != 13 ) pass += a; :)

Thanks tux4life :)

tux4life, I challenge you to write the same program with the same features and restrictions and show me how it's so much better than mirfan00's that he deserved all of that harassment. You're quick to mouth off, but can you back it up with your own perfection?

This program is real rubbish, old style headers, conio, half of the code is commented out, system("pause")

The only old style header is <string.h>, and even though it's officially deprecated, it's not going away any time soon because then compatibility with C would go out the window.

There's no way to replace typed characters with a star using standard code. To do that you have to use something that isn't portable, and conio is as good a choice as any for a toy program.

Only 3 lines out of over 40 are commented out. I didn't get good grades in grade school math classes, but I'm pretty sure 3 isn't even close to half of 40.

You forgot to say that <string> should be included because the code uses the standard C++ string class. And you forgot to say that '\r' is a portable replacement for 13. For all the complaining about old style headers, you forgot to say that the only code using them is commented out so both can be removed. I see you complaining about the same things over and over, and ignoring other things that are in the same league or worse. It kind of makes me think that you don't really know what you're talking about and just copy people who do.

tux4life, I challenge you to write the same program with the same features and restrictions and show me how it's so much better than mirfan00's that he deserved all of that harassment. You're quick to mouth off, but can you back it up with your own perfection?


The only old style header is <string.h>, and even though it's officially deprecated, it's not going away any time soon because then compatibility with C would go out the window.

There's no way to replace typed characters with a star using standard code. To do that you have to use something that isn't portable, and conio is as good a choice as any for a toy program.

Only 3 lines out of over 40 are commented out. I didn't get good grades in grade school math classes, but I'm pretty sure 3 isn't even close to half of 40.

You forgot to say that <string> should be included because the code uses the standard C++ string class. And you forgot to say that '\r' is a portable replacement for 13. For all the complaining about old style headers, you forgot to say that the only code using them is commented out so both can be removed. I see you complaining about the same things over and over, and ignoring other things that are in the same league or worse. It kind of makes me think that you don't really know what you're talking about and just copy people who do.

Well, when I replied his code wasn't surrounded by code tags, well, you can see the result in my post :(

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.