hey guys, i'm using Borland 6 c++ in windows xp, now that we got that i have a problem with my program, i have recently created a password function in a programm that works so that when they try to type in their password it shows the * instead of the character for security measures (and just cus it is cool too lol) but the problem is that when someone makes a typo and wants to delete a character it still displays another *, is there a way that i can make the programm destroy one character when the backspace button is struck? here is my function so that you guys can look at it too.

#include <iostream>
#include <conio>
#include <condefs.h>
#pragma hdrstop
#pragma argsused


string EnterPassword(){
    string numAsString = "";
    char ch = getch();
    while (ch != '\r') {
        cout << '*';
        numAsString += ch;
        ch = getch();
    }
    return numAsString;
}

void passfunction();



int main(int argc, char **argv)
{
    passfunction();
    return 0;
}



void passfunction()
{

    cout << endl;
    cout << "password:";

    string password = EnterPassword();

    if( passowrd == "rbrown" ){
        cout << "Hello Mr. Brown, Welcome back" << endl;
        system("PAUSE");
        leave = true;
        return;
    }

    else if (password != "rbrown"){
        cout << "incorrect password, please try again." << endl;
        system("PAUSE");
        leave = false;
        passfunction (leave);
    }

    cout << password << endl;
    system("PAUSE");
    return;

}

Output a backspace ('\b') when on is input? And back up the string pointer and such?

hey guys, i'm using Borland 6 c++ in windows xp, now that we got that i have a problem with my program, i have recently created a password function in a programm that works so that when they try to type in their password it shows the * instead of the character for security measures (and just cus it is cool too lol) but the problem is that when someone makes a typo and wants to delete a character it still displays another *, is there a way that i can make the programm destroy one character when the backspace button is struck? here is my function so that you guys can look at it too.

#include <iostream>
#include <conio>
#include <condefs.h>
#pragma hdrstop
#pragma argsused


string EnterPassword(){
    string numAsString = "";
    char ch = getch();
    while (ch != '\r') {
        cout << '*';
        numAsString += ch;
        ch = getch();
    }
    return numAsString;
}

void passfunction();



int main(int argc, char **argv)
{
    passfunction();
    return 0;
}



void passfunction()
{

    cout << endl;
    cout << "password:";

    string password = EnterPassword();

    if( passowrd == "rbrown" ){
        cout << "Hello Mr. Brown, Welcome back" << endl;
        system("PAUSE");
        leave = true;
        return;
    }

    else if (password != "rbrown"){
        cout << "incorrect password, please try again." << endl;
        system("PAUSE");
        leave = false;
        passfunction (leave);
    }

    cout << password << endl;
    system("PAUSE");
    return;

}
#include <conio.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream.h>
//***************************************************************************//
//************Program for entering password of max 8 characters**************/
//************Press backspace if you make a mistake in the middle************//
//************Press Enter once you are done**********************************//
void main()
{
	clrscr();
	int i=0,j;
	char ch,temp[8],arr[8],*password;
	strcpy(arr,"");
	while(i<9)
	{
		ch=getch();
		if(i==8&&(ch==13))
		break;
		if(ch==8)
		{
			if(i!=0)
			i--;
		}
		else
		if(ch==13&&i<8)
		{
			temp[i]='\0';
			break;
		}
		else
		if(i<8)
		{
			temp[i]=ch;
			arr[i]='*';
			i++;
		}
		clrscr();
		gotoxy(1,1);
		for(j=0;j<i;j++)
		cout<<arr[j];
	}
	password=new char[i];
	for(j=0;j<i;j++)
	password[j]=temp[j];
	password[j]='\0';
	cout<<"\nThe Password you typed is :";puts(password);
	getch();
}

I just wrote this small program.I know there may be 100 better ways to write it but this was the first program I wrote for handling passwords.You can always use the getpass() function but there is a catch with that.

Um... hello? This thread is like 2 years old, and plus your code isn't the finest quality... (like using void main and outdated headers)

Um... hello? This thread is like 2 years old, and plus your code isn't the finest quality... (like using void main and outdated headers)

hmm..yes..That was before I joined this forum.Actually that was just my first post.I stumbled upon that by mistake and didnt see the date..And yeah I was really ignorant of the fact that you should never use void main().And the reason very obvious.."My teachers never told me it was wrong"..Anyways thats not an excuse..And about the outdated header..As I said that was written a long time ago on a now outdated Borland C++ 3.0..So its all my mistake..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.