* [Beginner Problem] Varibles not getting assigned correctly
@ 2008-01-11 5:52 Shane Smith
2008-01-11 8:01 ` Niklas Holsti
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Shane Smith @ 2008-01-11 5:52 UTC (permalink / raw)
Hi all,
I'm trying to write a simple program (that manacla game), I've
encountered a problem where the variables that are used in a nested
procedure can't be assigned a value. I'm using GNAT 4.1.2 on Linux.
---CODE---
procedure Manacla is
type pits is array (1 .. 14) of Integer;
type Score_Type is array (1 .. 2) of integer;
type arr_point is access pits;
board : arr_point;
Score1 : Integer := 0;
Score2 : Integer := 0;
Scores : Score_type := (0,0);
Player : Integer range 1 .. 2 := 1;
Winner : integer := 0;
Test : constant integer := 11;
Pit : Integer := 0;
...
procedure Status (Player : in integer)
is
begin
-- Check if 'playing pits' are empty, if not exit
while board.all(2 .. 7) = (0,0,0,0,0,0) or board.all(9 .. 14) =
(0,0,0,0,0,0) loop
if Player = 1 then
if board.all(2 .. 7) = (0,0,0,0,0,0) then
Winner := 1;
else exit;
end if;
else
if board.all(9 .. 14) = (0,0,0,0,0,0) then
Winner := 2;
else exit;
end if;
end if;
end loop;
end Status;
...
procedure Player1
is
Pit : Integer := 0;
Player : Integer := 1;
begin
...
Status(Player);
end PLayer1;
begin
...
player1;
end Manacla;
---END CODE---
Basically main body calls player1 and player1 calls status(player) (which
edits the winner variable).
In gdb 'p winner' gives "$14 = 7270388".
It happens to arrays and access varibles as well.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Beginner Problem] Varibles not getting assigned correctly
2008-01-11 5:52 [Beginner Problem] Varibles not getting assigned correctly Shane Smith
@ 2008-01-11 8:01 ` Niklas Holsti
2008-01-11 10:46 ` Peter Hermann
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Niklas Holsti @ 2008-01-11 8:01 UTC (permalink / raw)
Shane Smith wrote:
> Hi all,
>
> I'm trying to write a simple program (that manacla game), I've
> encountered a problem where the variables that are used in a nested
> procedure can't be assigned a value. I'm using GNAT 4.1.2 on Linux.
I think your conclusion ("can't be assigned a value") is too
strong; what you really observed is "gdb displays an unexpected value".
I occasionally get weird values from gdb (which I seldom use,
though) from Ada programs, where a Text_IO.Put at the same place
shows the correct, expected value.
> type arr_point is access pits;
> board : arr_point;
It's not necessarily relevant, but I'm curious why you use an
access type for the board. Are there several boards? (I don't know
this game.)
By the way, even with an access type, you don't need the ".all"
below, where you write board.all(2 .. 7), you can write just
board(2 .. 7). The ".all" is implied when you index an array or
take a slice of it.
> procedure Status (Player : in integer)
> is
>
> begin
> -- Check if 'playing pits' are empty, if not exit
> while board.all(2 .. 7) = (0,0,0,0,0,0) or board.all(9 .. 14) =
> (0,0,0,0,0,0) loop
Why are you looping here? The only thing that may change in the
loop is the Winner variable, so the loop either never starts, or
exits in the first iteration, or never exits.
> if Player = 1 then
> if board.all(2 .. 7) = (0,0,0,0,0,0) then
> Winner := 1;
If this happens, the loop gets stuck and never exits.
> else exit;
> end if;
> else
> if board.all(9 .. 14) = (0,0,0,0,0,0) then
> Winner := 2;
Same here.
> else exit;
> end if;
> end if;
> end loop;
> end Status;
>
> In gdb 'p winner' gives "$14 = 7270388".
At what point in the program did you give that gdb command? That
is, where did you break the program's execution?
If I had this problem I would first use Text_IO, not gdb, to print
out the values of the variables. If you do that, and you still get
the wrong value, perhaps you could show a bit more of the program
in your nest posting.
HTH
--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Beginner Problem] Varibles not getting assigned correctly
2008-01-11 5:52 [Beginner Problem] Varibles not getting assigned correctly Shane Smith
2008-01-11 8:01 ` Niklas Holsti
@ 2008-01-11 10:46 ` Peter Hermann
2008-01-11 17:51 ` Jeffrey R. Carter
2008-01-12 3:00 ` Steve
3 siblings, 0 replies; 5+ messages in thread
From: Peter Hermann @ 2008-01-11 10:46 UTC (permalink / raw)
Shane Smith <rusty_@users.sourceforge.net> wrote:
> I'm trying to write a simple program (that manacla game), I've
you might have a look at
http://www.sofport.com:5055/appletmagic/download/
;-)
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Beginner Problem] Varibles not getting assigned correctly
2008-01-11 5:52 [Beginner Problem] Varibles not getting assigned correctly Shane Smith
2008-01-11 8:01 ` Niklas Holsti
2008-01-11 10:46 ` Peter Hermann
@ 2008-01-11 17:51 ` Jeffrey R. Carter
2008-01-12 3:00 ` Steve
3 siblings, 0 replies; 5+ messages in thread
From: Jeffrey R. Carter @ 2008-01-11 17:51 UTC (permalink / raw)
Shane Smith wrote:
>
> type pits is array (1 .. 14) of Integer;
> type Score_Type is array (1 .. 2) of integer;
Your code would be easier to understand (and hence to comment on) if you were
consistent in your capitalization.
What does it mean for a pit or a score to be negative? Perhaps Natural or
another type or subtype based on the rules of the game would be better for the
component types here.
I presume the index of Score_Type represents a specific player. Using numbers is
probably not a good way to identify a player; an enumeration type might be better:
type Player_ID is (Human, Computer);
or some such.
> type arr_point is access pits;
> board : arr_point;
I see no reason to use access types in this program.
> procedure Status (Player : in integer)
It appears that the purpose of Status is to assign to Winner. It's usually
clearer to pass parameters than to modify global variables. I would think you'd
want something like
procedure Status
(Player : in Player_ID; Board : in Pits; Winner : out Player_ID);
Others have addressed your questions, so I won't repeat that here. I haven't
used a debugger for years, so I find it usually quicker to stick in some output
that to relearn how to use one. Given that gdb sometimes misbehaves, it can be
more effective, too.
--
Jeff Carter
"Propose to an Englishman any principle, or any instrument, however
admirable, and you will observe that the whole effort of the English
mind is directed to find a difficulty, a defect, or an impossibility
in it. If you speak to him of a machine for peeling a potato, he will
pronounce it impossible: if you peel a potato with it before his eyes,
he will declare it useless, because it will not slice a pineapple."
Charles Babbage
92
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [Beginner Problem] Varibles not getting assigned correctly
2008-01-11 5:52 [Beginner Problem] Varibles not getting assigned correctly Shane Smith
` (2 preceding siblings ...)
2008-01-11 17:51 ` Jeffrey R. Carter
@ 2008-01-12 3:00 ` Steve
3 siblings, 0 replies; 5+ messages in thread
From: Steve @ 2008-01-12 3:00 UTC (permalink / raw)
"Shane Smith" <rusty_@users.sourceforge.net> wrote in message
news:13oe10uh39g0v85@corp.supernews.com...
> Hi all,
>
> I'm trying to write a simple program (that manacla game), I've
> encountered a problem where the variables that are used in a nested
> procedure can't be assigned a value. I'm using GNAT 4.1.2 on Linux.
>
Hi.
Since you volunteered "beginner" problem I would like to make a suggestion
on program structure.
When I was in college (a couple of decades ago) where I learned Pascal, one
of my instructors had the requirement that no global variables were
permitted. Only global types.
If you are want to avoid using multiple packages (until you're a little...
very little) farther along you can structure the main procedure:
procedure Manacala is
procedure A( ... args ... ) is
begin
...
end A;
procedure Main is
... variables delclared here ..
begin
... code here that passes arguments to other procedures/functions
end Main;
begin
Main;
end Manacala;
When programs are broken down this way you can understand the operation of a
procedure or function based on just the arguments and the type definitions.
This makes it easy to understand small pieces of code and how they interact
with data.
Regards,
Steve
(The Duck)
> ---CODE---
>
> procedure Manacla is
>
> type pits is array (1 .. 14) of Integer;
> type Score_Type is array (1 .. 2) of integer;
> type arr_point is access pits;
> board : arr_point;
>
> Score1 : Integer := 0;
> Score2 : Integer := 0;
> Scores : Score_type := (0,0);
> Player : Integer range 1 .. 2 := 1;
> Winner : integer := 0;
> Test : constant integer := 11;
> Pit : Integer := 0;
>
> ...
>
> procedure Status (Player : in integer)
> is
>
> begin
> -- Check if 'playing pits' are empty, if not exit
> while board.all(2 .. 7) = (0,0,0,0,0,0) or board.all(9 .. 14) =
> (0,0,0,0,0,0) loop
>
> if Player = 1 then
> if board.all(2 .. 7) = (0,0,0,0,0,0) then
> Winner := 1;
> else exit;
> end if;
> else
> if board.all(9 .. 14) = (0,0,0,0,0,0) then
> Winner := 2;
> else exit;
> end if;
> end if;
> end loop;
> end Status;
>
> ...
>
> procedure Player1
> is
> Pit : Integer := 0;
> Player : Integer := 1;
>
> begin
> ...
> Status(Player);
> end PLayer1;
>
> begin
> ...
> player1;
> end Manacla;
>
> ---END CODE---
>
> Basically main body calls player1 and player1 calls status(player) (which
> edits the winner variable).
>
> In gdb 'p winner' gives "$14 = 7270388".
> It happens to arrays and access varibles as well.
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2008-01-12 3:00 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-01-11 5:52 [Beginner Problem] Varibles not getting assigned correctly Shane Smith
2008-01-11 8:01 ` Niklas Holsti
2008-01-11 10:46 ` Peter Hermann
2008-01-11 17:51 ` Jeffrey R. Carter
2008-01-12 3:00 ` Steve
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox