How to make a password show as stars

Does anyone know how to make it so that when you are typing in a passw

Page 1 of 1

7 Replies - 35060 Views - Last Post: 03 August 2008 - 05:04 AM Rate Topic: -----

#1 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

How to make a password show as stars

Posted 02 August 2008 - 01:51 PM

Can anyone tell me how to make it so that when you enter in the password it shows as "*"'s? thank you in advance

#include<iostream>
#include<string>
using namespace std;

int main(void)
{
	beginning:
	system("CLS");
	system("TITLE Enter Password");
	string password;
	string playgames;
	cout << "LOGON: ";
	cin >> password;
	if (password == "passwordhere"){
		system("TITLE Welcome");
		system("CLS");
		cout << "Welcome, master HAXIFIX" << endl;
		system("PAUSE");
		goto playgames;
	}else {
		cout << "Incorrect Username" << endl;
		cout << "-- CONNECTION TERMINATED --" << endl;
		system("PAUSE");
		goto exit;
	}
	exit:
	return 0;
	playgames:
	system("CLS");
	system("TITLE Hello, Mr. HAXIFIX");
	cout << "Would you like to play a game?" << endl;
	cout << "Answer: ";
	cin >> playgames;
	if (playgames == "Yes" || playgames == "yes"){
		goto whatgame;
	}else {
		cout << "Ok, I will ask later." << endl;
		system("PAUSE");
		goto playgames;
	}
	whatgame:
	system("PAUSE");
	return 0;
}

This post has been edited by C++ Programmer: 02 August 2008 - 01:51 PM


Is This A Good Question/Topic? 0
  • +

Replies To: How to make a password show as stars

#2 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: How to make a password show as stars

Posted 02 August 2008 - 02:07 PM

http://faq.cprogramm...p;id=1043284392
Was This Post Helpful? 0
  • +
  • -

#3 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: How to make a password show as stars

Posted 02 August 2008 - 02:10 PM

Click here

/***************************/
/** SECURE PASSWORD ENTRY **/
/**   BY DANNY BATTISON   **/
/**  gabehabe@hotmail.com **/
/***************************/

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

using namespace std;

class Password /* Password class... will store all necessary data */
{
    protected: /* A password should be protected, right? */
        string password; /* The string to store the password */
        string input; /* The string to store the input */

    public:
        /* Constructor, pass a string to it (the actual password) */
        Password (string pass) {this->password = pass;}
        void Input () /* Get the password from the user */
        {
            while (true) /* Infinite loop, exited when RETURN is pressed */
            {
                char temp;
                temp= getch (); /* Get the current character of the password */
                if (GetAsyncKeyState (VK_RETURN)) /* If the user has pressed return */
                    return; /* Exit the function */
                input += temp;
                cout << '*'; /* Print a star */
            }
        }
        bool Compare () /* Check if the input is the same as the password */
        {
            if (password.length() != input.length()) /* If they aren't the same length */
                return false; /* Then they obviously aren't the same! */
            for (unsigned int i = 0; i <= input.length(); i++)
            { /* Loop through the strings */
                if (password[i] != input[i])
                    return false; /* If anything is not a match, then they are not the same */
            }
            return true; /* If all checks were passed, then they are the same */
        }
};

int main ()
{
    /*
     * NOTE: We could get a password from anywhere here, for example,
     *       we could import it from an encrypted file
     */
    Password pass ("gabehabe"); /* Assign password a value with our constructor */
    pass.Input (); /* Get the input from the user */

    if (pass.Compare()) /* If they match, user could gain access to something */
        cout << "\nThat's correct!"; 
    else /* Otherwise, give them some abuse! */
        cout << "\nYou son of a bitch! That's wrong!"; 

    cin.get (); /* Pause for input */
    return EXIT_SUCCESS; /* Program was executed successfully */
}


Avoid using goto, anything you want to do with goto can be broken down into loops and functions.

Was this post helpful? :wink:
Was This Post Helpful? 1
  • +
  • -

#4 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: How to make a password show as stars

Posted 02 August 2008 - 02:22 PM

yes it was thank you
Was This Post Helpful? 0
  • +
  • -

#5 F!st!cuffs  Icon User is offline

  • D.I.C Head

Reputation: 12
  • View blog
  • Posts: 153
  • Joined: 15-July 08

Re: How to make a password show as stars

Posted 02 August 2008 - 04:10 PM

Haha gabehabe "You son of a bitch! That's wrong!" [Edit] "Now go make me a soda! I'm thirsty!"
Was This Post Helpful? 0
  • +
  • -

#6 jwwicks  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 24
  • View blog
  • Posts: 162
  • Joined: 31-July 08

Re: How to make a password show as stars

Posted 02 August 2008 - 10:10 PM

Hello,



View PostC++ Programmer, on 2 Aug, 2008 - 12:51 PM, said:

Can anyone tell me how to make it so that when you enter in the password it shows as "*"'s? thank you in advance




C++ doesn't provide a standard way of doing this just check the FAQ....



http://www.parashift....html#faq-15.18



I'm trying to work out a manipulator that will handle this so you can enter something like...



cin >> mask_input('*') >> password;



JW
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Re: How to make a password show as stars

Posted 02 August 2008 - 10:16 PM

View Postjwwicks, on 3 Aug, 2008 - 01:10 AM, said:

C++ doesn't provide a standard way of doing this just check the FAQ....

Eww, it's a canned usenet answer!

C++ Programmer: what you'll need to do is trap the input on a per character basis. So you can't use the standard getc or getchar that wait for enter. Same with scanf, as it triggers off the enter key as well. You can either read the keyboard buffer, or define your own input command, & then on a 1 to 1 basis, for each character input (as long as it is not enter) output the character you wish instead.
Was This Post Helpful? 1
  • +
  • -

#8 C++ Programmer  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 20
  • View blog
  • Posts: 548
  • Joined: 12-June 08

Re: How to make a password show as stars

Posted 03 August 2008 - 05:04 AM

kk, thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1