comp.lang.ada
 help / color / mirror / Atom feed
* Re: Ada vs C++ for numeric IO (integers)
  1996-07-22  0:00 Ada vs C++ for numeric IO (integers) Nasser Abbasi
@ 1996-07-22  0:00 ` Tom Zagotta
  1996-07-22  0:00   ` Robert Dewar
  1996-07-23  0:00   ` Nasser Abbasi
  1996-07-24  0:00 ` Keith Thompson
  1 sibling, 2 replies; 5+ messages in thread
From: Tom Zagotta @ 1996-07-22  0:00 UTC (permalink / raw)



Nasser Abbasi wrote:
> 
> hi,
> 
> I was playing with integer IO to see how C++ and Ada handle it.
> I noticed couple of things, I show the Ada and the C++ examples,
> and have a question on each language.
> 

  { snip }

> --
> Nasser Abbasi. C/C++/Ada Solaris. GeneAssist - A client/server application
> for Nucleic acid and protein sequence search and analysis.
> Perkin Elmer - Applied BioSystem division. email:  nasser@apldbio.com
> MSEE(control), MSCS, MSCE, FM (Fide Chess Master).

None of the languages I've ever used have provided "bullet-proof" I/O
functions that get user input; I have always written them myself.  An example 
would be a function with the following signature:

  int ReadInteger (const char* Prompt, int Min, int Max)
  {
    // your code here
  }

This function might write out the prompt, read a number from the user, make sure
it's in range, and return the integer when a valid number is typed.

These functions are typically not provided by compiler libraries because of the
uniqueness of your combination of user interface, error handling strategy, etc.

Hope that helps!
Tom Zagotta




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

* Re: Ada vs C++ for numeric IO (integers)
  1996-07-22  0:00 ` Tom Zagotta
@ 1996-07-22  0:00   ` Robert Dewar
  1996-07-23  0:00   ` Nasser Abbasi
  1 sibling, 0 replies; 5+ messages in thread
From: Robert Dewar @ 1996-07-22  0:00 UTC (permalink / raw)



Tom Zagotta said:

None of the languages I've ever used have provided "bullet-proof" I/O
functions that get user input; I have always written them myself.  An example
would be a function with the following signature:

  int ReadInteger (const char* Prompt, int Min, int Max)
  {
    // your code here
  }

This function might write out the prompt, read a number from the user, make sure
it's in range, and return the integer when a valid number is typed.



I don't see how that is different from what you would get in Ada from:

  type Input_Integer is range Min .. Max;
  package My_Input is new Ada.Text_IO.Integer_IO (Input_Integer);
  ...
  Put_Line (Prompt);
  My_Input.Get (My_Input_Integer); -- My_Input_Integer is type Input_Integer

then if you want to trap invalid input, catch the exception Data_Error.





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

* Ada vs C++ for numeric IO (integers)
@ 1996-07-22  0:00 Nasser Abbasi
  1996-07-22  0:00 ` Tom Zagotta
  1996-07-24  0:00 ` Keith Thompson
  0 siblings, 2 replies; 5+ messages in thread
From: Nasser Abbasi @ 1996-07-22  0:00 UTC (permalink / raw)




hi,

I was playing with integer IO to see how C++ and Ada handle it.
I noticed couple of things, I show the Ada and the C++ examples, 
and have a question on each language. 

I post this in one article just to show the difference between the 2 cases,
and may be have a discussion on how each langauge approaches this area.

Ada case:
========
I noticed that constraint error is not raised for short_integer if 
I input a value outside short_integer range in an IO operation for 
example. I check the LRM, and it does say that in 3.5.4.20 .

But this kind'a made me think this is not too nice, as I expected 
an exception to be raised from Ada.

Any thought on this, why does not a derived type inherit a new
constraint range for its new type range? Now this means one have 
to figure a way to check for this case themselves instead of letting the
run-time do it.

for example:

with Ada.Text_Io; use Ada.Text_Io;
procedure Main is
 package Short_Io is new Ada.Text_Io.Integer_Io(Short_Integer); use Short_Io;
 I: Short_Integer;

begin
   loop
      Put("i ? ");
      Get(I);
      Put("you typed: "); Put(I);
      New_line;
   end loop;
end Main;

and runnning the above program:


i ? 32768   <--- I typed this expecting to get constraint error
you typed: -32768  <-- it did not! , it wrapped around instead !
i ? 2147483648 <-- now, typed in value outside range of BASE type

raised ADA.IO_EXCEPTIONS.DATA_ERROR <--- now we get the constraint error


C++ case
========

C++ does not raise exceptions here, at least not with the
compiler I am using now.

Behaviour is The same with Ada for values outside range of 
-32768..32767, actually the same for range (-2147483647-1) ..2147483647 ,
i.e. no error is detected for short int IO, if one type a value larger
than 32767 it wrappes around.  But in C++, If I type in a value
larger than 2147483647, say 2147483648, it still wrappes (moves -1 
to my variable), and the cin stream is still in good state, while in 
Ada a constraint error is raised here.

Example:


#include <iostream.h>
#include <limits.h>

main()
{

short int i;

cin.unsetf(ios::skipws);

for(;;)
  {
   cout<<"Enter i and hit return: "<<flush;

   for(;;)
   {
     cin>>i;
     if( cin )
     {
        cin.ignore(INT_MAX,'\n');
        cout<<endl<<"input was good. you typed "<<i<<endl;
        break;
     }
     cin.clear();   // clear error state
     cin.ignore(INT_MAX,'\n');
     cout<<"error in read, try again"<<flush;
   }
  }
   return 0;
}


running the above program:

Enter i and hit return: 32768   <--- larger than SHRT_MAX
input was good. you typed -32768  <--- wrapped around

Enter i and hit return: 2147483647 

input was good. you typed -1

Enter i and hit return: 2147483648 <--- larger than INT_MAX
input was good. you typed 0 <--- wrapped around

Enter i and hit return: 2147483649 
input was good. you typed 1

Enter i and hit return: 999999999999999999999999999999999999999999999999
input was good. you typed -1  <--- oh well, cin still is good !
                                   but I think something went wrong, -1
                                   looks weired value

Well, I guess I don't understand integer IO much in C++, I could not
figure a way how to detect overflow input in this example. Does any
one know the best way to handle integer IO in C++ so that if someone
types in a value larger than MAX_INT for example, it will be
detected? I did check for cin.bad() and cin.fail() also, but those
passed the test. 


thanks,
Nasser
-- 
Nasser Abbasi. C/C++/Ada Solaris. GeneAssist - A client/server application 
for Nucleic acid and protein sequence search and analysis. 
Perkin Elmer - Applied BioSystem division. email:  nasser@apldbio.com   
MSEE(control), MSCS, MSCE, FM (Fide Chess Master).







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

* Re: Ada vs C++ for numeric IO (integers)
  1996-07-22  0:00 ` Tom Zagotta
  1996-07-22  0:00   ` Robert Dewar
@ 1996-07-23  0:00   ` Nasser Abbasi
  1 sibling, 0 replies; 5+ messages in thread
