From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,866f55656f224cc8 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2004-02-16 03:25:24 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news.tele.dk!news.tele.dk!small.news.tele.dk!news2.telebyte.nl!news.jgaa.com!news.hacking.dk!pnx.dk!munin.nbi.dk!not-for-mail From: Jacob Sparre Andersen Newsgroups: comp.lang.ada Subject: Re: Question from newbie Date: 16 Feb 2004 12:20:42 +0100 Organization: Munin Sender: sparre@sparre.crs4.it Message-ID: References: <40302ec4$1_2@news.tm.net.my> NNTP-Posting-Host: sparre.crs4.it Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: munin.grove.home 1076930443 30029 156.148.70.170 (16 Feb 2004 11:20:43 GMT) X-Complaints-To: sparre@munin.nbi.dk NNTP-Posting-Date: Mon, 16 Feb 2004 11:20:43 +0000 (UTC) User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2 Xref: archiver1.google.com comp.lang.ada:5594 Date: 2004-02-16T12:20:42+01:00 List-Id: 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."