1 Attachment(s)
Need help with Pascal program on array
Need help in making a program for a meal plan and adding meal cost and displaying that information. I need to store number of guests in an array with their name and meal typy 'V' for vegetarian and 'N' for non vegetarian , this data must me stored in an array . can anyone help me to complete this program, m sort of stuck this is what i have so far .
Program WeddingGuests (input,output);
Var
mealpreference:array[1..500]of char;
guests:array[1..500] of string;
costV:real;
costN:real;
totalguest:integer;
g:integer;
i:char;
V:char;
N:char;
Begin
Writeln('Enter total number of invited guests');
Readln( totalguest);
For g:=1 to totalguest do
Writeln ('enter meal preference');
Readln ( i);
If( i ='V') OR (i = 'N') then
mealpreference[g]:= i
Else
Writeln('invalid meal type re-enter until valid');
End.{end else}
End;{endif}
End;{end for}
If (i:='V')then
costV:= costV * 35
Else
(meal preference:='N') then
costN:= costN * 40
End; {endif}
End; {end else}
Total cost :=costN + costV
Writeln('Enter name of guests')
Readln (guest)
If (guests[g] <> guests) then
Wrieln('sorry your name does not appear leave!')
Else
Writeln('Welcome to the wedding please enjoy!')
End; {endif}
End; {end else}
End.
Re: Need help with Pascal program on array
For this I would be looking to have an array of record with a record holding the info for each guest. It's a very long time since I last programmed in pascal! - and haven't got easy access to a pascal compiler. So something along the lines of (not tried)
Code:
Program WeddingGuests(input, output);
Const MaxGuests = 500;
veg = 'V';
nonVeg = 'N';
Type Guest = Record
mealpreference :char;
name : string
End;
Var guests : array[1..maxGuests] of Guest;
totalguest : integer;
gnam : string;
p : char;
g : integer;
Begin
Repeat
Writeln('Enter total number of invited guests');
Readln(totalguest);
if (totalguest < 0) OR (totalguest > maxGuests) then
Writeln('Invalid number of guests')
Until (totalguest > 0) AND (totalguest <= maxGuests);
for g := 1 to totalguest do
begin
Writeln('Enter name of guests');
Readln (gnam);
guests[g].name := gnam;
Repeat
Writeln ('enter meal preference');
Readln (p);
If ( p = veg) OR (p = nonVeg) then
guests[g].mealpreference := p
Else
Writeln('invalid meal type re-enter until valid')
Until (p = veg) OR (p = nonVeg)
End
End.
PS OK got access to a pascal compiler and the code compiles OK.