2017-01-25 20 views
0

對不起,這是愚蠢的方式,但我不知道怎麼說技術。如何讓我的程序「忘記」我在鍵盤上按下的字母?

我的程序是打字訓練。如果用戶寫的字母(userWord [i],從鍵盤上用getch())獲得的字母與我給他的單詞中的相應字母不相同,我想發出聲音(filas [maxIndex] [i] )。我的代碼工作正常,除了如果例如我給他「你好」,並且他寫道「heklo」,因爲他寫得非常快,失敗聲音將播放3次(對於k,l,o)。我想以某種方式釋放鍵盤的緩衝區(我認爲這是說「忘記我按下的字母」的技術方式),用戶不用'k','l','o '被存儲並作爲userWord [我],我試過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」,而不是「阿爾巴」)和FailSound打3次。我想在做出錯誤時忽略用戶輸入的其餘部分,但我不知道如何去做。

+0

_Note_:我可能是錯在這裏(我目前沒有一個_Win_箱進行測試),但我想你應該添加一個'睡眠(1); 'while(!kbhit())'循環中的語句,否則當用戶沒有按任何東西時,程序會殺死你的一個處理器。 – CristiFati

+0

不幸的是,我無法將描述中的問題鏈接到提供的代碼。 1:'break'語句從__outer__循環中取出(與註釋中的內容相反)。另外'i'不會增加,所以代碼在數組的相同字符上運行。另外,如果用戶緩慢地按錯了序列,您又希望它的行爲如何? _注意:不是「試圖忘記」(無效?)字符,你可以自己丟棄它們(不要將它們放在'userWord'中) – CristiFati

+0

我修改了代碼,希望現在你可以理解它,對於以前缺乏信息。現在,我怎麼能丟棄自己的字符?我不會提出一個想法,因爲他們就像被記住......我不知道,他們不是用戶詞的一部分,直到我到達getch(),但是這會讓他們...... @CristiFati –

回答

0

你試圖混合直接處理鍵盤輸入的低級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; 
     } 
+0

這對我有效,謝謝。但是,我在另一個網站上讀到,如果我包含** **,我可以在我的代碼中寫入'bdos(0xC,0,0);'這將清除BIOS鍵盤緩衝區。你推薦使用這個嗎?由於我非常想編程,所以我不知道錯誤使用是否會「危險」@SergeBallesta –

+0

唷......'bdos(0xc,0,0)'! Haden't幾十年來都沒有看到原始的BIOS調用......我不知道它在Windows控制檯應用程序中是否受支持,因此您可以試試它。但我會避免使用任何低級別的任何東西...... –

相關問題