3

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 C and tried to do so myself, in 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,i.e, 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.

Anchith Acharya
  • 369
  • 1
  • 4
  • 16
  • 1.[using std namespace is bad]( http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-in-c-considered-bad-practice) 2. Use curses library to do this – Ed Heal Jun 12 '16 at 09:51
  • 1
    I think you should teach yourself how to debug. After you run the program step by step, even if you won't find the problem yourself, you'll probably be able to ask a more precise question that will be easier to answer. – selalerer Jun 12 '16 at 09:54
  • @EdHeal Curses? On Windows? – melpomene Jun 12 '16 at 09:54
  • Where is windows mentioned in the question? – Ed Heal Jun 12 '16 at 09:55
  • @EdHeal ``, `getch()`, `system("cls")` – melpomene Jun 12 '16 at 09:57
  • But 'using namespace std' is compulsary in dev c++, as far as I know – Anchith Acharya Jun 12 '16 at 09:58
  • @melpomene - Perhaps BTW `getch` is a part of curses. `cls` could be a UNIX command (I have one set up). `conio,h` could be any header file, – Ed Heal Jun 12 '16 at 10:01
  • @AnchithAcharya - No it is not – Ed Heal Jun 12 '16 at 10:01
  • 1
    @selalerer I just realized that I don't even know what debugging exactly is at all! When I press debug after compiling, it just runs the program. And here I am, diving into arrays and complex login programmes! – Anchith Acharya Jun 12 '16 at 10:03
  • @AnchithAcharya I don't know which development tool you are using (Visual Studio?), in any case you can always google "debugging with [PUT YOUR DEVELOPMENT TOOL NAME HERE]". – selalerer Jun 12 '16 at 10:08
  • Its Dev C++. And thanks, I'll check it out. – Anchith Acharya Jun 12 '16 at 10:18
  • `parr[i]` is a `char` array of 1 element and you are filling it with 8 `char`s. `string (const char* s)` creates the string from a null terminated c string and you are not initializing `parr`. – perencia Jun 12 '16 at 10:21
  • @perencia But in the for loop, as the value of 'i' gets increased, doesn't the size of the array increase every step? Also, can you please explain in layman terms whatever you said in the second sentence? – Anchith Acharya Jun 12 '16 at 10:30

2 Answers2

3

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.

Roland Illig
  • 40,703
  • 10
  • 88
  • 121
3

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.

nugae
  • 499
  • 2
  • 5