2012-12-07 189 views
1

我寫了一個清除鍵盤緩衝區的函數,因爲我覺得在get後有剩餘的輸入,但它只是一直輸入。在Ada中清除鍵盤緩衝區

這裏是GET(字符串(1..10)

--getString10-- 
procedure getString10(s : in out string10) is 
    Last: Integer; 
begin 
    s := (others => ' '); 
    Get_Line(S, Last); 
end getString10; 

,這裏是我做了刷新,幾乎從維基複製上清除鍵盤緩衝區

--flush-- 
procedure flush is 
    char : character; 
    more : boolean; 
begin 
    loop 
     get_immediate(char, more); 
     exit when not more; 
    end loop; 
end flush;  

每當刷新被調用,無論我輸入什麼,直到我退出程序屏幕上輸出。

此外,我的getString10函數並不總是等待用戶輸入。例如,如果我有

put("Enter a name: "); 
getString10(name); 
put("Enter a number: "); 
getString10(number); 

輸出將是

Enter a name: Enter a number: exampleinput 

我對蚋編程Studio中使用2005年的Ada。

與整個主要更新:

with Ada.Text_IO, Ada.Integer_Text_IO, BinarySearchTree; 
use Ada.Text_IO, Ada.Integer_Text_IO; 

procedure lab4 is 

    subtype String10 is String(1..10); 

    -- function "<"-- 
    function "<"(TheKey: in String10; ARecord: in String10) return Boolean is 
    begin 
     for i in integer range 1..10 loop 
     if TheKey(i) <= ARecord(i) then 
      return true; 
     else 
      return false; 
     end if; 
       end loop; 

     return false; 

    end "<"; 

    -- function ">"-- 
    function ">"(TheKey: in String10; ARecord: in String10) return Boolean is 
    begin 
     for i in integer range 1..10 loop 
     if TheKey(i) >= ARecord(i) then 
      return true; 
     else 
      return false; 
     end if; 
     end loop; 

     return false; 
    end ">"; 

    -- function "="-- 
    function "="(TheKey: in String10; ARecord: in String10) return Boolean is 
    begin 
     for i in integer range 1..10 loop 
     if TheKey(i) /= ARecord(i) then 
      return false; 
      end if; 
     end loop; 

     return true; 
    end "="; 

    --getString10-- 
    procedure getString10(s : in out string10) is 
     Last: Integer; 
    begin 
      s := (others => ' '); 
     Get_Line(S, Last); 
    end getString10; 

    --flush-- 
    procedure flush is 
     char : character; 
     more : boolean; 
    begin 
     loop 
     get_immediate(char, more); 
     exit when not more; 
     end loop; 
    end flush;  

    package BST is new BinarySearchTree(String10, String10, "<", ">", "="); 

    Root, found : BST.BinarySearchTreePoint; 
    choice : integer; 
    nameTemp, phoneTemp : String10; 
begin 
    BST.setRoot(Root); 

    loop 
     new_line; 
     put_line("Options:"); 
     put_line("1 - Insert a record"); 
     put_line("2 - Find a person iteratively and print their phone number"); 
     put_line("3 - Find a person recursively and print their phone number"); 
     put_line("4 - Traverse the tree from a person to a person"); 
     put_line("0 - Quit program"); 
     put("Choose an option: "); 
     get(choice); put(choice, 0); new_line; 

     case choice is 
     --case 1 
     when 1 => 
      put("Enter the name: "); 
      get(nameTemp); put(nameTemp); new_line; 

      put("Enter the phone number : "); 
      get(phoneTemp); put(phoneTemp); new_line; 

      BST.InsertBinarySearchTree(root, nameTemp, phoneTemp); 
     --case 2 
     when 2 => 
      put("Enter the name of the person to find: "); 
      get(nameTemp); put(nameTemp); 
      BST.FindCustomerIterative(root, nameTemp, found); 

      if BST.isNull(found) then 
       new_line; 
       put("Customer not found!"); 
      else 
       new_line; 
       put("The phone number is: "); 
       put(BST.CustomerPhone(found)); 
      end if; 
     --case 3 
     when 3 => 
      put("Enter the name of the person to find: "); 
      get(nameTemp); put(nameTemp); 
      BST.FindCustomerRecursive(root, nameTemp, found); 

      if BST.isNull(found) then 
       new_line; 
       put_line("Customer not found!"); 
      else 
       new_line; 
       put("The phone number is: "); 
       put(BST.CustomerPhone(found)); 
      end if; 
      new_line; 

     --case 4 
     when 4 => 
      put("Enter of the name of the person to start traversal at: "); 
      get(nameTemp); put(nameTemp); 
      BST.FindCustomerRecursive(root, nameTemp, found); 

      put("Enter then name of the person to stop traversal at: "); 
      get(phoneTemp); put(phoneTemp); --using phoneTemp for a name here 

      BST.FindCustomerRecursive(Root, nameTemp, found); 
      while BST.isNull(found) /= true loop 
       put_line("Name = " & BST.CustomerName(found)); 
       BST.setNode(found, BST.InOrderSuccessor(found)); 
      end loop; 

     --case 0 
     when 0 => 
      exit; 
     --others 
     when others => 
      put_line("Invalid choice!"); new_line; 
     end case; 

    end loop; 

end lab4; 

我換了所有的getString10的()s的get()方法是因爲我試圖調試螺紋二叉搜索樹。我正在使用一個輸入文件,所以現在很好,我只能弄清楚爲什麼其他方法不起作用。 nameTemp和phoneTemp上的所有get調用應該是getString10()調用。

回答

1

您可以使用get_line()函數,該函數自動清除鍵盤緩衝區。讀取整個主後

with ADA.TEXT_IO;    use ADA.TEXT_IO; 
with ADA.TEXT_IO.UNBOUNDED_IO; use ADA.TEXT_IO.UNBOUNDED_IO; 
with ADA.STRINGS.UNBOUNDED;  use ADA.STRINGS.UNBOUNDED; 

procedure MAIN is 

    type STRING_10 is new STRING(1..10); 

    procedure GET_STRING_10(S : in out STRING_10) is 
     BUF : UNBOUNDED_STRING; 
    begin 
     BUF := GET_LINE; 

     for I in STRING_10'range loop 
     if I <= LENGTH(BUF) then 
      S(I) := ELEMENT(BUF, I); 
     else 
      S(I) := ' '; 
     end if; 
     end loop; 
    end GET_STRING_10; 

    S : STRING_10; 

begin 

    GET_STRING_10(S); 
    PUT_LINE(STRING(S)); 

end MAIN; 

編輯: 你應該一個SKIP_LINE;GET(CHOICE);插入。然後,您可以在每種情況下將GET替換爲GETSTRING10

一般來說,get必須總是後跟skip_line。不需要使用get_line來完成此操作,因此您不必修改gettring10過程。

+0

我的getString10使用get_line()函數,在我調用它之後,它跳過了下一個getString10,這就是爲什麼我認爲緩衝區中仍然存在返回字符。你確定get_line清除緩衝區嗎? – user1279914

+0

@ user1279914我不確定任何東西:)我認爲'GET_LINE'清除緩衝區。但是,我注意到'GET_LINE'的許多版本都存在[here](http://tinyurl.com/crtn2ez)和[here](http://tinyurl.com/d3dxuek)。他們可能會有一些稍微不同的行爲,導致你很大的麻煩。在上面的代碼中,我使用了_function_,它使用_no args_並且來自_unbounded io_包。這似乎是清除緩衝區。如果你真的需要使用_procedure_,你能不能請張貼更多的代碼(特別是進口),以便我能夠確定你使用的是哪一個? – tvuillemin

+0

我更新了原帖。謝謝。 – user1279914