You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

首次编写Ada程序遇输出异常求助:部分变量输出异常

Fixing Your First Ada Program's Input/Output Issues

Hey there! Let's break down why your Ada program is acting weird and get it working properly. I can spot a few key issues with your code that are causing the wonky output and missing variable displays:

Key Problems in Your Current Code

  • Fixed-length String limitations: You declared Author, Title, and Pages as String := " " — this creates strings with a fixed length of 1. When you use Get(Author), it can only read 1 character from your input, which is why the first output looks messed up. Any extra characters you type get stuck in the input buffer, so subsequent Get calls just grab those leftover characters instead of waiting for your new input.
  • Wrong type for Pages: You're trying to input a number of pages, but Pages is a String instead of a numeric type like Integer. This makes no sense for a count and will cause issues if you ever want to do math with it later.
  • Incomplete code: Your snippet cuts off before reading Pages and has no output statements or closing end; — that alone would cause compilation or runtime errors.

Corrected Program

Here's a revised version that fixes all these issues, using Ada's unbounded strings for flexible text input and proper integer handling for pages:

with Ada.Text_IO; use Ada.Text_IO;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is
   Author : Unbounded_String;
   Title  : Unbounded_String;
   Pages  : Integer;
begin
   Put("Enter Author: ");
   Author := To_Unbounded_String(Get_Line); -- Reads entire line of input

   Put("Enter Title: ");
   Title := To_Unbounded_String(Get_Line);

   Put("Enter number of pages: ");
   Get(Pages); -- Reads integer input
   Skip_Line; -- Clears the newline left in the buffer

   -- Print out the collected info
   New_Line;
   Put_Line("Book Details:");
   Put_Line("Author: " & To_String(Author));
   Put_Line("Title: " & To_String(Title));
   Put("Pages: ");
   Put(Pages, Width => 1);
   New_Line;
end Main;

What Changed & Why

  1. Unbounded strings: Unbounded_String lets you read input of any length (instead of being stuck with a fixed 1-character limit). Get_Line reads an entire line of text until you press Enter, which is exactly what you want for author names and titles.
  2. Integer for Pages: We switched Pages to Integer and used Ada.Integer_Text_IO.Get to properly read numeric input. The Skip_Line call clears the newline character left in the buffer after entering the number, so nothing gets messed up if you add more input later.
  3. Proper output: Added Put_Line and Put statements to display all the collected data clearly. We convert Unbounded_String back to regular String with To_String when printing.

Give this a try — it should let you input full names/titles, read the page count correctly, and output everything as expected!

内容的提问来源于stack exchange,提问作者No longer in college

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 02:23:47