My simple Ada program cannot execute the next command, but during the build and run process it doesn't matter

Viewed 88

sorry I wanted to ask about the Ada language. First, I want to explain that I first learned the Ada language and me is trying to make a simple program like the one I attached -> https://pastebin.com/Z1CWUSQv

with Ada.Text_IO;
use Ada.Text_IO;

procedure Main is
Name : STRING (1..50);

begin
Put("Hallo, whats your name?");
Get(Name);

if (Name = "William") then
   Put_Line ("Hai william");
elsif (Name = "Erick") then
   Put_Line ("Hai erick");
else
   Put_Line ("i dont know, who are you!");
   end if;
end Main;

https://i.stack.imgur.com/1fTLR.png So, my questions are: When I finished writing the code, I did the build and it was a success, after that I did running the program and the output looks like this:

  • Hello, what's your name? (This is ouput)
  • Erick (This is my input)

when finished I pressed the Enter key and after that nothing happened like that the program doesn't execute the next command (I made an if / else command)

I am using the Gnat Studio IDE or online compiler --> https://www.tutorialspoint.com/compile_ada_online.php, so what is the problem with my program? Are there any tools that I need to install and so on? please know I am very difficult to find references on this issue. I hope all of you reading this can help me. (Sorry my English is not good)

~ Thank you

1 Answers

Strings are difficult for newcomers to Ada to get right. In this case, the problem is that Name is 50 characters long, so

  • when you call Get, it wants to read 50 characters, and it will carry on reading until it gets 50 characters (or reaches the end of the input). This is why your program appeared to get stuck.
  • "William" is not 50 characters long, so it cannot possibly be equal to Name.

You could use the procedure Get_Line (ARM A.10.7(18)), something like this:

with Ada.Text_IO;
use Ada.Text_IO;

procedure Main is
   Name : STRING (1..50);
   Last : Natural;
begin
   Put("Hallo, whats your name?");
   Get_Line (Name, Last);

   if (Name (1 .. Last) = "William") then
      Put_Line ("Hai william");
   elsif (Name (1 .. Last) = "Erick") then
      Put_Line ("Hai erick");
   else
      Put_Line ("i dont know, who are you!");
   end if;
end Main;
Related