getch() 将回车 return 等同于 esc 键
getch() equating carriage return to esc key
我正在编写一个重设密码的程序,但在最终(接受新密码)条件成立之前,我在退出程序时遇到了一些问题。我正在使用 conio.h 的 getch() 函数接受输入以将其写入字符串(使用 string.h)
我使用 Borland TurboC++ V4.0 来保持兼容性,因为这是 add-on 旧 MS DOS 程序的代码。如果可以的话,我会使用更新的 ide/compiler。
这是程序的一小段;我省略了 headers 和新的 password/check 密码行,因为它们在 atm 上起作用。我是 C 编程的新手,我知道这可能不是最简单的编写方式,而且有很多冗余。我已经尝试创建一个标志来退出程序,以及一个中断函数,它们都 运行 进入了同样的问题;他们将 '\r' 视为 esc,反之亦然。
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, '7
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;
}
}
检测 Esc 键的代码块中缺少一行。您已检测到它,但没有为此目的设置标志。
else if(password[p] == 27 )
{
esc_key = 1; // <-- add this line
break;
}
我正在编写一个重设密码的程序,但在最终(接受新密码)条件成立之前,我在退出程序时遇到了一些问题。我正在使用 conio.h 的 getch() 函数接受输入以将其写入字符串(使用 string.h)
我使用 Borland TurboC++ V4.0 来保持兼容性,因为这是 add-on 旧 MS DOS 程序的代码。如果可以的话,我会使用更新的 ide/compiler。
这是程序的一小段;我省略了 headers 和新的 password/check 密码行,因为它们在 atm 上起作用。我是 C 编程的新手,我知道这可能不是最简单的编写方式,而且有很多冗余。我已经尝试创建一个标志来退出程序,以及一个中断函数,它们都 运行 进入了同样的问题;他们将 '\r' 视为 esc,反之亦然。
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, '7
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;
}
}
检测 Esc 键的代码块中缺少一行。您已检测到它,但没有为此目的设置标志。
else if(password[p] == 27 )
{
esc_key = 1; // <-- add this line
break;
}