2015-07-11 84 views
1

從Integer輸入輸入後,將自動從012獲取來自String的輸入跳過。我不知道爲什麼?越來越字符串和整數的投入Ada輸入和輸出問題

簡單的Ada代碼:

with ada.Text_IO; use ada.Text_IO; 
with ada.Integer_Text_IO; use ada.Integer_Text_IO; 
procedure Main is 

    inputText: String (1..10); 
    inputNmbr : Integer; 
    StringNatural: Integer; 

begin 

    Put_Line("Enter Integer"); 
    Get(inputNmbr,1); 
    Put_Line("Enter String"); 
    Get_Line(inputText,StringNatural); 
    Put_Line("==================="); 
    Put("Input for Integer: "); 
    Put(inputNmbr,1); 
    Put_Line(""); 
    Put_Line("Input for String: "); 
    Put_Line(inputText(1..StringNatural)); 

end Main; 

輸出:

Enter Integer 
2 
Enter String 
=================== 
Input for Integer: 2 
Input for String: 

[2015-07-11 23:01:00] process terminated successfully, elapsed time: 00.86s 

回答

6

Get不會清除鍵盤緩衝區,所以您在發送到一個回車Get_Line作爲輸入。你可以把一個Skip_LineGet後解決這個問題:

Put_Line("Enter Integer"); 
Get(inputNmbr,1); 
Skip_Line; -- add this 
Put_Line("Enter String"); 

Skip_Line documentation

Skip_Line是輸入程序,將導致輸入跳到下一行。這對於從輸入緩衝區中刪除回車很有用。 Skip_Line應在任何調用Get過程之後執行。它也可用於使程序暫停並等待輸入回車。

參見:Clearing the keyboard buffer in Ada

+0

這是偉大的!非常感謝您的幫助。 – user2689972

+1

請注意,關於何時調用Skip_Line的建議並不完全合適,因爲您可能有案例,您想從同一行讀取多個對象。 –

+0

@ipavl你能幫我在這裏:http://stackoverflow.com/questions/31410589/ada-getting-string-from-text-file-and-store-in-array – user2689972