Jacob Grumley
Jacob Grumley

Reputation: 63

The error invalid Pointer Operation on delphi 7

procedure searchAndReceipt;
var
  amt, counter, check: integer;
  gtinStore, qtyStore: array of integer;
  totalCost: real;
begin
  check     := 0;
  totalCost := 0.0;

  write('Enter how many products you are purchasing: ');
  repeat
    readln(amt);
    if (amt > 11) and (amt <= 0) then
      writeln ('Please re-enter how many products are you purchasing with a value between 1-10')
    else
      check:= 1;
  until check = 1;

  SetLength(gtinStore, amt);
  SetLength(qtyStore, amt);
  SetLength(receiptArray, amt);

  for counter:=1 to amt do
  begin
    write('Enter a GTIN code: ');
    repeat
      readln(gtinStore[counter]);
      if (gtinStore[counter] >= 99999999) and (gtinStore[counter] <= 1000000) then
        writeln ('Please re-enter the Gtin Code with a value of 8 digits')
      else
        check:= 1;
    until check = 1;

    check := 0;
    write('Enter the Quantity: ');
    repeat
      readln(qtyStore[counter]);
      if (qtyStore[counter] >= 11) and (qtyStore[counter] <= 0) then
        writeln ('Please re-enter the quantity with a value between 1-10')
      else
        check:= 1;
    until check = 1;
  end;

  assign(stockFile,'stockFile.dat');
  Reset(stockFile);
  counter:=1;
  while not EOF(stockFile) do
  begin
    receiptArray[counter].productName := ('Product Not Found');
    receiptArray[counter].productGTIN := 0;
    receiptArray[counter].productPrice := 0.0;
    inc(counter);
  end;
  read (stockFile, Stock);
  for counter:=1 to amt+1 do
  begin
    while not EOF(stockFile) do
    begin
      read (stockFile, Stock);
      if Stock.productGTIN = gtinStore[counter] then
        receiptArray[counter].productGTIN:= Stock.productGTIN;
      receiptArray[counter].productName:= Stock.productName;
      receiptArray[counter].productPrice:= Stock.productPrice;
    end;
  end;

  assign(receiptFile, 'receipt.txt');
  rewrite(receiptFile);
  for counter:= 1 to amt+1 do
  begin
    if receiptArray[counter].productName = 'Product Not Found' then
    begin
      writeln(receiptFile, 'GTIN: ', gtinStore[counter]);
      writeln(receiptFile, receiptArray[counter].productName);
      writeln(receiptFile, '');
    end
    else
    begin
      writeln(receiptFile, 'GTIN: ',gtinStore[counter]);
      writeln(receiptFile, 'Name: ',receiptArray[counter].productName);
      writeln(receiptFile, 'Quantity: ', qtyStore[counter]);
      writeln(receiptFile, 'Price: £',receiptArray[counter].productPrice*qtyStore[counter]:4:2);
      writeln(receiptFile, '');
      totalCost := ((receiptArray[counter].productPrice * qtyStore[counter]) + totalCost);
    end;
  end;
  choices:=1;
end;

begin
  choices:= 1;
  while choices <> 3 do
  begin;
    writeln('Press 1 to create the stock file');
    writeln('Press 2 to search for an item and print a receipt');
    writeln('Press 3 to exit');
    write('Choice: ');
    readln(choices);
    writeln;
    case choices of
      1: createStock;
      2: searchAndReceipt;
    end;
  end;
end.

I run this procedure (there's another procedure before this that places stock into a file), what this is supposed to do is to take that stock out and place it into a text file... however after I've entered the GTIN number and the quantities of the items my program produces this error

Exception EAccessViolation in module Task_2.exe at 00002550. Access violation at address 00402550 in module 'Task_2.exe'. Read of address 03491DD4.

within the shell, and a message box pops up saying

Project Task_2.exe raised exception class EInvalidPointer with message 'invalid Pointer Operation'. Process Stopped

Thanks in advance

Upvotes: 0

Views: 1763

Answers (1)

David Heffernan
David Heffernan

Reputation: 613441

Dynamic arrays are 0-based, but your code assumes 1-based indexing. Hence you index off the end of the array, and hence the runtime errors. Fix the code by using 0-based indices. That is loop from 0 to N-1 rather than from 1 to N.

Even what you fix that, you have loops that run from 1 to N+1 so you aren't even allocating enough space for your arrays.

You should enable range checking in the compiler options so that the compiler can emit diagnostics code to give you better error messages.

Upvotes: 5

Related Questions