2

I have created a program to determine if a quadratic equation gives a 'real' number as its answer and if so, what it is. However, this is my first time working with if/else so my program won't compile past the else and, after searching for half an hour I'm no closer to finding out why Code is as follows:

program Quadratic_Equation_Solver;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, CustApp;
  var
    a, b, c : real;
begin
   writeln('Insert the Value for a please');
   readln(a);
   writeln('Insert the Value for b please');
   readln(b);
   writeln('Insert the Value for c please');
   readln(c);
   if (-4*a*c<b*b) then
      writeln('These variables return an imaginary quantity that');
      writeln('Cannot be computed. Please try again');
      readln;
   (*here it breaks*) else
   Writeln('The Answer is x = ',(-b+sqrt((b*b)-(4*a*c))/(2*a)):8:2);
   readln;
end.

At the break it says it needs a semi-colon but that hasn't worked

2 Answers 2

4

It looks like you're missing a begin and end statement inside your if and else sections. The compiler needs these to determine what line of code is included in the if or the else codepath:

if some condition then
begin
    ...
end
else
begin
    ...
end

so in your case:

program Quadratic_Equation_Solver;

{$mode objfpc}{$H+}

uses
  Classes, SysUtils, CustApp;
  var
    a, b, c : real;
begin
   writeln('Insert the Value for a please');
   readln(a);
   writeln('Insert the Value for b please');
   readln(b);
   writeln('Insert the Value for c please');
   readln(c);
   if (-4*a*c>b*b) then
   begin
      writeln('These variables return an imaginary quantity that');
      writeln('Cannot be computed. Please try again');
   end
   else
   begin
     Writeln('The Answer is x = ',(-b+sqrt((b*b)-(4*a*c))/(2*a)):8:2);
   end
   readln;
end.
Sign up to request clarification or add additional context in comments.

Comments

0

You can't use semicolons in your last statement before your else statement.

program Quadratic_Equation_Solver;

{$mode objfpc}{$H+}

uses
Classes, SysUtils, CustApp;
var
a, b, c : real;
begin
 writeln('Insert the Value for a please');
 readln(a);
 writeln('Insert the Value for b please');
 readln(b);
 writeln('Insert the Value for c please');
 readln(c);
   if (-4*a*c>b*b) then
    begin
    writeln('These variables return an imaginary quantity that');
    writeln('Cannot be computed. Please try again')
   end (*When using an else statement dont use semicolons*)    
 else 
Writeln('The Answer is x = ',(-b+sqrt((b*b)-(4*a*c))/(2*a)):8:2);
readln;
end.

1 Comment

This is wrong, because the real issue is the missing begin..end (which you've also included but chosen not to mention), not a semicolon (which is not the problem in this case but you did choose to describe). You can in fact use a semicolon here, because the statement would be before the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.