gpt4 book ai didi

c - 如何使我的程序 "forget"成为我在键盘上按下的字母?

转载 作者:行者123 更新时间:2023-11-30 16:51:14 26 4
gpt4 key购买 nike

很抱歉以愚蠢的方式提问,但我不知道如何从技术上表达。

我的项目是打字训练。如果用户写的字母(userWord[i],使用 getch() 从键盘获得)与我给他的单词中对应的字母(filas[maxIndex][i])不同,我想发出声音)。我的代码工作正常,但如果我给他“hello”,他写“heklo”,因为他写得很快,则失败声音将播放 3 次(对于 k、l、o)。我想以某种方式释放键盘的缓冲区(我认为这是“忘记我按下的字母”的技术方式),以便用户开始再次书写他的单词,而不需要“k”,“l”,“o” ' 被存储并作为 userWord[i],我尝试了 fflush 但不起作用。

我正在使用 Visual Studio

printf("\n\n%s\n", filas[maxIndex]); //Here it prints "Alba"

cont = 1;

while (cont == 1) {
i = 0;
while (userWord[i] != ' ') { // the user presses space to make the program know he has completed his word and wants to go to the next one

while (!kbhit()) {}
fflush(stdin); //I believe this should free the keyboard buffer?
userWord[i] = getch();
printf("%c", userWord[i]); //prints each letter the user writes each time the loop is executed, then he presses space to exit


if (userWord[i] != filas[maxIndex][i]) {

PlaySound(TEXT("failSound.wav"), NULL, SND_SYNC); //MY PROBLEM
cont = 1;
Sleep(8);
printf("\n"); //now the user should start writing his word again from the beginning
break;
}
else if (filas[maxIndex][i + 1] == '\0') {
cont = 0;
i++;
break;
}
else { cont = 0; }

i++;
}
if (cont == 0) {
userWord[i] = '\0';

if (length != length2) { //calculated before, but irrelevant to my problem
cont = 1;
Sleep(8);
printf("\n\n%s\n", filas[maxIndex]);
}
}
}

if (cont != 3) {

while (!kbhit()) {}
option = getch(); // if option = '2' the user closes the program and if it equals ' ' the next word if obtained with a function and the loop begins again.
}
}

我的问题是,如果他因为想尽快完成而打字速度很快,他就会犯拼写错误(写“Akba”而不是“Alba”)并且 FailSound 会播放 3 次。我想做一些事情来在用户犯错误时忽略其余的用户输入,但我不知道该怎么做。

最佳答案

您尝试将直接处理键盘输入的低级 conio 输入函数与对过滤后的输入缓冲区进行操作的高级 stdio 输入函数混合在一起,该缓冲区仅在按下回车键后才可用。

这意味着正如@PaulR所解释的,您的fflush(stdin)在这种情况下是无用的,应该避免作为非标准。

Sleep 暂停程序以获取输入值(以毫秒为单位)。就我而言,8毫秒内我什么也做不了!因此循环的初始部分可能会变成:

    while (userWord[i] != ' ') { // the user presses space to make the program know he has completed his word and wants to go to the next one

/*while (!kbhit()) {}
fflush(stdin); // All this is useless */
userWord[i] = getch(); // get next char from keyboard
printf("%c", userWord[i]); //prints each letter the user writes each time the loop is executed, then he presses space to exit


if (userWord[i] != filas[maxIndex][i]) {

PlaySound(TEXT("failSound.wav"), NULL, SND_SYNC); //MY PROBLEM
cont = 1;
// Sleep(8); useless
printf("\n"); //now the user should start writing his word again from the beginning
while (kbhit) { getch(); } // empties keyboard event queue
break;
}

关于c - 如何使我的程序 "forget"成为我在键盘上按下的字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41857324/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com