2017-04-12 183 views
0
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. 

我运行这个程序(也就是这一点,放在股票进入一个文件之前另一个程序),这是什么是应该做的是采取股票出来,并把它转换为文本文件......我已经进入然而之后的GTIN号码和我的程序产生这个错误在模块Task_2.exeDelphi的错误无效的指针操作7

异常EAccessViolation的项目在00002550. 访问冲突在模块的数量在地址00402550“Task_2 。可执行程序'。阅读地址03491DD4。

外壳内,并且一个消息框弹出说

项目Task_2.exe引发的异常类EInvalidPointer与消息“无效的指针操作”。过程停止提前

由于

回答

5

动态阵列是基于0的,但是您的代码假定基于1的索引。因此,您可以索引数组的末尾,从而避免运行时错误。通过使用基于0的索引来修复代码。这是循环从0到N-1,而不是从1到N

即使你解决什么,你必须运行从1到N + 1,所以你甚至不为你的阵列分配足够的空间循环。

你应该能够在编译器选项范围检查,以便编译器可以发出诊断代码,以提供更好的错误讯息。

+0

谢谢!我已经照你所说的,不过,我现在接受范围错误 –

+0

那是因为你的代码仍然是断开的。您不仅必须使用基于零的索引,还必须分配足够大的数组。第二段涵盖了这一点。现在编译器正在帮助您找到错误的确切位置,您将能够调试您的程序。这对你来说是一个机会。你可以学习如何调试。一项技能对你来说是无价的。 –

+0

@Jacob你的代码还有更多的缺陷。特别是,你应该检查你的比较陈述。 –