Programming

(Free Pascal) TryStrToInt

steloflute 2021. 5. 21. 00:47

C#의 TryParse와 비슷하다. out parameter도 그렇고...

 

Int32.TryParse Method (System) | Microsoft Docs

 

Int32.TryParse Method (System)

Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.

docs.microsoft.com

 

TryStrToInt (freepascal.org)

 

TryStrToInt

Try to convert a string to an int64 value, and report on success.

www.freepascal.org

예제:

uses sysutils;
var
    a, b, c, answer: longint;
    s: string;
    score: longint = 0;
begin
    randomize;
    while true do
    begin
        writeln('점수: ', score);
        a := random(20);
        b := random(20);
        answer := a + b;
        write(a, ' + ', b, ' = ');
        readln(s);
        if trystrtoint(s, c) and (c = answer) then
            score := score + 1
        else
            writeln('정답: ', answer);
        writeln()
    end
end.