comp.lang.ada
 help / color / mirror / Atom feed
* Help setting up an array of records
@ 2002-05-22 15:09 Matt Thomas
  2002-05-22 16:39 ` Stephen Leake
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Matt Thomas @ 2002-05-22 15:09 UTC (permalink / raw)


I know this might be a stupid question to most of you but i have just
started using Ada.

For my uni coursework i have to write a program to simulate a vending
machine.

I'm using an array of records to represent the items in the vending machine
but I dont know how to actually get the information into my array.  I need
to specify the name of the item, its cost and the quantity left.

Here is the record and array declaration, any help would be grateful.

MACHINE_SLOTS  :   constant := 6; --

type PRODUCT_TYPE is
   record
         NAME      :   STRING (1..12);
         LEN       :   POSITIVE;
         PRICE     :   INTEGER;
         QUANTITY  :   INTEGER;
   end record;

type PRODUCT_TYPE_ARR is array (1..MACHINE_SLOTS) of PRODUCT_TYPE;

PRODUCT   :   PRODUCT_TYPE_ARR;





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

* Re: Help setting up an array of records
  2002-05-22 15:09 Help " Matt Thomas
@ 2002-05-22 16:39 ` Stephen Leake
  2002-05-22 17:18 ` Marin David Condic
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 13+ messages in thread
From: Stephen Leake @ 2002-05-22 16:39 UTC (permalink / raw)


"Matt Thomas" <matt_ontheroad@hotmail.com> writes:

> I know this might be a stupid question to most of you but i have just
> started using Ada.

No problem. We don't do homework, but we will answer specific questions.

> <snip>

> MACHINE_SLOTS  :   constant := 6; --
> 
> type PRODUCT_TYPE is
>    record
>          NAME      :   STRING (1..12);
>          LEN       :   POSITIVE;
>          PRICE     :   INTEGER;
>          QUANTITY  :   INTEGER;
>    end record;
> 
> type PRODUCT_TYPE_ARR is array (1..MACHINE_SLOTS) of PRODUCT_TYPE;
> 
> PRODUCT   :   PRODUCT_TYPE_ARR;

This is a good start. Now show what you have tried to do that doesn't
work, so we can make suggestions.

-- 
-- Stephe



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

* Re: Help setting up an array of records
  2002-05-22 15:09 Help " Matt Thomas
  2002-05-22 16:39 ` Stephen Leake
@ 2002-05-22 17:18 ` Marin David Condic
  2002-05-22 17:32 ` Mark Johnson
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 13+ messages in thread
From: Marin David Condic @ 2002-05-22 17:18 UTC (permalink / raw)


That depends. Do you want to read the content from a file or just initialize
the array with some constant values?

The array you declared "PRODUCT" can have an initialization expression as
part of its declaration. The expression needs to evaluate to the type
PRODUCT_TYPE_ARR - so it could be a function. In general, the simplest thing
to do would be to use an aggregate. Look in the Ada Reference Manual in
section 4.3 for a description of aggregates for arrays and records. Also,
your textbook almost certainly has a description of it, so check the index
for "aggregate" there as well.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com


"Matt Thomas" <matt_ontheroad@hotmail.com> wrote in message
news:acgcbn$sue$1@paris.btinternet.com...
> I know this might be a stupid question to most of you but i have just
> started using Ada.
>
> For my uni coursework i have to write a program to simulate a vending
> machine.
>
> I'm using an array of records to represent the items in the vending
machine
> but I dont know how to actually get the information into my array.  I need
> to specify the name of the item, its cost and the quantity left.
>
> Here is the record and array declaration, any help would be grateful.
>
> MACHINE_SLOTS  :   constant := 6; --
>
> type PRODUCT_TYPE is
>    record
>          NAME      :   STRING (1..12);
>          LEN       :   POSITIVE;
>          PRICE     :   INTEGER;
>          QUANTITY  :   INTEGER;
>    end record;
>
> type PRODUCT_TYPE_ARR is array (1..MACHINE_SLOTS) of PRODUCT_TYPE;
>
> PRODUCT   :   PRODUCT_TYPE_ARR;
>
>





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

