delphi 7上的错误无效指针操作

时间:2017-04-12 14:54:18

标签: delphi 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.

我运行这个程序(在此之前还有另一个程序将库存放入文件中),这应该做的是将该库存取出并放入文本文件中...但是在我和之后#39;已输入GTIN号码和我的程序产生此错误的项目数量

  

00002550模块Task_2.exe中的异常EAccessViolation。   模块&#39; Task_2.exe&#39;中地址00402550的访问冲突。读取地址03491DD4。

shell中的

,弹出一个消息框说

  

项目Task_2.exe引发了异常类EInvalidPointer,并显示消息&#39;无效的指针操作&#39;。流程已停止

提前致谢

1 个答案:

答案 0 :(得分:5)

动态数组是基于0的,但您的代码假定基于1的索引。因此,您索引数组的末尾,从而导致运行时错误。使用基于0的索引修复代码。这是从0到N-1而不是从1到N的循环。

即使你解决了这个问题,你也有从1到N + 1的循环,所以你甚至没有为数组分配足够的空间。

您应该在编译器选项中启用范围检查,以便编译器可以发出诊断代码,以便为您提供更好的错误消息。