3 Replies - 531 Views - Last Post: 13 January 2013 - 03:40 AM Rate Topic: -----

#1 Alexie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 13

How to mask my password with ***?

Posted 12 January 2013 - 11:53 PM

#include<stdio.h>
#include<stdlib.h>


int main()
{
int Pass;

printf("Please enter the password: ");
scanf("%d", &Pass);

if(Pass==123456)
{
printf("\nHello! Eve. Welcome to Disney ATM\n\n");              
}
else if(Pass==654321)
{
printf("\nHello! Adam. Welcome to Disney ATM\n\n");
}
else
{
printf("\nAccess denied!!! :)/>\n\n");
}
system("pause");
return 0;

} 



how can i mask out the password. example if i type out my password, it will show as ******
And i would like to print out this --> printf("Processing - - - - - - -") in a loop. every 300 msec print one dot ("-").

thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: How to mask my password with ***?

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,531
  • Joined: 09-June 09

Re: How to mask my password with ***?

Posted 13 January 2013 - 01:20 AM

The solution to your problem is platform dependent.

http://www.cplusplus...icles/E6vU7k9E/
Was This Post Helpful? 0
  • +
  • -

#3 Alexie  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 12-January 13

Re: How to mask my password with ***?

Posted 13 January 2013 - 01:32 AM

Thanks for the reply. but I'm using dev C++
Was This Post Helpful? 0
  • +
  • -

#4 snoopy11  Icon User is offline

  • Engineering ● Software
  • member icon

Reputation: 762
  • View blog
  • Posts: 2,214
  • Joined: 20-March 10

Re: How to mask my password with ***?

Posted 13 January 2013 - 03:40 AM

If this is for windows then this is my
favourite solution although there are many others


#include <windows.h>
#include <stdio.h>


HANDLE hStdin;
DWORD fdwSaveOldMode;
bool flag = false;
VOID ErrorExit(LPSTR);
char* KeyEventProc(KEY_EVENT_RECORD);

int main()
{
	DWORD cNumRead = 1, fdwMode, i;
	INPUT_RECORD irInBuf[128]= {};
	int counter=0;
	char password[128] ={};
	char buf[10]={};
	// Get the standard input handle.

	hStdin = GetStdHandle(STD_INPUT_HANDLE);
	if (hStdin == INVALID_HANDLE_VALUE)
		ErrorExit((char*)"GetStdHandle");

	// Save the current input mode, to be restored on exit.

	if (! GetConsoleMode(hStdin, &fdwSaveOldMode) )
		ErrorExit((char*)"GetConsoleMode");

	// Enable the window and mouse input events.

	fdwMode = ENABLE_WINDOW_INPUT;
	if (! SetConsoleMode(hStdin, fdwMode) )
		ErrorExit((char*)"SetConsoleMode");

	// Loop to read and handle the next 100 input events.
	printf("Enter Password: ");
	while (counter++ <= 100)
	{
		// Wait for the events.

		if (! ReadConsoleInput(
			hStdin,      // input buffer handle
			irInBuf,     // buffer to read into
			128,         // size of read buffer
			&cNumRead) ) // number of records read
			ErrorExit((char*)"ReadConsoleInput");

		// Dispatch the events to the appropriate handler.

		for (i = 0; i < cNumRead; i++)
		{
			switch(irInBuf[i].EventType)
			{
			case KEY_EVENT: // keyboard input

				if (GetAsyncKeyState(VK_RETURN)) // if return has been pressed
				{
					flag = true;
					break; // break from the loop
				}
				else

					strcpy(buf,KeyEventProc(irInBuf[i].Event.KeyEvent));
				strcat(password,buf);
				break;






			}
		}

		if(flag)
			break;
	}
	if (!strcmp(password, "123456"))
		printf("\ncorrect\n");
	else
		printf("\nincorrect\n");
	// Restore input mode on exit.

	SetConsoleMode(hStdin, fdwSaveOldMode);

	scanf("%c",buf);
	return 0;
}

VOID ErrorExit (LPSTR lpszMessage)
{
	fprintf(stderr, "%s\n", lpszMessage);

	// Restore input mode on exit.

	SetConsoleMode(hStdin, fdwSaveOldMode);

	ExitProcess(0);
}

char* KeyEventProc(KEY_EVENT_RECORD ker)
{

	char buf[128]= {};
	if(ker.bKeyDown)
	{
		printf("*");

		sprintf(buf,"%c",ker.uChar.AsciiChar);

	}

	return buf;
}





I dont know if it will work on Dev C++ as its a very old compiler...


Snoopy.

This post has been edited by snoopy11: 13 January 2013 - 09:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1