From: Jacob Sparre Andersen <sparre@nbi.dk>
Subject: Re: Question from newbie
Date: 16 Feb 2004 12:20:42 +0100
Date: 2004-02-16T12:20:42+01:00 [thread overview]
Message-ID: <ploerzfeyd.fsf@sparre.crs4.it> (raw)
In-Reply-To: 40302ec4$1_2@news.tm.net.my
Cecilia Chew worte:
> with Ada.Text_Io, Ada.Integer_Text_Io;
> use Ada.Text_Io, Ada.Integer_Text_io;
>
> procedure Sort is
> subtype Index is Integer range 1 .. 10;
> subtype Char is Character range 'a' .. 'z';
> type Str is array (Index) of Char;
> procedure Ascending (Left : in out Character; Right : in out
> Character);
>
> procedure Ascending (Left : in out Character; Right : in out
> Character) is
> Left : Character;
> Right : Character;
These two identifiers are already used for the parameters passed to
the procedure. And I don't think you want those variables anyway.
> Temp : Character;
> begin
> if Left > Right then
> Temp := Left;
> Left := Right;
> Right := Temp;
> end if;
> end Ascending;
>
> Num : Index;
> Input : Str;
>
> begin
> Put ("Please enter the number of character : ");
> Get (Num);
I think you might want to insert a call to Ada.Text_IO.Skip_Line here.
Just to eat the newline entered after the number.
> New_Line;
> if Num not in Index then
> Put ("Please enter 1 to 10 characters only!");
> else
> Put ("Please enter" & Integer'Image(Num) & " characters : ");
> for num in Index loop
It is a little bit dangerous to use the name of an existing variable
for the counter in a for loop. I think you actually wanted to write:
for Character_Number in Index'First .. Num loop;
> Get (Input (Num));
And this line was probably intended (as it did) to use the counter
from the for loop:
Get (Input (Character_Number));
> if Input'Length = num then
Input has a fixed length (10), so this will only be true, when you
have entered exactly 10 characters.
>
> for I in 1 .. num loop
> Ascending_char (Input(i), Input(i+1));
> end loop;
>
> while not End_Of_Line loop
> Put (Input(Num));
> end loop;
>
> else
> exit;
> end if;
>
> end loop;
> end if;
> end Sort;
> =============================================================================
> Question:
> 1. As the procedure ascending compiles as a independent unit with the
> other file name, it could sort the two characters correctly.
> However, as I put it together with sort.adb program. Specification
> part that declared the procedure ascending conflict with the
> variable left and right. Where is the mistake?
The mistake is that you have parameters and variables of the same
type.
> 2. Before plug in the procedure ascending, the program successfully
> being compiled. But the program can't allocate the specified space
> as x characters in outputting the result. For an example, user
> specified 2 characters, but the program still allow user to input
> less than or excess 2 characters..Where is the mistakes?
The mistake is that you fix the length of the string to 10 characters
at compile time.
> 3. From my coding, what should I pay more attention to?
You should be careful about using name overloading (until you are a
bit more sure about what you are doing).
You should look a bit more at how arrays are allocated and used in Ada
(your use of 'Length seems like you haven't understood it properly).
Greetings,
Jacob
--
"I've got _plenty_ of common sense!"
"I just choose to ignore it."
prev parent reply other threads:[~2004-02-16 11:20 UTC|newest]
Thread overview: 11+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-02-16 2:43 Question from newbie Cecilia Chew
2004-02-16 10:49 ` Luke Guest
2004-02-16 10:53 ` Preben Randhol
2004-02-16 15:46 ` Jacob Sparre Andersen
2004-02-16 16:14 ` Preben Randhol
2004-02-18 9:52 ` Cecilia Chew
2004-02-18 10:19 ` Jacob Sparre Andersen
2004-02-18 10:24 ` Adrian Hoe
2004-02-18 10:24 ` Adrian Hoe
2004-02-18 10:21 ` Adrian Hoe
2004-02-16 11:20 ` Jacob Sparre Andersen [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox