I wanted to write a simple arithmetic calculation in Delphi. But it displays an error:

[Error] Project1.dpr (10): Missing operator or semicolon

Here is my code:

program Project1; uses SysUtils; var x,y:real; begin write('x='); readln(x); if(x>1) then y:=cos(x)/(x*x*x+3*sin(x)+8) else y:=x*x*x+3*sin(x)+8 writeln('y=', y:0:3); Readln; end. 

    2 answers 2

    Not enough ; after the else block. Not to be confused, remember - always surround the code blocks with begin and end operator brackets, even if it is a block from one call:

     program Project1; uses SysUtils; var x,y:real; begin write('x='); readln(x); if(x>1) then begin y:=cos(x)/(x*x*x+3*sin(x)+8); end else begin y:=x*x*x+3*sin(x)+8; end; writeln('y=', y:0:3); Readln; end. 

    It's on ideone

    • I copied your code and compiled this error is issued sh.uploads.ru/3bcPB.png - Erkin Pardayev
    • @ErkinPardayev I have no idea what you compiled there, but definitely not my code. Ideone compiles everything perfectly. - gbg
    • Thank. In my opinion, I need to change the compiler - Erkin Pardayev
    • @ErkinPardayev - Delphi 7, though old, but not in marasmus. Such problems should not be. - gbg

    So it will be correct both from a logical point of view and from the point of view of formatting:

     program Project1; {$APPTYPE CONSOLE} // Инструкция для Делфи, что это консольное приложение uses SysUtils; var x, y: real; begin // Ждем ввода значения Х от пользователя write('x='); readln(x); // Логика программы if (x > 1) then y := cos(x) / (x * x * x + 3 * sin(x) + 8) else y := x * x * x + 3 * sin(x) + 8; // <-- Ошибка была здесь, пропущена ; // Показываем результат writeln('y=', y:0:3); readln; end. 

    Notice how proper formatting allows you to immediately see the whole program and notice errors in it.

    PS Just in case, cos trigonometric functions expect an angle in radians at the entrance, so if the user enters a number in degrees, you will need to convert it to radians.