简体   繁体   中英

Masking password in c++ login program

I am somewhat between beginner and intermediately well-versed in c++, and I managed to write a basic login program in c++ using Dev C++, Windows 7. This program was capable of taking in inputs for the username and password and comparing them to see if they were equal. If so, it would welcome the user and terminate the program and if not, it would loop back to the beginning (infinitely).
Then I looked at some tutorials on the net to mask the password in and tried to do so myself, in c++. 的密码,并尝试用c ++编写。
With the code that I have included below, I do not get any errors, but I do not get the desired output either. In the end, even if I enter the right 'password', I always get said to try again. Also, the program masks everything, including the 'Enter' and 'Backspace' key. Here's the code:

#include <iostream.h>
#include <stdlib.h>
#include <conio.h>

using namespace std;
int main()

   {
   int i=0;string u;char parr[i],ch;
   while (1)
   { 
     system("cls");
     cout<<"Enter username."<<endl;
     cin>>u;
     system("cls");
     cout<<"Enter password."<<endl;
     for (i=0;i<=8;i++)
     {   
         ch=getch();
         parr[i]=ch;
         ch='*';
         cout<<ch;
     }

 string p(parr);

 if (u=="username" && p=="password")
 {
    system("cls");
    cout<<"Welcome!";
    break;
 }

 else
 {
     system("cls");
     cout<<"Username and password entered does not match! Please try again.";
 }
 getch();
   }    
     getch();  
    }  

Also, is there any way to make the password masked only for the characters and for howewer much the length of the password given by the user as input,ie, not just 8 characters of the word 'password', but if the user enters "notapassword", then all of the characters must be masked, not just the first eight of them.

Some operating systems already provide a getpass function or advanced versions thereof. For example, the NetBSD implementation is here , the documentation is here . Read that code if you have to implement the function yourself.

Why did you not print out the value of p before asking this question?

You are creating an array called parr which is filled with random data. (Some compilers will fill the array with known, non-zero data, but this is different between compilers and between different modes of each compiler).

Your array is 0 bytes long. (In fact, creating an array whose size is given by a variable is not allowed by standard C++).

You are retrieving 9 characters from the keyboard and writing them in sequence to the 0-byte array parr .

You are then creating a string called p based on the characters starting at parr[0] and continuing until a zero-valued character is found. Since you have never written a zero-valued character to the array, this means that p will contain the characters that have been typed, followed by an unknown number of unknown characters, depending on what happens to be in the computer's memory after the start of parr . This could be zero bytes (so that you end up with a 9-character string in p ) or it could be a million bytes, or the program might crash because it hits non-existent memory before if finds a character whose value is zero.

You are then comparing a string which contains at least 9 characters with "password", which is 8 characters long. Of course it will never match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM