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-Thread: 103376,3323ee59cb66d2b6 X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!g14g2000cwa.googlegroups.com!not-for-mail From: "jimmaureenrogers@worldnet.att.net" Newsgroups: comp.lang.ada Subject: Re: do while loop help ( newbie) Date: 4 Feb 2006 18:01:32 -0800 Organization: http://groups.google.com Message-ID: <1139104892.388202.308400@g14g2000cwa.googlegroups.com> References: <1139094979.043765.42770@g14g2000cwa.googlegroups.com> NNTP-Posting-Host: 69.170.89.0 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1139104897 13729 127.0.0.1 (5 Feb 2006 02:01:37 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 5 Feb 2006 02:01:37 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; rv:1.7.3) Gecko/20040913 Firefox/0.10,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: g14g2000cwa.googlegroups.com; posting-host=69.170.89.0; posting-account=SqOfxAwAAAAkL81YAPGH1JdBwpUXw9ZG Xref: g2news1.google.com comp.lang.ada:2779 Date: 2006-02-04T18:01:32-08:00 List-Id: isaac2004 wrote: > hi im new to ada and im having trouble with executing a do while loop. > im trying to take an input number called Number and sort it into > ascending and descending order and subtract the two to get a new number > called NewNumber. i want the loop to continue looping until variable > Number is equal to variable NewNumber. if this doesnt make sense here > is my code > > with Ada.Text_IO; > use Ada.Text_IO; > with Ada.Integer_Text_IO; > use Ada.Integer_Text_IO; > procedure Assignment2 is > > ----------------------------------------------------------------------------------------------- > --| Recieves four variables and uses Kaprekar's Function to arrange > them into descending order and > --| and subtract the number in ascending order from it > --| Author: Isaac Levin, > --| Last Modified: January 2006 > > ---------------------------------------------------------------------------- > > subtype Digit is Natural range 0 .. 9; > > D1 : Digit; -- a list of four single digit > numbers > D2 : Digit; > D3 : Digit; > D4 : Digit; > Number : Natural range 0 .. 9999; -- number taken from user > Temp : Natural; > AsNumber : Natural; -- number in ascending order > DsNumber : Natural; -- number in descening order > NewNumber : Natural; -- new number derived from > DsNumber - AsNumber > > > > -- procedure specifications > procedure Order ( > X : in out Natural; > Y : in out Natural); > > -- procedure body > procedure Order ( > X : in out Natural; > Y : in out Natural) is > -- pre: X and Y are assigned values; > -- post: X has the smaller value and Y has the larger value > > Temp : Natural; --copy of number originally in X > > begin -- Order > if X > Y then > Temp := X ; -- store X into temp > X := Y ; -- move old y into x > Y := Temp ; -- move old x from temp to y > end if; > end Order; > begin > > > > loop > begin -- block for exception handler > Put(Item => "Please enter a four digit number."); > New_Line; > Get(Item => Number); > exit; > > > exception > when Constraint_Error => > Put ("The input value or result is out of range."); > Skip_Line; > New_Line; > when Data_Error => > Put (Item => "The input value is not well formed."); > Skip_Line; > New_Line; > end; -- block for exception handler > end loop; > Your input routine will not yield the error checking you seem to think it yields here. You define number as an instance of an anonymous subtype of Natural. You then use the package Ada.Integer_Text_IO for your input. That package knows nothing about your anonymous subtype except that it is a subtype of Inteter. It will not generat a Constraint_Error upon out of range input. You should declare a named subype and then instantiate the generic package Ada.Text_IO.Integer_IO for that subtype. subtype Four_Digits_Max is Natural range 0..9999; package Num_IO is new Ada.Text_Io.Integer_IO(Four_Digits_Max); Number : Four_Digits_Max; > > while Number /= NewNumber loop You should have gotten a warning from the compiler here that the value of NewNumber is used before it is set. You never initialize NewNumber to any known value before it is first evaluated upon entering the loop. > > -- extract digits of number > Temp := Number ; > D1:= Temp rem 10 ; > Temp := Temp / 10 ; > D2:= Temp REM 10; > Temp:= Temp / 10; > D3:= Temp REM 10; > Temp := Temp / 10; > D4 := Temp REM 10; > > -- sort the digits of the number > Order ( > X => D1, > Y => D2); > Order ( > X => D1, > Y => D3); > Order ( > X => D1, > Y => D4); > Order ( > X => D2, > Y => D3); > Order ( > X => D2, > Y => D4); > Order ( > X => D3, > Y => D4); > > -- put ascending and descending digits back into numbers > > AsNumber := (D1 * 1000) + (D2 * 100) + (D3 * 10) + D4; > DsNumber := (D4 * 1000) + (D3 * 100) + (D2 * 10) + D1; > > -- subtract ascending number from descending number > NewNumber := DsNumber - AsNumber; > > > -- display the results > Put( > Item => Dsnumber, > Width => 1); > Put(Item => " - "); > Put( > Item => Asnumber, > Width => 1); > Put(Item => " = "); > Put( > Item => Newnumber, > Width => 1); > > Number := NewNumber; > > end loop ; > > end Assignment2; > > it looks right to me but it just will not start the loop any help would > be great. thanx before hand Jim Rogers