comp.lang.ada
 help / color / mirror / Atom feed
* Strange problem in GNAT 3.04 Win 95
@ 1996-10-24  0:00 Simon Moore
  1996-10-24  0:00 ` Nicolay Belofastow
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: Simon Moore @ 1996-10-24  0:00 UTC (permalink / raw)



Hello all

If anybody has got the time could they please take a look at the
listing below. The code seems syntactically and semantically correct
but doesn't perform as it should.

When 2 is selected you don't get the chance to input a character
although option 1 works fine.

But, if you un-coment the get(c) before the case statement the program
works fine. It doesn't bother with that get(c) but the one inside the
case statement does.

Any ideas?


-- ** Program listing ** --

with gnat.io; use gnat.io;

procedure t1 is

	choice: INTEGER;
	n: NATURAL;
	c: CHARACTER;
	quitflag: BOOLEAN:= FALSE;

BEGIN

	WHILE quitflag= FALSE LOOP

		NEW_LINE;
		PUT_LINE("Select an option");
		PUT_LINE("****************");
		PUT_LINE("1 - Get a natural");
		PUT_LINE("2 - Get a character");
		PUT_LINE("3 - Nothing");
		PUT_LINE("4 - Nothing");
		NEW_LINE;
		PUT_LINE("0 - Exit");
  
		GET(choice);

		-- ** GET(c);	-- THIS IS THE IMPORTANT LINE

		CASE choice IS

			WHEN 1 =>
				PUT_LINE("Selection 1");
				GET(n);
				PUT(n);
				PUT_LINE(" was entered");
				PUT_LINE("~~~~~~~");
      
			WHEN 2 =>
				PUT_LINE("Selection 2");
				GET(c);
				PUT(c);
				PUT_LINE(" was entered");
				PUT_LINE("~~~~~~~");

			WHEN 3 =>
				PUT_LINE("Selection 3");
				PUT_LINE("~~~~~~~");
      
			 WHEN 4 =>
				PUT_LINE("Selection 4");
				PUT_LINE("~~~~~~~");

			WHEN 0 =>
				quitflag:= TRUE;
    
			WHEN others =>
				PUT("Invalid entry");

		END CASE;

	END LOOP;

END t1;
*****************************************************************************
* Simon Moore - smoore@chowgar.demon.co.uk - http://www.chowgar.demon.co.uk *
*****************************************************************************




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

* Re: Strange problem in GNAT 3.04 Win 95
  1996-10-24  0:00 Strange problem in GNAT 3.04 Win 95 Simon Moore
@ 1996-10-24  0:00 ` Nicolay Belofastow
  1996-10-25  0:00   ` Simon Moore
  1996-10-25  0:00 ` Pascal Obry
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Nicolay Belofastow @ 1996-10-24  0:00 UTC (permalink / raw)



Hello!

smoore@chowgar.demon.co.uk (Simon Moore) writes:

>If anybody has got the time could they please take a look at the
>listing below. The code seems syntactically and semantically correct
>but doesn't perform as it should.

>When 2 is selected you don't get the chance to input a character
>although option 1 works fine.

>But, if you un-coment the get(c) before the case statement the program
>works fine. It doesn't bother with that get(c) but the one inside the
>case statement does.

>Any ideas?


>-- ** Program listing ** --

>with gnat.io; use gnat.io;

>procedure t1 is

>	choice: INTEGER;
>	n: NATURAL;
>	c: CHARACTER;
>	quitflag: BOOLEAN:= FALSE;

>BEGIN

>	WHILE quitflag= FALSE LOOP

>		NEW_LINE;
>		PUT_LINE("Select an option");
>		PUT_LINE("****************");
>		PUT_LINE("1 - Get a natural");
>		PUT_LINE("2 - Get a character");
>		PUT_LINE("3 - Nothing");
>		PUT_LINE("4 - Nothing");
>		NEW_LINE;
>		PUT_LINE("0 - Exit");
>  
>		GET(choice);

>		-- ** GET(c);	-- THIS IS THE IMPORTANT LINE

>		CASE choice IS

>			WHEN 1 =>
>				PUT_LINE("Selection 1");
>				GET(n);
>				PUT(n);
>				PUT_LINE(" was entered");
>				PUT_LINE("~~~~~~~");
>      
>			WHEN 2 =>
>				PUT_LINE("Selection 2");
>				GET(c);
>				PUT(c);
>				PUT_LINE(" was entered");
>				PUT_LINE("~~~~~~~");

>			WHEN 3 =>
>				PUT_LINE("Selection 3");
>				PUT_LINE("~~~~~~~");
>      
>			 WHEN 4 =>
>				PUT_LINE("Selection 4");
>				PUT_LINE("~~~~~~~");

>			WHEN 0 =>
>				quitflag:= TRUE;
>    
>			WHEN others =>
>				PUT("Invalid entry");

>		END CASE;

>	END LOOP;

>END t1;

I am novice in Ada, but from my C experience I know, that sometimes
after usage of scanf function to make a formatted input (e.g. integer
value, as you do) some extra character remains in the keyboard
 and following getchar (getc in Ada) eats it and so makes no expected
 input. 

 May be GNAT compiler uses inside of  the I/O package
 this C functinality. I see only  one way to overcome 
 the problem -- the same, you did -- 
 use extra getc call after the integet input.

 Best regards, Nick.
--
-------------------------------------------------
Nickolay Belofastow
University of the Federal Armed Forces, Munich
e81bnick@rz.unibw-muenchen.de




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

* Re: Strange problem in GNAT 3.04 Win 95
  1996-10-24  0:00 Strange problem in GNAT 3.04 Win 95 Simon Moore
  1996-10-24  0:00 ` Nicolay Belofastow
@ 1996-10-25  0:00 ` Pascal Obry
  1996-10-26  0:00 ` Tom Griest
  1996-10-31  0:00 ` John English
  3 siblings, 0 replies; 6+ messages in thread
