2017-04-12 27 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你的代碼還有更多的缺陷。特別是,你應該檢查你的比較陳述。 –