I'm writing a program to reset a password, and I am having some issues with escaping the program before the final (accept new password) condition is made. I am using conio.h's getch() function to accept input to write it to a string (using string.h)
I'm using Borland TurboC++ V4.0 to keep compatibility, as this is add-on code to an old MS DOS program. I would use a more current ide/compiler if I could.
This is a small snippet of the program; I have left out the headers and the new password/check password lines as they function atm. I am very new to C programming, I am aware this is probably not the easiest way to write, and there is a lot of redundancy. I have tried creating a flag to exit the program, as well as a break function, and they both run into the same problem; they treat '\r' as esc and vise versa.
main()
{
char password[] = "0000000000000000000";
char newpassword[] = "0000000000000000000";
char checkpassword[] = "0000000000000000000";
char answer[] = "hrigsetup";
initgraph(&gd,&gm,"C://TC//BGI");
//draw screen
rectangle(0,0,screen_x,screen_y);
rectangle(screen_x/4,screen_y/3,(3*(screen_x/4)),(2*(screen_y/3)));
rectangle(((screen_x/4)+50),((screen_y/3)+45),(3*(screen_x/4)-50),((screen_y/3)+20));
rectangle(((screen_x/4)+50),((screen_y/3)+90),(3*(screen_x/4)-50),((screen_y/3)+65));
rectangle(((screen_x/4)+50),((screen_y/3)+135),(3*(screen_x/4)-50),((screen_y/3)+110));
//write text
setcolor(headercolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,2);
settextjustify(CENTER_TEXT,TOP_TEXT);
outtextxy(screen_x/2,((screen_y/4)),"Change Password");
setcolor(drawcolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,TOP_TEXT);
outtextxy((screen_x/2)-108,((screen_y/2)-70),"Enter Old Password");
outtextxy((screen_x/2)-108,((screen_y/2)-25),"Enter New Password");
outtextxy((screen_x/2)-108,((screen_y/2)+20),"Re-Enter New Password");
//enter old password
//password entry
while(unlock != 1 && esc_key != 1)
{
while((password[p] = getch()) != '\r') //mask text logic
{
if(password[p] == '\b') //tolerate a backspace
{
if(p == 17 && endchar == 0) //Logic password field
{ //bounds/overflow protection
password[p] = ' ';
setcolor(clear);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,TOP_TEXT);
outtextxy((screen_x/2)-(100-(p*8)),(screen_y/2)-50,"Û");
endchar = 1;
}
else
{
password[p] = ' ';
setcolor(clear);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,TOP_TEXT);
outtextxy((screen_x/2)-(100-((p-1)*8)),(screen_y/2)-50,"Û");
p--;
if(p <= -1)
p = 0;
}
}
else if(password[p] == 27 ) //esc logic
{ //have tried 27, 0x1b, '\027
break;
}
else
{
if(clear_bar == 1) //erase contents of password
{ //box
setcolor(clear);
settextstyle(DEFAULT_FONT,HORIZ_DIR,3);
outtextxy((screen_x/2)-2,(screen_y/2)-59,"ÛÛÛÛÛÛÛÛÛ");
setcolor(drawcolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,TOP_TEXT);
clear_bar = 0;
}
setcolor(drawcolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
settextjustify(LEFT_TEXT,TOP_TEXT);
outtextxy((screen_x/2)-(100-(p*8)),(screen_y/2)-50,"*");
if(p == 17)
{
p = 17;
endchar = 0;
}
else
p++;
}
}
if(strcmp(password, answer) !=0 && esc_key != 1) //check password = wrong,
{ //clear password box, fill
setcolor(headercolor); //read and write "incorrect"
settextjustify(CENTER_TEXT,TOP_TEXT); //in the box, write "Try Again"
settextstyle(DEFAULT_FONT,HORIZ_DIR,3); //below input box
outtextxy((screen_x/2)-2,(screen_y/2)-59,"ÛÛÛÛÛÛÛÛÛ");
setcolor(drawcolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(screen_x/2,(screen_y/2)-50,"Incorrect");
outtextxy(screen_x/2,(screen_y/2)+100,"Try Again");
unlock = 0;
clear_bar = 1;
newp_ent = 0;
password[p] = 0;
p = 0;
}
if(strcmp(password, answer) == 0 && esc_key != 1) //check password = correct
{ // clear and fill input box
setcolor(unlock_color); //green and display "Correct"
settextjustify(CENTER_TEXT,TOP_TEXT); //write "Press Any Key" below
settextstyle(DEFAULT_FONT,HORIZ_DIR,3); //the input box
outtextxy((screen_x/2)-2,(screen_y/2)-59,"ÛÛÛÛÛÛÛÛÛ");
setcolor(clear);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(screen_x/2,(screen_y/2)+100,"ÛÛÛÛÛÛÛÛÛ");
setcolor(drawcolor);
settextstyle(DEFAULT_FONT,HORIZ_DIR,1);
outtextxy(screen_x/2,(screen_y/2)-50,"Correct");
unlock = 1;
newp_ent = 0;
}
}
There is a line missing fom the code block which detects the Esc key. You have detected it but not set the flag you have for this purpose.
else if(password[p] == 27 )
{
esc_key = 1; // <-- add this line
break;
}