comp.lang.ada
 help / color / mirror / Atom feed
* Getting valid Integer values !!
@ 2003-10-21 19:11 CheGueVerra
  2003-10-21 20:15 ` Martin Dowie
  0 siblings, 1 reply; 13+ messages in thread
From: CheGueVerra @ 2003-10-21 19:11 UTC (permalink / raw)


Hi guys it's me again,

Well after so much time playing with Ada I think I'm starting to get the
beat of it a special thanks to all those who have helped and will help down
the road...

Now, after testing a bit, this is the procedure I want to use to make sure
that the Integer values entered are valid and are in the range that I expect
them to be, it seems to be working fine, I'll test some more after the post,
but if you see some things that I Might have not thought of please tell me
so

[code snippet]
package Entiers is new Text_io.Integer_io (Num => Integer)

IntData : integer range 1000000..9999999;
ValidKey : Boolean := false;
begin
 put_line("Test Valid Key");

while not ValidKey loop
begin
    Put_line(" Enter Key :");
    Entiers.Get(IntData);
    ValidKey := True;
exception
    When Data_error | Constraint_error =>
        Put_line(" Error reading value,  Integer expected of range ...");
        Skip_line;
end;
end loop;

Put_Line("Valid data entered");
...

Keep in mind that I can't use an exit in a loop so I used the boolean value,
it seems to work but ada has surprised me before ....

TIA

CheGueVerra






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

* Re: Getting valid Integer values !!
  2003-10-21 19:11 CheGueVerra
@ 2003-10-21 20:15 ` Martin Dowie
  2003-10-21 20:27   ` CheGueVerra
  0 siblings, 1 reply; 13+ messages in thread
From: Martin Dowie @ 2003-10-21 20:15 UTC (permalink / raw)


"CheGueVerra" <chegueverra@hotmail.com> wrote in message
news:Fxflb.1900$VQ3.239738@news20.bellglobal.com...
> [code snippet]
> package Entiers is new Text_io.Integer_io (Num => Integer);

Assuming you haven't got a prior "use Ada", for new code you
should call it "Ada.Text_IO" - "Text_IO" is listed as an "Obsolescent"
feature.

> IntData : integer range 1000000..9999999;

This is an "anonymous type" - generally considered bad (esp. for
exported things, not so bad when used localling within a subprogram).
You should declare a 'subtype' for this range.

> ValidKey : Boolean := false;
Not needed - see later

> begin
>  put_line("Test Valid Key");
>
> while not ValidKey loop

   loop

> begin
>     Put_line(" Enter Key :");
>     Entiers.Get(IntData);
>     ValidKey := True;

      exit;  -- this is the canonical method

> exception
>     When Data_error | Constraint_error =>
>         Put_line(" Error reading value,  Integer expected of range ...");
>         Skip_line;
> end;
> end loop;
>
> Put_Line("Valid data entered");

Must be valid to get out of the loop

> Keep in mind that I can't use an exit in a loop so I used the boolean
value,
> it seems to work but ada has surprised me before ....

Why can't you use an exit?





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

* Re: Getting valid Integer values !!
  2003-10-21 20:15 ` Martin Dowie
