comp.lang.ada
 help / color / mirror / Atom feed
* Re: raised Constraint_Error?
  1995-04-03  0:00 ` Michael Feldman
@ 1995-04-03  0:00   ` Jeff Winchester
  0 siblings, 0 replies; 4+ messages in thread
From: Jeff Winchester @ 1995-04-03  0:00 UTC (permalink / raw)


On 3 Apr 1995, Michael Feldman wrote:


<extra responses deleted>

Sadly enough, almost all they're teaching is the actual code itself--very 
little help is given with the compiler or debugging of the code...  This 
detective had been up for about 25 hours straight working on his 
constraint_error when he decided to post!  Thanks anyhow.


/--------------------------------------------------------\
| Jeff Winchester | PD Playhouse BBS | FidoNet 1:3635/40 |
|   [Team OS/2]   |  (813) 422-7391  | RGSNet 50:270/504 |
|------------------------------------| EX-Net 80:200/10  |
|  Catering to the support of OS/2   | ITCNet 88:881/518 |
|   and the Renegade BBS software    |                   |
|--------------------------------------------------------|
| CompuServe: 73404,2160      America Online: JeffW13680 |
|        Internet: winchest@babbage.csee.usf.edu         |
\--------------------------------------------------------/





^ permalink raw reply	[flat|nested] 4+ messages in thread

* raised Constraint_Error?
@ 1995-04-03  0:00 Jeff Winchester
  1995-04-03  0:00 ` Michael Feldman
  1995-04-04  0:00 ` Kevin F. Quinn
  0 siblings, 2 replies; 4+ messages in thread
From: Jeff Winchester @ 1995-04-03  0:00 UTC (permalink / raw)


Could anyone tell me why the following code would cause a constraint 
error?  I've been pulling out hairs for a couple of days now and am 
obviously overlooking something simple.  Thanks.


     current := head;
     while current /= null loop
	if (current.name (current.nl)) = (sname (snl)) then
        current := current.next;
	   if (current.class (current.cl)) = (sclass (scl)) then
           current := current.next;
             if (current.number = snumber) then
                current := current.next;
                pay := current.cost;
                text_io.put ("The test costs $");
                flt_io.put (item => current.cost, aft => 2, exp => 0);
                text_io.put ("Please enter your payment --> $");
                flt_io.get (pay);
                text_io.new_line;
                   if pay < cost then
                     pcost := cost - pay;
                     text_io.put ("You didn't pay enough.  You still owe $");
                     flt_io.put (item => pcost, aft => 2, exp => 0);
                   elsif cost < pay then
                     pcost := pay - cost;
                     text_io.put ("Thank you.  Your change is $");
                     flt_io.put (item => pcost, aft => 2, exp => 0);
                   else current := current.next;
                   end if;
             else current := current.next;
             end if;
	   else current := current.next;
	   end if;
	else current := current.next;
	end if;
     exit when current = null;
     end loop;
   end price;

-------------------- or this one. . . .

        if lthis = 1 then
           text_io.new_line;
           text_io.put ("Please enter the class name --> ");
           text_io.get_line (lclass, lcl);
           text_io.skip_line;
                lcurrent := head;
                while lcurrent /= null loop
                   if (lcurrent.class (lcurrent.cl)) = (lclass (lcl)) then
                     text_io.put_line (lcurrent.name (1..lcurrent.nl));
                     text_io.put_line (lcurrent.class (1..lcurrent.cl));
                     int_io.put (lcurrent.number);
                     text_io.new_line;
                     int_io.put (lcurrent.quantity);
                     text_io.new_line;
                     flt_io.put (lcurrent.cost);
                     text_io.new_line;
                     int_io.put (lcurrent.pages);
                     text_io.new_line;
                     text_io.new_line;
                     lcurrent := lcurrent.next;
                   else lcurrent := lcurrent.next;
                   end if;
                end loop;

Very simple variable definitions--either string (1..35), integer, or 
float.  That's not the problem, however.  The linked lists seem to cause 
constrainst errors when searched, and I'm not sure why.  any help would 
be appreciated--this is a last ditch effort for a programming class.  Thanks.


/--------------------------------------------------------\
| Jeff Winchester | PD Playhouse BBS | FidoNet 1:3635/40 |
|   [Team OS/2]   |  (813) 422-7391  | RGSNet 50:270/504 |
|------------------------------------| EX-Net 80:200/10  |
|  Catering to the support of OS/2   | ITCNet 88:881/518 |
|   and the Renegade BBS software    |                   |
|--------------------------------------------------------|
| CompuServe: 73404,2160      America Online: JeffW13680 |
|        Internet: winchest@babbage.csee.usf.edu         |
\--------------------------------------------------------/





^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: raised Constraint_Error?
  1995-04-03  0:00 raised Constraint_Error? Jeff Winchester
