1

what is the technique to get a masked password input as follows:

enter image description here

danial weaber
  • 846
  • 2
  • 17
  • 37

1 Answers1

-3
#include <iostream>
#include<windows.h> // for system("pause")
#include<conio.h>  //for getch()

using namespace std;

int main()
{
    char x[10];

    cout<<"enter a password\n";

    for(int i=0; i<10;i++){
        x[i]=getch();
        cout<<"*";

        if(x[i]=='\r') //check if enter key is pressed
            break;

        else if(x[i]=='\b'){

            if(i==0)
                cout<<"\b"<<" "<<"\b";
            else if(i>=1){
                x[i-1]='\0';//make the previous byte null if backspase is pressed
                i=i-2;
                cout<<"\b"<<" "<<"\b\b"<<" "<<"\b";
            }

         }
    }

    cout<<endl<<"the password is  :"<<x<<endl;
    system("pause");
 }
user93353
  • 13,733
  • 8
  • 60
  • 122
danial weaber
  • 846
  • 2
  • 17
  • 37
  • 2
    `getch()` is deprecated. Use `_getch()` instead. (see http://msdn.microsoft.com/de-de/library/ms235446(v=vs.80).aspx) – Zane Dec 03 '12 at 17:12
  • 1
    -1: The requirement was "*the C++ method without using winAPI*". `getch()` (or `_getch()`, if you prefer) is a Windows function, not a C++ function. – Robᵩ Dec 03 '12 at 17:13
  • 1
    @Rob: right, `_getch()`is Windows, not C++. But where is the requirement for an OS-independent solution? The question is even tagged with Windows. – Zane Dec 03 '12 at 17:16
  • @Zane - I edited the tags to add the `windows` tag - because the solution was obviously a windows one. It does a `#include `,`#include ` and uses `getch/_getch`. All these are non-standard and will obviously not work on all platforms. And there are no platform independent solutions. – user93353 Dec 03 '12 at 22:03