newtothis newtothis - 8 months ago 99
Pascal Question

Conditional Statement Not Working With Records

How do i display all customers who have purchased an item on a certain day, my code doesn't seem to work, ive tried implementing the code within the displayByDayPurchased procedure. Sorry if this is a simple question, i'm still new to programming.

type

Tday = (monday, tuesday);

Tcustomer = record
itemPurchased:string;
dayPurchased: Tday;
end;

Tcustomers = array of Tcustomer;

function readDay(prompt:string): Tday;
var
selection:Integer;
begin
writeln('1. Monday');
writeln('2. Tuesday');

selection := ReadIntegerRange('Select day purcased (1 - 3): ', 1, 3);
result := Tday(selection-1);
end;

function readCustomers(prompt:string):TCustomers;
var
numOfCustomers:integer;
i:integer;
begin
numOfCustomers := ReadInteger('Enter number of customers: ');
setLength(result, numOfCustomers);

for i := 0 to high(result) do
begin
writeln(i);
result[i].itemPurchased := ReadString('Item Purchased: ');
result[i].dayPurchased := readDay(prompt);
end;
end;

procedure displayByDayPurchased(customers:TCustomers);
var
specific_day:integer;
begin
specific_day := ReadInteger('Enter day to see items purchased');

if (specific_day = customers.dayPurchased[specific_day])then
begin

end;
end;

procedure Main();
var
customer: Tcustomers;
begin
customer := readCustomers('Read in customers');

end;

begin
Main();
end.

Answer Source

my code doesn't seem to work, ive tried implementing the code within the displayByDayPurchased procedure.

Well, in the code you've posted, your displayByDayPurchased doesn't actually implement anything which would result in matching records being displayed, all you have is an empty begin ... end block:

if (specific_day = customers.dayPurchased[specific_day])then
begin

end;

and a) that doesn't correctly specify the match condition anyway and b) it will not compile because a Tcustomers array does not have a dayPurchased field ( a Tcustomer record does, but not the array).

Your code depends of quite a lot of things whose definitions you have not provide (ReadString, ReadInteger, ReadIntegerRange, etc) so it is difficult to give you a tested solution.

However an implementation of your displayByDayPurchased should probably look something like this:

procedure  displayByDayPurchased(customers:TCustomers);
var
    specific_day:integer;
    i : integer;
    ADay : Tday;
begin
    specific_day := ReadInteger('Enter day to see items purchased');
    ADay := Tday(specific_day); //  converts integer to TDay value

    for i := Low(customers) to High(Customers) do begin
      if customers[i].dayPurchased = ADay then begin
        writenln(customers[i].itemPurchased);
      end;
    end;
end;

I assume your Tcustomer record actually includes the customer's name, andx your code needs modifying to handle that.

Btw, your function readDay(prompt:string): Tday is wrong; Because of your definition of Tday, the allowed values in readDay should be 0 and 1, because the lowest value of the Tday enumeration actually corresponds to zero, not 1.

Also, you did not say which Pascal compiler you are using, but most modern versions allow a call like Tday(integerValue) to convert an integer to an instance value of the enumeration.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download