@ 1995-04-03  0:00 ` Michael Feldman
  1995-04-03  0:00   ` Jeff Winchester
  1995-04-04  0:00 ` Kevin F. Quinn
  1 sibling, 1 reply; 4+ messages in thread
From: Michael Feldman @ 1995-04-03  0:00 UTC (permalink / raw)


In article <Pine.SUN.3.91.950403111430.12054B-100000@babbage>,
Jeff Winchester  <winchest@csee.usf.edu> wrote:
>Could anyone tell me why the following code would cause a constraint 
>error?  I've been pulling out hairs for a couple of days now and am 
>obviously overlooking something simple.  Thanks.
>
Hmmm. I think it's going a bit far to ask net readers to
dig in so deep into your code. You need to do this sort of
detective work yourself, especially in a course. OTOH:

Some hints:

Your compiler ought to have a debugger, or at least give you a stack
dump when the Constraint_Error is propagated. This should tell you
in just which line the exception was raised, and where it propagated.

Constraint_Error in linked-list processing is most often caused by the
list-traversal loop walking right off the end of the list - a kind
of off-by-one problem.  When you reach the end of the list, and then
try to dereference a null pointer, Ada raises Constraint_Error.

If all else fails, add a Put_Line or two with a message that displays
each time you go around the loop.

Aren't they teaching debugging techniques at USF?

Mike Feldman
------------------------------------------------------------------------
Michael B. Feldman -  chair, SIGAda Education Working Group
Professor, Dept. of Electrical Engineering and Computer Science
The George Washington University -  Washington, DC 20052 USA
202-994-5919 (voice) - 202-994-0227 (fax) - mfeldman@seas.gwu.edu (Internet)
------------------------------------------------------------------------
One, two, three ways an underdog: Ada fan, Mac fan, Old Liberal Democrat
------------------------------------------------------------------------
Ada on the WWW: http://lglwww.epfl.ch/Ada/ or http://info.acm.org/sigada/
------------------------------------------------------------------------




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: raised Constraint_Error?
  1995-04-03  0:00 raised Constraint_Error? Jeff Winchester
  1995-04-03  0:00 ` Michael Feldman
@ 1995-04-04  0:00 ` Kevin F. Quinn
  1 sibling, 0 replies; 4+ messages in thread
From: Kevin F. Quinn @ 1995-04-04  0:00 UTC (permalink / raw)


In article <Pine.SUN.3.91.950403111430.12054B-100000@babbage>,
          winchest@csee.usf.edu (Jeff Winchester) wrote:

> Could anyone tell me why the following code would cause a constraint 
> error?  I've been pulling out hairs for a couple of days now and am 
> obviously overlooking something simple.  Thanks.

Indeed you are - dereferencing null pointers you naughty boy :)

>      current := head;
>      while current /= null loop

OK, assume here that current.next = NULL (which it will be, eventually,
assuming the list isn't looped)...

> 	if (current.name (current.nl)) = (sname (snl)) then
>         current := current.next;

So now current=NULL

> 	   if (current.class (current.cl)) = (sclass (scl)) then

Crash and burn.

>            current := current.next;
>              if (current.number = snumber) then
>                 current := current.next;
>                 pay := current.cost;

Ditto here, of course (if current.next.next on entry to loop is NULL).

>        [...deletia...]
>                      text_io.put ("Thank you.  Your change is $");
>                      flt_io.put (item => pcost, aft => 2, exp => 0);
>                    else current := current.next;  \
>                    end if;                         | Phew!  Scratch
>              else current := current.next;         | this lot for a
>              end if;                               \ start!  Put the
> 	   else current := current.next;             / c := c.next
> 	   end if;                                   | outside all the ifs.
> 	else current := current.next;                |
> 	end if;                                     /
>      exit when current = null;   <--- This is redundant, BTW
>      end loop;
>    end price;

I'm not doing both for you :)

A tip for the above, I would suggest flattening stuff a bit.
Inside the loop, it's best only to consider current.*, and not
any deeper than that.  Only traverse the list at the end of the
loop, i.e. immediately before the check "current /= NULL":

   while current /= NULL loop
     [Lots of wibble about current.<whatever> here - don't
      change current]
     current := current.next;
   end loop;


BTW Why not ask your course lecturer?  That's what they're for...

-- 
Kevin F. Quinn           * "No-one ever made any money out of good
kevq@banana.demon.co.uk  *  looks and charm."
kevq@cix.compulink.co.uk * "You obviously haven't met Lady Hamilton..."
Compu$erve: 100025,1525  *                               Blackadder III




^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~1995-04-04  0:00 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1995-04-03  0:00 raised Constraint_Error? Jeff Winchester
1995-04-03  0:00 ` Michael Feldman
1995-04-03  0:00   ` Jeff Winchester
1995-04-04  0:00 ` Kevin F. Quinn

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox