Pascal 程序抛出未知错误:EConvertError
我正在尝试创建一个程序,它接受用户的输入,并检查其有效日期是否为2000-2099年。当我对代码进行bug测试时,我连续两次输入enter作为输入,第一次输入enter时,没有抛出错误,但第二次控制台闪烁消息并崩溃。我在它崩溃时拍了一张截图,这是它显示的错误Pascal 程序抛出未知错误:EConvertError,pascal,freepascal,Pascal,Freepascal,我正在尝试创建一个程序,它接受用户的输入,并检查其有效日期是否为2000-2099年。当我对代码进行bug测试时,我连续两次输入enter作为输入,第一次输入enter时,没有抛出错误,但第二次控制台闪烁消息并崩溃。我在它崩溃时拍了一张截图,这是它显示的错误 An unhandled exception occured at $0040CDE1 EConvertError : "" is an invalid integer 我最好的猜测是,这个错误是由我的一个stroin
An unhandled exception occured at $0040CDE1
EConvertError : "" is an invalid integer
我最好的猜测是,这个错误是由我的一个stroint由于EConvertError引起的,但我不清楚为什么它只在第二次运行时抛出,而不是第一次运行时抛出。我怀疑错误发生在第85、98、111或195行
当我最初编写这个程序时,a是Pascal的初学者,所以我为任何草率的代码提前道歉
下面是所有的代码,如果你想自己运行它
program DateVerifierAssignment;
uses crt, sysutils;
var
userInputArray: array[0..7] of string;
len, i, day, month, year: integer;
date, userInput, stringDay, stringMonth, stringYear: string;
errorCatch: boolean;
//DECLARING FUNCTIONS & PROCEDURES
procedure TitleScreen();
begin
//prints a coloured wall of text to give information like name and instructions
WriteLn(' o-------------------------o');
Write(' | ');TextColor(White);Write('Date Validation Program');TextColor(Cyan);WriteLn(' |');
WriteLn(' o-------------------------o');
WriteLn();
WriteLn(' o-------------------------o');
Write(' | ');TextColor(White);Write('Instructions');TextColor(Cyan);WriteLn(' |');
WriteLn(' o-------------------------o');
Write(' | ');TextColor(White);Write('Enter a date and the');TextColor(Cyan);WriteLn(' |');
Write(' | ');TextColor(White);Write('program will verify it');TextColor(Cyan);WriteLn(' |');
Write(' | ');TextColor(White);Write('and convert it to its');TextColor(Cyan);WriteLn(' |');
Write(' | ');TextColor(White);Write('long form');TextColor(Cyan);WriteLn(' |');
WriteLn(' o-------------------------o');
WriteLn();
TextColor(White);
Write(' press any key to begin');
//waits for user input then clears the screen and returns the text color to white
ReadKey();
ClrScr;
end;
function DateVerification(var userInput: string): boolean;
var
errorLimit : boolean;
bounds: integer;
begin
errorLimit := True;
//stores the length of the string as a variable
len := Length(userInput);
//checks to see if entry is 8 chracters long and displays an error message and returns user to input screen if it doesn't fit
if (len <> 8) and (errorLimit = True) then
begin
ClrScr();
TextColor(Red);
WriteLn('input was not the right length (8 characters)');
TextColor(White);
Write('make sure date fits format ');
TextColor(LightGreen);
WriteLn('dd/mm/yy');
TextColor(White);
Readkey();
ClrScr();
errorLimit := false;
Dateverification := false;
end;
//spits each character into its own spot in an array
for i := 1 to len do
userInputArray[i - 1] := userInput[i];
//tests if every slot in the array where a slash should be is a slash
for i := 0 to len-1 do
begin
if (userInputArray[2] <> '/') or (userInputArray[5] <> '/') and (errorLimit = true) then
begin
ClrScr();
TextColor(Red);
WriteLn('input did not have slashes in correct locations ');
TextColor(LightGreen);
WriteLn('dd/mm/yy');
TextColor(White);
Readkey();
ClrScr();
errorLimit := false;
Dateverification := false;
end;
end;
year := ((StrToInt(userInputArray[6]))*10) + StrToInt(userInputArray[7]);
if (year < 1) or (year > 99) and (errorLimit = true) then
begin
ClrScr();
TextColor(Red);
WriteLn('year was not from 0 to 99');
TextColor(White);
Readkey();
ClrScr();
errorLimit := false;
Dateverification := false;
end;
month := ((StrToInt(userInputArray[3]))*10) + StrToInt(userInputArray[4]);
if (month < 1) or (month > 12) and (errorLimit = true) then
begin
ClrScr();
TextColor(Red);
WriteLn('month was not from 1 to 12');
TextColor(White);
Readkey();
ClrScr();
errorLimit := false;
Dateverification := false;
end;
day := ((StrToInt(userInputArray[0]))*10) + StrToInt(userInputArray[1]);
if (month = 4) or (month = 6) or (month = 9) or (month = 11) then
bounds := 30;
if (month = 2) then
begin
if (IsLeapYear(year) = true) then
bounds := 29
else
bounds := 28;
end
else
bounds := 31;
if (day < 1) or (day > bounds) and (errorLimit = true) then
begin
ClrScr();
TextColor(Red);
WriteLn('day was not from 1 to days in month');
TextColor(White);
Readkey();
ClrScr();
errorLimit := false;
Dateverification := false;
end;
if (errorLimit = true) then
DateVerification := True;
end;
function IsLeapYear(var year: integer): boolean;
//simple function to determine if a year is a leap year on the gregorian calender
begin
if (year mod 4) = 0 then
if (year mod 100 <> 0) then
if (year mod 400 = 0) then
IsLeapYear := true
else
IsLeapYear := false
else
IsLeapyear := false
else
IsLeapyear := false
end;
procedure DateToAlpha(var userInput: string);
begin
end;
//MAIN PROGRAM
begin
//preparing for while loop later in code and changes text colour to cyan
errorCatch := true;
TextColor(Cyan);
//Displays a title screen and instruction about how to use(stored in a procedure to help with readability)
TitleScreen();
//begins a loop so that if an error is to occur the program can easily ask for a new date and try again
while (errorCatch = true) do
begin
//sets error catch to 0 so that if there a no errors the program will not loop
errorCatch := false;
//displays information on how to input a date as well as an example with different colours for better readability
Write('Enter a date in the format');TextColor(LightGreen); WriteLn(' dd/mm/yy');TextColor(White);
Write('e.g. ');TextColor(LightGreen);WriteLn(' 09/07/20'#13#10);TextColor(White);
//takes date user inputs and stores it
ReadLn(userInput);
//calls the date verification function to see if the date is valid
//(refer to dateVerification at the top of the program for more info and comments)
if (DateVerification(userInput) = false) then
errorCatch := true;
len := Length(userInput);
for i := 1 to len do
userInputArray[i - 1] := userInput[i];
year := ((StrToInt(userInputArray[6]))*10) + StrToInt(userInputArray[7]);
readKey();
end;
end.
通常情况下,我们不会提供完全编码的qs答案,这显然是课程作业 在这种情况下,我想我会破例,因为很明显,你是在犯错误 这是不必要的恶劣天气,而您编写代码的方式很可能会妨碍您 调试它并使其正常工作 下面的例子展示了一个非常简单的, 以dd/mm/yyyy格式提供的验证21世纪日期的干净方法。信息技术 显示处理和验证步骤的正确顺序;如果它检测到错误 在任何步骤中,它都会指出问题所在并停止。如果执行到最后, 提供的日期字符串必须有效 我故意省略了任何循环 或者美化代码,因为首先要做的最重要的事情是简洁明了 处理和验证步骤的编码。如果你想使用循环,很好,但是你使用的方式 e、 errorCatch,我敢打赌如果你在6个月后回到你的代码,你不会记得怎么做了 这应该是有效的。此任务不需要任何复杂的标志或循环-用户只需能够键入后跟[Enter]的八个字符,然后代码就会告诉您它是否有效 注意,我使用了标准的复制功能来分离 输入日期。顺便说一句,你的声明
userInputArray: array[0..7] of string;
这完全是错误的。如果您想要一个包含一定数量字符的数组,很好,但这会
只是不必要地使处理和处理用户键入的内容变得复杂。太远了
更简单,因此接受8个字符的单个字符串时更不容易出错
处理好这件事。但在任何情况下,为了允许字符串包含日期和月份数字之后的/字符,字符串的长度应为10,而不是8,以允许这些数字和4年数字
program DateVerification;
uses
SysUtils;
var
sUserInput,
sDay,
sMonth,
sYear : String;
iDay,
iMonth,
iYear,
iDays: Integer;
bIsLeapYear : Boolean;
function DaysInMonth(iMonth, iYear : Integer) : Integer;
begin
Result := -1; // you supply the code for this taking into account leap year for February
end;
begin
writeln('Please enter a date between 01/01/2000 and 31/12/2099 in the format shown');
readln(sUserInput);
if Length(sUserInput) <> 10 then begin
writeln('Input is wrong length.');
Halt;
end;
if (sUserInput[3] <> '/') or (sUserInput[6] <> '/') then begin
writeln('Input is incorrectly delimited.');
Halt;
end;
sDay := Copy(sUserInput, 1, 2);
sMonth := Copy(sUserInput, 4, 2);
sYear := Copy(sUserInput, 7, 4);
iYear := StrToInt(sYear);
if (iYear < 2000) or (iYear > 2099) then begin
writeln('Invalid year : ', sYear);
Halt;
end;
bIsLeapYear := IsLeapYear(iYear); // you supply the code for IsLeapYear
iMonth := StrToInt(sMonth);
if (iMonth < 1) or (iMonth > 12) then begin
writeln('Invalid month : ', sMonth);
Halt;
end;
iDay := StrToInt(sDay);
if (iDay < 1) or (iDay > DaysInMonth(iMonth, iYear)) then begin
// You need to supply the DaysInMoth function, which of course
// needs to account for February in a leap year
writeln('Invalid day of month: ', sMonth);
Halt;
end;
writeln(sUserInput, ' is a valid date.');
readln;
end.
你可能想和你的老师坐下来讨论课程。帮助您学习是他们的工作。在ReadLnuserInput之后,您应该做的第一件事是检查userInput的长度是否正确6?如果不是,则发出错误信号。错误长度的一个具体情况是用户刚刚按下[Enter],在这种情况下,userInput将为空,在其任何部分调用StrToIn都会引发您提到的异常。顺便说一句,您有太多的变量-您不应该需要全部三个或errorLimit、errorCatch和DateVerification。简化…这篇文章中的代码太多了。请把它简化为一个注释,用“最小”这个词来说明你遇到的具体问题。@AndreasRejbrand[拍打额头的声音]哎呀!你当然是对的!结果是,我原来的代码没有考虑分隔符或四位数年份,然后我改变了主意,决定处理它们。我马上纠正它。非常感谢!