* Re: Help setting up an array of records
  2002-05-22 15:09 Help " Matt Thomas
  2002-05-22 16:39 ` Stephen Leake
  2002-05-22 17:18 ` Marin David Condic
@ 2002-05-22 17:32 ` Mark Johnson
  2002-05-22 19:15 ` Preben Randhol
  2002-05-23  1:58 ` Steve Doiel
  4 siblings, 0 replies; 13+ messages in thread
From: Mark Johnson @ 2002-05-22 17:32 UTC (permalink / raw)


Matt Thomas wrote:
> [help for homework]
> I'm using an array of records to represent the items in the vending machine
> but I dont know how to actually get the information into my array.  I need
> to specify the name of the item, its cost and the quantity left.
> 
> Here is the record and array declaration, any help would be grateful.
> 
> MACHINE_SLOTS  :   constant := 6; --
> 
> type PRODUCT_TYPE is
>    record
>          NAME      :   STRING (1..12);
>          LEN       :   POSITIVE;
>          PRICE     :   INTEGER;
>          QUANTITY  :   INTEGER;
>    end record;
> 
> type PRODUCT_TYPE_ARR is array (1..MACHINE_SLOTS) of PRODUCT_TYPE;
> 
> PRODUCT   :   PRODUCT_TYPE_ARR;

You didn't say much, so I can't judge what you know or don't know. You
also didn't say which text book you are using, so I'll make a couple
references to the ever so helpful Ada Reference Manual :-). [I don't
recommend reading the ARM unless you want to be a "language lawyer"
because it is not so easy to read...] You could look up the terms I
refer to in your text book to find the chapters where this is described.

 - 4.1 Names,
  o 4.1.1 - indexed components (into the array)
  o 4.1.3 - selected components (elements of the record)
  the methods described can be combined to refer to a single (or more
than one) component of the record within a single component (or slice)
in the array
 - 4.2 Literals, a specific value of the appropriate type
 - 4.3 Aggregates, 
  o 4.3.1 - record aggregates (for Product_Type)
  o 4.3.3 - array aggregates (for Product_Type_Arr)
  if you want to initialize several values at once (e.g., an "empty
product")
 - 5.2 Assignment statement, to replace the current value of a variable
with the result of an expression

There is a lot more than what I refer to that will likely confuse at
this point. For example, continue to use simple types until you
understand the rules of type conversion. Don't use slices unless you
clearly understand them as well. Do a simple example first and
generalize until you understand the basic rules.
  --Mark



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

* Re: Help setting up an array of records
  2002-05-22 15:09 Help " Matt Thomas
                   ` (2 preceding siblings ...)
  2002-05-22 17:32 ` Mark Johnson
@ 2002-05-22 19:15 ` Preben Randhol
  2002-05-23  1:58 ` Steve Doiel
  4 siblings, 0 replies; 13+ messages in thread
From: Preben Randhol @ 2002-05-22 19:15 UTC (permalink / raw)


On Wed, 22 May 2002 15:09:44 +0000 (UTC), Matt Thomas wrote:
> I know this might be a stupid question to most of you but i have just
> started using Ada.
> 
> For my uni coursework i have to write a program to simulate a vending
> machine.
> 
> I'm using an array of records to represent the items in the vending machine
> but I dont know how to actually get the information into my array.  I need
> to specify the name of the item, its cost and the quantity left.
> 
> Here is the record and array declaration, any help would be grateful.

Read: 

   http://goanna.cs.rmit.edu.au/~dale/ada/aln/6_arrays.html

and

   http://www.it.bton.ac.uk/staff/je/adacraft/ch06.htm

Preben



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

* Re: Help setting up an array of records
  2002-05-22 15:09 Help " Matt Thomas
                   ` (3 preceding siblings ...)
  2002-05-22 19:15 ` Preben Randhol
@ 2002-05-23  1:58 ` Steve Doiel
  4 siblings, 0 replies; 13+ messages in thread
From: Steve Doiel @ 2002-05-23  1:58 UTC (permalink / raw)


"Matt Thomas" <matt_ontheroad@hotmail.com> wrote in message
news:acgcbn$sue$1@paris.btinternet.com...
> I know this might be a stupid question to most of you but i have just
> started using Ada.

Good.  I hope you eventually like the language as much as I do.

> For my uni coursework i have to write a program to simulate a vending
> machine.
>
> I'm using an array of records to represent the items in the vending
machine
> but I dont know how to actually get the information into my array.  I need
> to specify the name of the item, its cost and the quantity left.

My first response is that you have at least 3 choices:
  Read the values from a file
  Ask the user to enter the values
  Hard code the values into the program

My second response (in case you're really _just_ getting started) is with a
very small sample:

  PRODUCT(1).PRICE := 42;

I hope this helps,
SteveD






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

* re: help setting up an array of records
@ 2002-05-26 13:00 Matt Thomas
  2002-05-26 13:18 ` Preben Randhol
  2002-05-26 16:16 ` Matt Thomas
  0 siblings, 2 replies; 13+ messages in thread
From: Matt Thomas @ 2002-05-26 13:00 UTC (permalink / raw)


I posted a few days ago with a problem about setting up an array of records,
thanks for your everyones help on he matter but i still cant get it right.

I dont want the user to enter the values, i want to initialise them in the
code.
I'm trying to do it like this:

PRODUCT(1) := ("Cola        ",4,60,5);

From all I have read up about this should be the way to do it but i can't
get it to compile.
I get the error message: missing ":"
and the (1) is the section that is highlighted.

A few of you asked which textbook I'm using, it is "J. Skansholm, Ada 95
From the Beginning"





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

* Re: help setting up an array of records
  2002-05-26 13:00 help setting up an array of records Matt Thomas
@ 2002-05-26 13:18 ` Preben Randhol
  2002-05-26 13:24   ` Preben Randhol
  2002-05-26 16:16 ` Matt Thomas
  1 sibling, 1 reply; 13+ messages in thread
From: Preben Randhol @ 2002-05-26 13:18 UTC (permalink / raw)


On Sun, 26 May 2002 13:00:10 +0000 (UTC), Matt Thomas wrote:
> I'm trying to do it like this:
> 
> PRODUCT(1) := ("Cola        ",4,60,5);
> 
> From all I have read up about this should be the way to do it but i can't
> get it to compile.
> I get the error message: missing ":"
> and the (1) is the section that is highlighted.

Have you definded PRODUCT( ) somewhere ?

-- 
"Jeg tror nordmenn har glemt hvordan de tilbreder fisk. De er mest
 opptatt av firkantet fisk."
  --  Kristian Kristiansen, yrkesfisker, aftenposten.no 19/04/02



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

* Re: help setting up an array of records
  2002-05-26 13:18 ` Preben Randhol
@ 2002-05-26 13:24   ` Preben Randhol
  2002-05-26 16:10     ` Matt Thomas
  0 siblings, 1 reply; 13+ messages in thread
From: Preben Randhol @ 2002-05-26 13:24 UTC (permalink / raw)


On Sun, 26 May 2002 13:18:36 +0000 (UTC), Preben Randhol wrote:
> Have you definded PRODUCT( ) somewhere ?

Sorry I think my answer is a bit confusing. Looking at the code you
posted earlier I see that you have done:

   MACHINE_SLOTS  :   constant := 6; --

   type PRODUCT_TYPE is
      record
            NAME      :   STRING (1..12);
            LEN       :   POSITIVE;
            PRICE     :   INTEGER;
            QUANTITY  :   INTEGER;
      end record;

   type PRODUCT_TYPE_ARR is array (1..MACHINE_SLOTS) of PRODUCT_TYPE;

   PRODUCT   :   PRODUCT_TYPE_ARR;

So the question is now where did you put the following line : 

   PRODUCT(1) := ("Cola        ",4,60,5);

in your code?

Preben



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

* Re: help setting up an array of records
  2002-05-26 13:24   ` Preben Randhol
@ 2002-05-26 16:10     ` Matt Thomas
  2002-05-28 15:01       ` Marin David Condic
  0 siblings, 1 reply; 13+ messages in thread
From: Matt Thomas @ 2002-05-26 16:10 UTC (permalink / raw)




>
>    MACHINE_SLOTS  :   constant := 6; --
>
>    type PRODUCT_TYPE is
>       record
>             NAME      :   STRING (1..12);
>             LEN       :   POSITIVE;
>             PRICE     :   INTEGER;
>             QUANTITY  :   INTEGER;
>       end record;
>
>    type PRODUCT_TYPE_ARR is array (1..MACHINE_SLOTS) of PRODUCT_TYPE;
>
>    PRODUCT   :   PRODUCT_TYPE_ARR;

--setting up the array
PRODUCT(1) := ("Cola        ",4,60,5);


Yes I put that line in the code just below the the rest of the code I had
written.






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

* Re: help setting up an array of records
  2002-05-26 13:00 help setting up an array of records Matt Thomas
  2002-05-26 13:18 ` Preben Randhol
@ 2002-05-26 16:16 ` Matt Thomas
  2002-05-26 19:53   ` Preben Randhol
  1 sibling, 1 reply; 13+ messages in thread
From: Matt Thomas @ 2002-05-26 16:16 UTC (permalink / raw)


I Think I have cracked it, the reason it wasn't working is that when i typed
in what i wanted in the array i did it in the declaration part.

I have now done it after the 'BEGIN' statement and i think it works ok.

Thanks for you help.





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

* Re: help setting up an array of records
  2002-05-26 16:16 ` Matt Thomas
@ 2002-05-26 19:53   ` Preben Randhol
  0 siblings, 0 replies; 13+ messages in thread
From: Preben Randhol @ 2002-05-26 19:53 UTC (permalink / raw)


On Sun, 26 May 2002 16:16:39 +0000 (UTC), Matt Thomas wrote:
> I Think I have cracked it, the reason it wasn't working is that when i typed
> in what i wanted in the array i did it in the declaration part.
> 
> I have now done it after the 'BEGIN' statement and i think it works ok.

Correct. :-)

