问题描述
我是 C 的初学者,想编写一些从用户那里获取名称的代码,然后用退格键、光标(使用箭头键)重新排列。
在程序中,当用户写下他的名字并按 ESC 时,它必须停止程序并在屏幕上打印名字。
到目前为止,我已经能够使用 ussign getch() 和它们的 ASCII 代码来执行 ESC 和 Backspace 部分,但是当用户尝试重写或重新排列他的名字时,我在使用箭头键移动光标时遇到了问题。>
这是我的代码:
#include<stdio.h>
#include<conio.h>
int main() {
int i,k = 0;
char character;
char samplearray[100];
printf("Enter your name:\n");
while (1) {
character=getch();
if (character != 0x1B && character != 0x8 && character != 0x4B) {
samplearray[k] = character;
k++;
printf("%c",character);
}
else if (character == 0x8) { //Deleting procces in name and it works without any problem.
samplearray[k];
k--;
system("cls");
printf("Enter your name:\n");
for (i = 0; i <= k; i++) {
printf("%c",samplearray[i - 1]);
}
}
else if (character == 0x4B) { //Having a problem on this part.
samplearray[k] = character;
k--;
printf("\b\b",samplearray[k]);
}
else
break;
}
printf("\nYour name is: \n");
for (i = 0; i <= k - 1; i++) {
printf("%c",samplearray[i]);
}
getch();
return 0;
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)