【问题标题】:Ada Input and Output issueAda 输入和输出问题
【发布时间】:2015-07-11 15:11:31
【问题描述】:

从 Integer 输入输入后,它会自动跳过从 String 获取输入。不知道为什么?

获取字符串和整数输入的简单 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

【问题讨论】:

    标签: input output ada gnat


    【解决方案1】:

    Get 不会清除键盘缓冲区,因此您将回车发送到Get_Line 作为输入。您可以在Get 之后添加Skip_Line 来解决此问题:

    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

    【讨论】:

    • 这太棒了!非常感谢您的帮助。
    • 请注意,关于何时调用 Skip_Line 的建议并不完全合适,因为您可能会遇到需要从同一行读取多个对象的情况。
    • @ipavl 你能帮帮我吗:stackoverflow.com/questions/31410589/…
    猜你喜欢
    • 1970-01-01
    • 2012-06-15
    • 2020-06-12
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 2020-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多