Results 1 to 6 of 6

Thread: How can I do a new line?

  1. #1

    How can I do a new line?

    Hello. I am writing a calculator with MidletPascal. You type into one input field and it shows the result after you click "Calcular".

    Currently it only accepts very simple input, on the kind A Operator B

    Where A and B are real or integer numbers and Operator is {*, /, -, +, ^}

    I am showing the output using this:

    uiText := FormAddString('' + Ret);

    But then the text is added linearly on the form. I would like to jump one line each time a calculation is done. I tryed this:

    uiText := FormAddString('' + Ret + '\n');

    But it doesn¬Ąt work =/

    Bellow is my code in case anyone wants to see:

    [pascal]
    {
    Pascalc - a simple calculator with memory
    }
    program pascalc;

    const
    cMultiply = '*';
    cSum = '+';
    cSubtract = '-';
    cDivide = '/';
    cPower = '^';

    var
    cmdQuit, cmdCalc, clicked: command;
    uiInput, uiText: integer;
    expression: string;
    Ret: real;

    procedure initialize;
    begin
    end;

    procedure drawScreen;
    begin
    // switch to form mode from default canvas mode
    ShowForm;

    SetFormTitle('Calculadora');

    uiInput := FormAddTextField('Entre com a express?Ło:', '', 20, TF_ANY);

    cmdQuit := createCommand('Sair', CM_EXIT, 1);
    cmdCalc := createCommand('Calcular', CM_OK, 1);
    addCommand(cmdQuit);
    addCommand(cmdCalc);

    repaint;
    end;

    procedure CalculateExpression;
    var
    first, second, str: string;
    i, pos: Integer;
    ifirst, isecond: real;
    Found: Boolean;
    begin
    pos := 0; // pos = 0 indica erro na express?Ło

    Found := False;

    // Locates where the operator is
    for i := 0 to Length(expression) - 1 do
    begin
    Str := Copy(expression, i, i + 1);

    if (Str = cMultiply) or (Str = cSum) or (Str = cSubtract) or (Str = cDivide) or (Str = cPower) then
    begin
    // If the operator is found twice, the expression is wrong
    if Found = True then pos := 0
    else
    begin
    pos := i;
    Found := True;
    end;
    end;
    end;

    if pos <> 0 then
    begin
    // Separates the string
    first := Copy(expression, 0, pos);
    ifirst := StringToReal(first, 10);
    second := Copy(expression, pos + 1, Length(expression));
    isecond := StringToReal(second, 10);

    // Does the calculation
    Str := Copy(expression, pos, pos + 1);

    if Str = cMultiply then Ret := ifirst * isecond
    else if Str = cSum then Ret := ifirst + isecond
    else if Str = cSubtract then Ret := ifirst - isecond
    else if Str = cDivide then Ret := ifirst / isecond
    else if Str = cPower then Ret := Pow(ifirst, isecond);

    // Mostra o resultado na tela
    uiText := FormAddString('' + Ret);
    end
    else uiText := FormAddString('Erro');
    end;

    begin
    initialize;

    drawScreen;

    // wait until the user clickes on a button
    repeat
    clicked := getClickedCommand;

    if (clicked = cmdCalc) then
    begin
    // retrieve the entered name
    expression := FormGetText(uiInput);

    CalculateExpression;
    end;

    until clicked = cmdQuit;
    end.[/pascal][/code]

  2. #2

    Re: How can I do a new line?

    Quote Originally Posted by sekelsenmat
    uiText := FormAddString('' + Ret + '\n');

    But it doesn¬Ąt work =/
    Try this:
    [pascal]
    uiText := FormAddString('' + Ret + #13#10);
    [/pascal]


    Edit: wait, wait... uiText is an integer. Are you sure that FormAddString returns an integer? :?:
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  3. #3

    How can I do a new line?

    I agree with Legolas using #13#10 is the Pascal way of doing a C \n
    <br /><br />There are a lot of people who are dead while they are still alive. I want to be alive until the day I die.<br />-= Paulo Coelho =-

  4. #4

    How can I do a new line?

    Sorry. This:

    uiText := FormAddString('' + Ret + #13#10);

    Will refuse to compile, due to unknown # character.

    I know on Java you can use line.separator. I will try somehow to access this java feature.

  5. #5

    How can I do a new line?

    Maybe this could work:
    [pascal]uiText := FormAddString('' + Ret + Chr(13) + Chr(10)); [/pascal]
    Get your fpc4gba copy now!
    Get your fpc4nds copy now!

  6. #6

    How can I do a new line?

    Thanks a lot!

    The Chr thing works =)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •