delphi 7上的错误无效指针操作 程序搜索和接收; 变量 金额、计数器、支票:整数; gtinStore、qtyStore:整数数组; 总成本:真实成本; 开始 检查:=0; 总成本:=0.0; 填写('输入您购买的产品数量:'); 重复 readln(amt); 如果(amt>11)和(amt=9999999)和(gtinStore[counter]=11)和(qtyStore[counter]
动态数组是基于0的,但您的代码假设是基于1的索引。因此,您索引了数组的末尾,因此出现了运行时错误。请使用基于0的索引修复代码。这是从0到N-1而不是从1到N的循环 即使您解决了这个问题,您也有从1到N+1的循环,因此您甚至没有为阵列分配足够的空间delphi 7上的错误无效指针操作 程序搜索和接收; 变量 金额、计数器、支票:整数; gtinStore、qtyStore:整数数组; 总成本:真实成本; 开始 检查:=0; 总成本:=0.0; 填写('输入您购买的产品数量:'); 重复 readln(amt); 如果(amt>11)和(amt=9999999)和(gtinStore[counter]=11)和(qtyStore[counter],delphi,delphi-7,Delphi,Delphi 7,动态数组是基于0的,但您的代码假设是基于1的索引。因此,您索引了数组的末尾,因此出现了运行时错误。请使用基于0的索引修复代码。这是从0到N-1而不是从1到N的循环 即使您解决了这个问题,您也有从1到N+1的循环,因此您甚至没有为阵列分配足够的空间 您应该在编译器选项中启用范围检查,以便编译器可以发出诊断代码以提供更好的错误消息。动态数组是基于0的,但您的代码采用基于1的索引。因此,您索引了数组的结尾,从而索引了运行时错误。通过使用基于0的索引修复代码。这是从0 t开始的循环o N-1,而不是从1
您应该在编译器选项中启用范围检查,以便编译器可以发出诊断代码以提供更好的错误消息。动态数组是基于0的,但您的代码采用基于1的索引。因此,您索引了数组的结尾,从而索引了运行时错误。通过使用基于0的索引修复代码。这是从0 t开始的循环o N-1,而不是从1到N 即使您解决了这个问题,您也有从1到N+1的循环,因此您甚至没有为阵列分配足够的空间
您应该在编译器选项中启用范围检查,以便编译器可以发出诊断代码以提供更好的错误消息。谢谢!我已经按照您所说的做了,但是,我现在收到了范围错误,这是因为您的代码仍然存在漏洞。您不仅必须使用基于零的索引,还必须充分分配大型数组。第二段涵盖了这一点。现在编译器正在帮助您找到错误的确切点,您将能够调试您的程序。这对您来说是一个机会。您可以学习如何调试。这对您来说是非常宝贵的技能。@Jacob您的代码中还有更多的缺陷。特别是,您应该检查你的比较语句。确实如此。例如,数字很难同时大于11和小于零。使用调试器。谢谢!我已经按照你说的做了,但是,我现在收到了范围错误,这是因为你的代码仍然不完整。你不仅必须使用基于零的索引,还必须分配足够大的ar射线。第二段涵盖了这一点。现在编译器正在帮助您找到错误的确切点,您将能够调试您的程序。这对您来说是一个机会。您可以学习如何调试。这对您来说是非常宝贵的技能。@Jacob您的代码中还有更多的缺陷。特别是,您应该检查您的比较语句。事实上。例如,一个数字既大于11又小于零是很困难的。请使用调试器。
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.