comp.lang.ada
 help / color / mirror / Atom feed
From: nasser@apldbio.com (Nasser Abbasi)
Subject: Ada vs C++ for numeric IO (integers)
Date: 1996/07/22
Date: 1996-07-22T00:00:00+00:00	[thread overview]
Message-ID: <nhn30scxml.fsf@paralysys> (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).







             reply	other threads:[~1996-07-22  0:00 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1996-07-22  0:00 Nasser Abbasi [this message]
1996-07-22  0:00 ` Ada vs C++ for numeric IO (integers) Tom Zagotta
1996-07-22  0:00   ` Robert Dewar
1996-07-23  0:00   ` Nasser Abbasi
1996-07-24  0:00 ` Keith Thompson
replies disabled

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