Preben



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

* Re: help setting up an array of records
  2002-05-26 16:10     ` Matt Thomas
@ 2002-05-28 15:01       ` Marin David Condic
  0 siblings, 0 replies; 13+ messages in thread
From: Marin David Condic @ 2002-05-28 15:01 UTC (permalink / raw)


Just guessing from what you are saying, that this appears in the declarative
part of the program. What you've got is an executable statement and it
belongs in the executable part of the program. Try putting it after the
"begin" and see if it gets better. (BTW, there *is* a way to initialize the
array in the declarative part of the program, but you've got to do it with
an aggregate at the point where the object is declared. Try it with the
statement you've got now first & if you want to try it another way, post
here again and maybe we can point you at an appropriate reference.)

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com


"Matt Thomas" <matt_ontheroad@hotmail.com> wrote in message
news:acr1ci$rd3$1@knossos.btinternet.com...
>
>
> >
> >    MACHINE_SLOTS  :   constant := 6; --
> >
> >    type PRODUCT_TYPE is
> >       record
> >             NAME      :   STRING (1..12);
> >             LEN       :   POSITIVE;
> >             PRICE     :   INTEGER;
> >             QUANTITY  :   INTEGER;
> >       end record;
> >
> >    type PRODUCT_TYPE_ARR is array (1..MACHINE_SLOTS) of PRODUCT_TYPE;
> >
> >    PRODUCT   :   PRODUCT_TYPE_ARR;
>
> --setting up the array
> PRODUCT(1) := ("Cola        ",4,60,5);
>
>
> Yes I put that line in the code just below the the rest of the code I had
> written.
>
>
>





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

end of thread, other threads:[~2002-05-28 15:01 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-05-26 13:00 help setting up an array of records Matt Thomas
2002-05-26 13:18 ` Preben Randhol
2002-05-26 13:24   ` Preben Randhol
2002-05-26 16:10     ` Matt Thomas
2002-05-28 15:01       ` Marin David Condic
2002-05-26 16:16 ` Matt Thomas
2002-05-26 19:53   ` Preben Randhol
  -- strict thread matches above, loose matches on Subject: below --
2002-05-22 15:09 Help " Matt Thomas
2002-05-22 16:39 ` Stephen Leake
2002-05-22 17:18 ` Marin David Condic
2002-05-22 17:32 ` Mark Johnson
2002-05-22 19:15 ` Preben Randhol
2002-05-23  1:58 ` Steve Doiel

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