【发布时间】:2014-01-24 06:16:55
【问题描述】:
我终于掌握了 C 语言中的箭头键。我发现了如何让 C 检测它们并实际编写了一个程序。
问题是……程序有问题。我不知道我做错了什么。
代码:
#include <stdio.h>
main()
{
char menitem[3][32], key, key2;
int i = 0;
strcpy(menitem[0], "Option 1 [X]");
strcpy(menitem[1], "Option 2 [ ]");
strcpy(menitem[2], "Option 3 [ ]");
start:
system("cls");
printf("%s\n%s\n%s", menitem[0], menitem[1], menitem[2]);
key = getch();
key2 = 0;
if(key = 0xE0)
key2 = getch();
ret:
if(i == 0)
{
switch(key2)
{
case 80:
strcat(menitem[0], "\b\b ]");
i++;
strcat(menitem[i], "\b\bX]");
goto start;
default: goto ret;
}
}
else if(i == 2)
{
switch(key2)
{
case 72:
strcat(menitem[2], "\b\b ]");
i--;
strcat(menitem[i], "\b\bX]");
goto start;
default: goto ret;
}
}
else
{
switch(key2)
{
case 80:
strcat(menitem[i], "\b\b ]");
i++;
strcat(menitem[i], "\b\bX]");
goto start;
case 72:
strcat(menitem[i], "\b\b ]");
i--;
strcat(menitem[i], "\b\bX]");
goto start;
default: goto ret;
}
}
}
问题来了:
当我从选项 2 上升时,选项 3 变成“X]”。知道为什么吗?
尝试编译它并继续使用箭头键。走着瞧吧。将不胜感激任何帮助!
【问题讨论】:
-
你的问题到底是什么???
-
这是什么平台?箭头键本身不是 C 的一部分,而是宿主环境。
-
“运行我的代码以查看问题”不是您应该在 SO 问题中提出的问题。此外,修复缩进(TAB char 在 SO 有问题)。打开编译器的警告并修复它们。
-
关于使用 goto 来实现循环......当你为自己编写代码时,任何事情都会发生,但请注意,几乎所有程序员都会回避任何代码那个。
-
if(key = 0xE0)=>if(key == 0xE0)
标签: c text arrow-keys