@ 2003-10-21 20:27   ` CheGueVerra
  2003-10-21 23:04     ` Martin Dowie
  2003-10-22  0:46     ` Jeffrey Carter
  0 siblings, 2 replies; 13+ messages in thread
From: CheGueVerra @ 2003-10-21 20:27 UTC (permalink / raw)


>Assuming you haven't got a prior "use Ada", for new code you
>should call it "Ada.Text_IO" - "Text_IO" is listed as an "Obsolescent"
>feature.

I will change that

>This is an "anonymous type" - generally considered bad (esp. for
>exported things, not so bad when used localling within a subprogram).
>You should declare a 'subtype' for this range.

In my homework I do declare a  subtype for it, I just didn't for the test...

> Why can't you use an exit?
Specified in the homework ... otherwise I would have used it because that
was the first way i tried it.  I just arranged it, so that the boolean value
"controls" the exit...

My guess is that, they want to teach them ( I'm learning ada more than
learning how to program) to control results better, thus not wanting :

Cannot exit a loop while and for with an exit
Cannot use goto
Cannot have global variables

That's why I'm not taking a chance and using the exit in the loop, the
teacher might just see the exit and I'll loose points, and the homework is
already late ;), but almost finished, I'm testing and installing data
cheking

CheGueVerra






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

* RE: Getting valid Integer values !!
@ 2003-10-21 20:34 Beard, Frank Randolph CIV
  2003-10-21 21:34 ` CheGueVerra
  2003-10-21 21:36 ` CheGueVerra
  0 siblings, 2 replies; 13+ messages in thread
From: Beard, Frank Randolph CIV @ 2003-10-21 20:34 UTC (permalink / raw)
  To: CheGueVerra, comp.lang.ada

We have a similar loop that we used on our older
console interfaces.  Using your example, it would be:

...

   type IntData_Type is range 1000000..9999999;
   IntData  : IntData_Type := IntData_Type'first;
   ValidKey : Boolean := false;

   package Entiers is new Text_io.Integer_io (Num => IntData);

begin

   put_line("Test Valid Key");

   loop
      begin
         Put(" Enter Key : ");  --+ Not Put_Line
         Entiers.Get(IntData);
         New_Line(2);
         exit;
      exception
         When Data_error | Constraint_error =>
              New_Line;
              Put_line(" Error reading value,  Integer expected of range ...");
              Skip_line;
      end;
   end loop;

   Put_Line("Valid data entered");
...

I don't follow why you can't use an exit.

Frank

-----Original Message-----
From: CheGueVerra [mailto:chegueverra@hotmail.com]
Sent: Tuesday, October 21, 2003 15:12
To: comp.lang.ada@ada-france.org
Subject: Getting valid Integer values !!


Hi guys it's me again,

Well after so much time playing with Ada I think I'm starting to get the
beat of it a special thanks to all those who have helped and will help down
the road...

Now, after testing a bit, this is the procedure I want to use to make sure
that the Integer values entered are valid and are in the range that I expect
them to be, it seems to be working fine, I'll test some more after the post,
but if you see some things that I Might have not thought of please tell me
so

[code snippet]
package Entiers is new Text_io.Integer_io (Num => Integer)

IntData : integer range 1000000..9999999;
ValidKey : Boolean := false;
begin
 put_line("Test Valid Key");

while not ValidKey loop
begin
    Put_line(" Enter Key :");
    Entiers.Get(IntData);
    ValidKey := True;
exception
    When Data_error | Constraint_error =>
        Put_line(" Error reading value,  Integer expected of range ...");
        Skip_line;
end;
end loop;

Put_Line("Valid data entered");
...

Keep in mind that I can't use an exit in a loop so I used the boolean value,
it seems to work but ada has surprised me before ....

TIA

CheGueVerra



_______________________________________________
comp.lang.ada mailing list
comp.lang.ada@ada-france.org
http://www.ada-france.org/mailman/listinfo/comp.lang.ada



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

* Re: Getting valid Integer values !!
  2003-10-21 20:34 Beard, Frank Randolph CIV
@ 2003-10-21 21:34 ` CheGueVerra
  2003-10-21 21:36 ` CheGueVerra
  1 sibling, 0 replies; 13+ messages in thread
From: CheGueVerra @ 2003-10-21 21:34 UTC (permalink / raw)


>I don't follow why you can't use an exit.

See post reply to  Martin Dowie

CheGueverra







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

* Re: Getting valid Integer values !!
  2003-10-21 20:34 Beard, Frank Randolph CIV
  2003-10-21 21:34 ` CheGueVerra
@ 2003-10-21 21:36 ` CheGueVerra
  1 sibling, 0 replies; 13+ messages in thread
From: CheGueVerra @ 2003-10-21 21:36 UTC (permalink / raw)


>   type IntData_Type is range 1000000..9999999;
>   IntData  : IntData_Type := IntData_Type'first;

would the last line put the value of 1000000 in IntData ?

CheGueVerra






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

* Re: Getting valid Integer values !!
  2003-10-21 20:27   ` CheGueVerra