From: Nasser Abbasi @ 1996-07-23  0:00 UTC (permalink / raw)




   From: Tom Zagotta <tzagotta@mich.com>


   Nasser Abbasi wrote:
   > 
   > hi,
   > 
   > I was playing with integer IO to see how C++ and Ada handle it.
   > I noticed couple of things, I show the Ada and the C++ examples,
   > and have a question on each language.
   > 


   None of the languages I've ever used have provided "bullet-proof" I/O
   functions that get user input; I have always written them myself.  An example 
   would be a function with the following signature:

     int ReadInteger (const char* Prompt, int Min, int Max)
     {
       // your code here
     }

   This function might write out the prompt, read a number from the user, make sure
   it's in range, and return the integer when a valid number is typed.

   These functions are typically not provided by compiler libraries because of the
   uniqueness of your combination of user interface, error handling strategy, etc.

   Hope that helps!
   Tom Zagotta


Thanks Tom, but that does not really help :)

How are you going to know what the user typed in in the first
place if wrapping will occure for example? 

Show me the code that will read in a value for the range for say short int,
and how are you going to detect that the user did not type a value
over that range as their input ?

code, please show me code :)

Nasser

-- 
Nasser Abbasi. C/C++/Ada Solaris. GeneAssist - A client/server application 
for Nucleic acid and protein sequence search and analysis. 
Perkin Elmer - Applied BioSystem division. email:  nasser@apldbio.com   
MSEE(control), MSCS, MSCE, FM (Fide Chess Master).







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

* Re: Ada vs C++ for numeric IO (integers)
  1996-07-22  0:00 Ada vs C++ for numeric IO (integers) Nasser Abbasi
  1996-07-22  0:00 ` Tom Zagotta
@ 1996-07-24  0:00 ` Keith Thompson
  1 sibling, 0 replies; 5+ messages in thread
From: Keith Thompson @ 1996-07-24  0:00 UTC (permalink / raw)



In <nhn30scxml.fsf@paralysys> nasser@apldbio.com (Nasser Abbasi) writes:
> I noticed that constraint error is not raised for short_integer if 
> I input a value outside short_integer range in an IO operation for 
> example. I check the LRM, and it does say that in 3.5.4.20 .

RM95-3.5.4(20) is the paragraph that says that operations that can't
deliver a correct answer due to overflow raise Constraint_Error.
I/O operations are not considered "operations" in the sense intended in
that paragraph.  The relevant paragraph is A.10.8(10), which says that
Get raises Data_Error if the value obtained is not within the bounds of
the subtype.

You didn't say so, but I presume you're using GNAT.  One of GNAT's more
obscure features is that overflow, stack, and elaboration checks are
disabled by default (because their current implementation of these checks
is inefficient).  You can enable them by using the "-gnato" option to gcc
or gnatmake.  With these checks enabled, Get correctly raises Data_Error.

Incidentally, if you use Ada.Short_Integer_Text_IO (a pre-instantiation
of Ada.Integer_IO), you'll get this problem regardless of how you compile
your own program.  Apparently Short_Integer_Text_IO was compiled without
the -gnato option.  I've reported this to report@gnat.com.

-- 
Keith Thompson (The_Other_Keith) kst@thomsoft.com <*>
TeleSoft^H^H^H^H^H^H^H^H Alsys^H^H^H^H^H Thomson Software Products
10251 Vista Sorrento Parkway, Suite 300, San Diego, CA, USA, 92121-2718
"As the most participatory form of mass speech yet developed, the Internet
deserves the highest protection from government intrusion." -- ACLU v. Reno




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

end of thread, other threads:[~1996-07-24  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-07-22  0:00 Ada vs C++ for numeric IO (integers) Nasser Abbasi
1996-07-22  0:00 ` Tom Zagotta
1996-07-22  0:00   ` Robert Dewar
1996-07-23  0:00   ` Nasser Abbasi
1996-07-24  0:00 ` Keith Thompson

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