mask password and store as string

Hi, my program requires a username and password to gain access. I need to mask the password input with *. I have researched heaps and found codes that don't work for what I want.
After the user enters the password, it is checked against a password already stored. the password is string 'alpha'.
Below is the login section of the program.Any help would be fantastic.
Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// login
int login ()
{
    // login prompt
    cout <<"\n\t Login \n" <<endl;
    cout <<" For Guest Access \n";
    cout <<"**Username: Guest**\n";
    cout <<"**Password: Guest** \n\n";
    cout <<" To Quit \n";
    cout <<"**Username: /\n\n";
    cout <<"Username:";
    cin >> beta;        //"username" string
        if (beta == "/")
        {
            return 1;   //quit function
        }
    cout <<"\nPassword:";
    cin >> alpha;
}
I don't know if this is what u exactly wanted

What this does is... It has 2 strings beta&Alpha and it opens the login where it asks u for a username and password... Once u type the username and password... it checks if the username typed is Guest and if the password is Guest..

if(beta != "Guest" || alpha != "Guest") so what this says is
if Username is not Guest And Password is not Guest return 1 aka exit

else ... so what the else means here is If both are correct it will proceed :)

You could also store the usernames and passwords in a text file and read them, but anyone can find the text file and change it and gain access :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
int main()
{
    string beta;
    string alpha;
    // login prompt
    cout <<"\n\t Login \n" <<endl;
    cout <<" For Guest Access \n";
    cout <<"**Username: Guest**\n";
    cout <<"**Password: Guest** \n\n";
    cout <<" To Quit \n";
    cout <<"**Username: /\n\n";
    cout <<"Username:";
    cin >> beta;        //"username" string
        if (beta == "/")
        {
            return 1;   //quit function
        }
    cout <<"\nPassword:";
    cin>>alpha;
    cout<<"\n\nChecking...";
    if(beta != "Guest" || alpha != "Guest")
    {
        cout<<"Wrong Username or Password!\n\n\n";
        return 1;
    }
    else cout<<"Access Approved\n\n\n";

}
Last edited on
There is no one way to hide what the user is typing; C++ has no knowledge of what a keyboard or a screen is. That sort of thing is handled by your operating system, so you need to use libraries beyond the C++ standard.

Using Windows?
You can actually :)
I got confused in his question and thought that he didn't knew how to check if password is correct or wrong :)

but tnx to Moschops i see that he wants to type and * show up on screen but the program would still know what u typed :)

Here you go ... try this piece of code :)

This program will let u type a password and then when u press enter it will tell you what you typed :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>

using namespace std;

int main()
{
  int ch;
  char pword[BUFSIZ];
  int i = 0;

  puts ("Enter your password");
  fflush(stdout);

  while ((ch = getch()) != EOF
          && ch != '\n'
          && ch != '\r'
          && i < sizeof(pword) - 1)
  {
    if (ch == '\b' && i > 0)
    {
      printf("\b \b");
      fflush(stdout);
      i--;
      pword[i] = '\0';
    }
    else if (isalnum(ch))
    {
      putchar('*');
      pword[i++] = (char)ch;
    }
  }

  pword[i] = '\0';

  printf ("\nYou entered >%s<", pword);

  return 0;
}

Note that this solution relies on the non-standard conio.h
I was expecting u :)
Yes it does and you are right :)

I learnt C++ on my own and the problem is that i skipped some parts and i have been in a situation where people asked me something and idk what that is but i only don't know the names of the functions :)

Btw i am working on a game and i'm stuck at the map a little i could use some help if anyone is interested :)
http://www.pasteall.org/32479
This work for me .
Thanks for all your replies. but something still doesn't work.
these codes store the password as a char and therefore I can't test it against my string. I need to convert the char into a string.
if you need the rest of the code, please ask.
Josh
Topic archived. No new replies allowed.