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

标签 c visual-studio

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

我的项目是打字训练。如果用户写的字母(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/

相关文章:

c - 在 Turbo C 的消息窗口中显示输出

c 宏,检查参数并用 nop 替换

visual-studio - 为什么我的可执行文件上的签名仍然有效?

c# - 没有方法和参数描述的 Visual Studio 2013 智能感知

visual-studio - 导入 Visual Studio TFS 工作区

visual-studio - assembly :以不同颜色打印一条线

c - 将struct的成员声明为c中的指针并使用它

c - 处理 scanf 和 printf 中的空格

c - 模拟流数据

visual-studio - 是否可以更新 Web 部署项目中的 WCF 服务引用