@ 2003-10-21 23:04     ` Martin Dowie
  2003-10-21 23:35       ` CheGueVerra
  2003-10-22  0:46     ` Jeffrey Carter
  1 sibling, 1 reply; 13+ messages in thread
From: Martin Dowie @ 2003-10-21 23:04 UTC (permalink / raw)


"CheGueVerra" <chegueverra@hotmail.com> wrote in message
news:EEglb.1917$VQ3.243794@news20.bellglobal.com...
> >You should declare a 'subtype' for this range.
>
> In my homework I do declare a  subtype for it, I just didn't for the
test...

Good

Also, for literals you should use '_' for such large numbers, e.g.
1_000_000 not 1000000

> Cannot exit a loop while and for with an exit
> Cannot use goto
> Cannot have global variables

And so another 'programmer invented flag' hits the coding sheets...
...my old JSP teacher would be spinning!

If you are also write a criteque of this, you can tell your prof, that
'exit' is the mechanism of choice in such circumstances for Ada
users (and always has been).

>
> That's why I'm not taking a chance and using the exit in the loop, the
> teacher might just see the exit and I'll loose points, and the homework is
> already late ;), but almost finished, I'm testing and installing data
> cheking

Fair enough





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

* Re: Getting valid Integer values !!
  2003-10-21 23:04     ` Martin Dowie
@ 2003-10-21 23:35       ` CheGueVerra
  2003-10-22  2:49         ` Steve
  0 siblings, 1 reply; 13+ messages in thread
From: CheGueVerra @ 2003-10-21 23:35 UTC (permalink / raw)


> Also, for literals you should use '_' for such large numbers, e.g.
> 1_000_000 not 1000000

my subtype is declared as so

subtype TelNumber is Integer range 1000000..9999999;

now you want me to change this to

subtype TelNumber is Integer range 1_000_000..9_999_999;

exact ? if so why is it better ?


> > Cannot exit a loop while and for with an exit
> > Cannot use goto
> > Cannot have global variables
>
> And so another 'programmer invented flag' hits the coding sheets...
> ...my old JSP teacher would be spinning!
>
> If you are also write a criteque of this, you can tell your prof, that
> 'exit' is the mechanism of choice in such circumstances for Ada
> users (and always has been).

Let's say for now I'm concentrating on finishing, and also I don't really
have the knowledge yet of why that would be the mechanism of choice , so I
couldn't defend my view, except for saying : " Martin Dowie said so on
Comp.lang.ada", I think that isn't a good argument ;).
I wouldn't accept it as a teacher, I think he wouldn't either

CheGueVerra





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

* Re: Getting valid Integer values !!
  2003-10-21 20:27   ` CheGueVerra
  2003-10-21 23:04     ` Martin Dowie
@ 2003-10-22  0:46     ` Jeffrey Carter
  2003-10-22  1:07       ` CheGueVerra
  1 sibling, 1 reply; 13+ messages in thread
From: Jeffrey Carter @ 2003-10-22  0:46 UTC (permalink / raw)


CheGueVerra wrote:

> Cannot exit a loop while and for with an exit

The loop with an exit is not a while or for loop, so this does not seem 
to apply.

-- 
Jeff Carter
"I unclog my nose towards you."
Monty Python & the Holy Grail
11




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

* Re: Getting valid Integer values !!
  2003-10-22  0:46     ` Jeffrey Carter
@ 2003-10-22  1:07       ` CheGueVerra
  0 siblings, 0 replies; 13+ messages in thread
From: CheGueVerra @ 2003-10-22  1:07 UTC (permalink / raw)


I understand what you mean, but I'm being cautious just in case he meant all
kinds of loops ...

CheGueVerra

"Jeffrey Carter" <spam@spam.com> wrote in message
news:1sklb.404$I04.32@newsread4.news.pas.earthlink.net...
> CheGueVerra wrote:
>
> > Cannot exit a loop while and for with an exit
>
> The loop with an exit is not a while or for loop, so this does not seem
> to apply.
>
> --
> Jeff Carter
> "I unclog my nose towards you."
> Monty Python & the Holy Grail
> 11
>





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

* Re: Getting valid Integer values !!
  2003-10-21 23:35       ` CheGueVerra
@ 2003-10-22  2:49         ` Steve
  0 siblings, 0 replies; 13+ messages in thread
From: Steve @ 2003-10-22  2:49 UTC (permalink / raw)


"CheGueVerra" <chegueverra@hotmail.com> wrote in message
news:Jojlb.2002$VQ3.263951@news20.bellglobal.com...
> > Also, for literals you should use '_' for such large numbers, e.g.
> > 1_000_000 not 1000000
>
> my subtype is declared as so
>
> subtype TelNumber is Integer range 1000000..9999999;
>
> now you want me to change this to
>
> subtype TelNumber is Integer range 1_000_000..9_999_999;
>
> exact ? if so why is it better ?
>

Which one is easier to read?

Steve
(The Duck)

[snip]
>
> CheGueVerra
>
>





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