From: Pascal Obry @ 1996-10-25  0:00 UTC (permalink / raw)




The Get (Integer) read the int you enter and the first Get (Char) read =
the <return>
character you hit after entering your choice.

Pascal.

--=20

--|------------------------------------------------------------
--| Pascal Obry                               Team-Ada Member |
--|                                                           |
--| EDF-DER-IPN-SID- Ing=E9nierie des Syst=E8mes d'Informations   |
--|                                                           |
--| Bureau G1-010           e-mail: pascal.obry@der.edfgdf.fr |
--| 1 Av G=E9n=E9ral de Gaulle  voice : +33-1-47.65.50.91         |
--| 92141 Clamart CEDEX     fax   : +33-1-47.65.50.07         |
--| FRANCE                                                    |
--|------------------------------------------------------------
--|
--|   http://ourworld.compuserve.com/homepages/pascal_obry
--|
--|   "The best way to travel is by means of imagination"






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

* Re: Strange problem in GNAT 3.04 Win 95
  1996-10-24  0:00 ` Nicolay Belofastow
@ 1996-10-25  0:00   ` Simon Moore
  0 siblings, 0 replies; 6+ messages in thread
From: Simon Moore @ 1996-10-25  0:00 UTC (permalink / raw)



On 24 Oct 1996 18:32:01 GMT, e81bnick@kommsrv.rz.unibw-muenchen.de
(Nicolay Belofastow) wrote:

>I am novice in Ada, but from my C experience I know, that sometimes
>after usage of scanf function to make a formatted input (e.g. integer
>value, as you do) some extra character remains in the keyboard
> and following getchar (getc in Ada) eats it and so makes no expected
> input. 
>
> May be GNAT compiler uses inside of  the I/O package
> this C functinality. I see only  one way to overcome 
> the problem -- the same, you did -- 
> use extra getc call after the integet input.
>
> Best regards, Nick.
>--
>-------------------------------------------------
>Nickolay Belofastow
>University of the Federal Armed Forces, Munich
>e81bnick@rz.unibw-muenchen.de

Hi Nick

Thanks for the reply, I guess I'll have to live with the problem.

Regards.

*****************************************************************************
* Simon Moore - smoore@chowgar.demon.co.uk - http://www.chowgar.demon.co.uk *
*****************************************************************************




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

* Re: Strange problem in GNAT 3.04 Win 95
  1996-10-24  0:00 Strange problem in GNAT 3.04 Win 95 Simon Moore
  1996-10-24  0:00 ` Nicolay Belofastow
  1996-10-25  0:00 ` Pascal Obry
@ 1996-10-26  0:00 ` Tom Griest
  1996-10-31  0:00 ` John English
  3 siblings, 0 replies; 6+ messages in thread
From: Tom Griest @ 1996-10-26  0:00 UTC (permalink / raw)



smoore@chowgar.demon.co.uk (Simon Moore) writes:

>If anybody has got the time could they please take a look at the
>listing below. The code seems syntactically and semantically correct
>but doesn't perform as it should.

You are missing statement to flush the rest of the input line
when you do a get on the integer.  You need a Skip_Line; after
the Get(Choice);

[snip]


>		PUT_LINE("1 - Get a natural");
>		PUT_LINE("2 - Get a character");
>		PUT_LINE("3 - Nothing");
>		PUT_LINE("4 - Nothing");
>		NEW_LINE;
>		PUT_LINE("0 - Exit");
>  
>		GET(choice);
>
>		-- ** GET(c);	-- THIS IS THE IMPORTANT LINE
>

Get(C) will not work in the general case, and specifically if the
user enters additional (possibly space) characters after the number.
BTW, it is good practice to use bullet-proof input routines for
this sort of thing, since a user making a typo could easily get
an unhandled exception in your example.

The reason why case 1 works, but not case two is that the get on
integer is defined to scan over non-numeric characters, whereas
get on a character simply returns the next input character.

-Tom




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

* Re: Strange problem in GNAT 3.04 Win 95
  1996-10-24  0:00 Strange problem in GNAT 3.04 Win 95 Simon Moore
                   ` (2 preceding siblings ...)
  1996-10-26  0:00 ` Tom Griest
@ 1996-10-31  0:00 ` John English
  3 siblings, 0 replies; 6+ messages in thread
From: John English @ 1996-10-31  0:00 UTC (permalink / raw)



Simon Moore (smoore@chowgar.demon.co.uk) wrote:
: Hello all

: When 2 is selected you don't get the chance to input a character
: although option 1 works fine.

: But, if you un-coment the get(c) before the case statement the program
: works fine. It doesn't bother with that get(c) but the one inside the
: case statement does.

: Any ideas?

Is it perhaps that you've hit return after entering "choice"
and that the get(c) before the case is reading an end-of-line
character?  Get(n) will skip whitespace onto the next line
but get(c) won't.  Try using Skip_Line before the loop.

---------------------------------------------------------------
 John English              | mailto:je@brighton.ac.uk
 Senior Lecturer           | http://www.comp.it.bton.ac.uk/je
 Dept. of Computing        | fax: (+44) 1273 642405
 University of Brighton    |
---------------------------------------------------------------




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

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

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-10-24  0:00 Strange problem in GNAT 3.04 Win 95 Simon Moore
1996-10-24  0:00 ` Nicolay Belofastow
1996-10-25  0:00   ` Simon Moore
1996-10-25  0:00 ` Pascal Obry
1996-10-26  0:00 ` Tom Griest
1996-10-31  0:00 ` John English

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