* Re: Getting valid Integer values !!
       [not found] <mailman.161.1066792253.25614.comp.lang.ada@ada-france.org>
@ 2003-10-22  3:46 ` Jeff C,
  2003-10-22 19:27   ` Simon Wright
  0 siblings, 1 reply; 13+ messages in thread
From: Jeff C, @ 2003-10-22  3:46 UTC (permalink / raw)


RE: Getting valid Integer values !!
"Beard, Frank Randolph CIV" <frank.beard@navy.mil> wrote in message
news:mailman.161.1066792253.25614.comp.lang.ada@ada-france.org...
-----Original Message-----
From:   CheGueVerra [mailto:chegueverra@hotmail.com]

>>   type IntData_Type is range 1000000..9999999;
>>   IntData  : IntData_Type := IntData_Type'first;

> would the last line put the value of 1000000 in IntData ?

Yes.

It's always good to initialize your variables, especially
when they have a resticted range.  There is no guarantee the
variable will contain valid data after the space is allocated.
You could get what ever garbage data was left in that memory
location.  If the data happens to be out of range and you
reference it (such as passing the variable into a routine,
etc) CONSTRAINT_ERROR will be raised.

Frank





Funny..I often hear the "Initalize all variables" things but I could not
disagree more! (kind of :)

The problem is that unless you are initializing it to a value that makes
sense in the problem
solution domain (e.g. Initilize array index to 'first for some interesting
loop construct) I believe
you are hurting yourself more than you are helping. The reason is that if
you do  not initialize it and
you get to a segment of the code where you try to use it there are several
good things (one of which
you list as a bad thing) that can happen

1) The compiler warns you that you are reading from a variable before
writing to it.
2) Your cool static analyzer program detects that you are reading from a
variable before writing it.
     (http://www.polyspace.com/product_datasheet/adaverifier.htm)
3) Your human code reviewers detect that you are reading from a variable
before writing it.
4) You are smart and use something like GNATs pragma normalize_scalars or
Initialize_Scalars to force
     the compiler to initialize the variables to invalid values so you can
get (and eliminate) constraint errors during
     test and debug.
(http://www.cs.kuleuven.ac.be/~dirk/papers/ae02cfmu-paper.pdf)

If you initialize everything you do get the added benifit that your program
will probably tend to run
the same (even if it is wrong (e.g. 'first might be a valid number not not
really make sense for how you now intend to use the variable)) ) more often
but I think the losses outweight the benefits because even the same
"wrongness" could go
undetected for years until provided with the magic killer input data.

I do agree that if you can reasonable initialize the variable to the value
that makes sense for its intended use at time of declaration that you have
done a very good thing. But just initializing to some fixed "in range" value
is of marginal use.








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

* Re: Getting valid Integer values !!
  2003-10-22  3:46 ` Getting valid Integer values !! Jeff C,
@ 2003-10-22 19:27   ` Simon Wright
  0 siblings, 0 replies; 13+ messages in thread
From: Simon Wright @ 2003-10-22 19:27 UTC (permalink / raw)


"Jeff C," <nolongersafeto@userealemailsniff.com> writes:

> I do agree that if you can reasonable initialize the variable to the
> value that makes sense for its intended use at time of declaration
> that you have done a very good thing. But just initializing to some
> fixed "in range" value is of marginal use.

I completely agree.

And, if possible, make the variable constant .. (GNAT now warns if you
don't but you could).

-- 
Simon Wright                               100% Ada, no bugs.



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

end of thread, other threads:[~2003-10-22 19:27 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <mailman.161.1066792253.25614.comp.lang.ada@ada-france.org>
2003-10-22  3:46 ` Getting valid Integer values !! Jeff C,
2003-10-22 19:27   ` Simon Wright
2003-10-21 20:34 Beard, Frank Randolph CIV
2003-10-21 21:34 ` CheGueVerra
2003-10-21 21:36 ` CheGueVerra
  -- strict thread matches above, loose matches on Subject: below --
2003-10-21 19:11 CheGueVerra
2003-10-21 20:15 ` Martin Dowie
2003-10-21 20:27   ` CheGueVerra
2003-10-21 23:04     ` Martin Dowie
2003-10-21 23:35       ` CheGueVerra
2003-10-22  2:49         ` Steve
2003-10-22  0:46     ` Jeffrey Carter
2003-10-22  1:07       ` CheGueVerra

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