Program Calculator;
Uses Crt;
Label 1;
Var Selection, YesNo : String;
    Number, A, B, C, Number2, Total, InsideRoot, MinusB, TWOA, Answer1, Answer2 : Integer;

  Begin
 1:Clrscr;
 Total :=0;
 Number :=0;
 Number2 :=0;
 Clrscr;
 Writeln('This is a more complicated calculator, please make a choice.');
 Writeln('1. Addition');
 Writeln('2. Subtraction');
 Writeln('3. Multiplication');
 Writeln('4. Division');
 Writeln('5. Quadratic');
 Writeln('6. Quit');
 Write('Your Choice Please:');
 Readln(Selection);
 If Selection = '1' then
        Begin
         ClrScr;
         Write('Number 1:');
         Readln(Number);
         ClrScr;
         Write('Number 2:');
         Readln(Number2);
         Total := Number + Number2;
         ClrScr;
         Write('Your Total is: ', Total);
         Readln;
         Write('Press any key to continue..');
         Readkey;
         Goto 1;
        End;
 If Selection = '2' then
        Begin
         ClrScr;
         Write('Number 1:');
         Readln(Number);
         ClrScr;
         Write('Number 2:');
         Readln(Number2);
         ClrScr;
         Total := Number - Number2;
         Write('Your Total is: ', Total);
         Readln;
         Write('Press any key to continue..');
         Readkey;
         Goto 1;
        End;
 If Selection = '3' then
        Begin
         Clrscr;
         Write('Number 1:');
         Readln(Number);
         Write('Number 2:');
         Readln(Number2);
         Total := Number * Number2;
         Write('Your Total is: ', Total);
         Readln;
         Write('Press any key to continue..');
         Readkey;
         Goto 1;
        End;
If Selection = '4' then
        Begin
         Clrscr;
         Write('Number 1:');
         Readln(Number);
         Write('Number 2:');
         Readln(Number2);
         Total := Number DIV Number2;
         Write('Your Total is: ', Total);
         Readln;
         Write('Press any key to continue..');
         Readkey;
         Goto 1;
        End;
If Selection = '5' then
        Begin
         Clrscr;
         Write('Now for this, ill need a b and c.');
         Readln;
         ClrScr;
         Write('Please enter a:');
         Readln(A);
         Write('Please enter b:');
         Readln(B);
         Write('Please enter c:');
         Readln(C);
         MinusB := 0-B;
         TWOA := 2*A;
         InsideRoot := (B*B) - (4*A*C);
         Answer1 := ((0-B) + (Sqrt(InsideRoot))) DIV TWOA;
         Answer2 := ((0-B) - Sqrt(InsideRoot)) DIV TWOA;
         Writeln('Your answer is: ', Answer1);
         Write('Or: ', Answer2);
         Readln;
        End;
 If Selection = '6' then
        Begin
         Clrscr;
         Write('Press any key to quit.');
         Readkey;
         Halt
        End;
End.

When i try to compile it says

Recompiling because of D:\Program Files\FreePascalCompiler\bin\i386-win32\Calculator.pas
Calculator.pas(97,21) Error: Incompatible types: got "Extended" expected "LongInt"
Calculator.pas(98,21) Error: Incompatible types: got "Extended" expected "LongInt"
Calculator.pas(110,4) Fatal: There were 2 errors compiling module, stopping
Calculator.pas(0) Fatal: Compilation aborted

And its not extended, its a Integer, i also dont know exactly what is incompatible.

(This program is a calculator and i was working on a system to work out Quadratic Equations to see if i could do it)

Another link to my code: http://pastebin.com/d29e8e48d

Recommended Answers

All 10 Replies

The sqrt function returns a value of type Extended. ( number with a decimal point in it.) Internally these numbers are stored differently then Integer or LongInt.
That's why they are called incompatible types.

Oohhh, that makes sense... Cheers mate.
Is there anyway i can change the output of the Sqrt to Integer? Setting it to 2 d.p. or anything?

If I where you I would not make the calculation with integers.
This is typically something where real numbers (and even complex numbers!) are involved. So get rid of the DIV, make A,B,C etc. real or extended.

i've made the quadratic bit into a separate program, i got it working and changed a few things as you suggested, but now i get error 127 whenever i run it, i cant see where this comes from.. any help?
(the error comes up after i type in C and says Exitcode 217)

Program Quadratic;
Uses Crt, Math;
Label 1;
Var
 A, B, C, InsideRoot, MinusB, TWOA, Final1, Final2 : Real;
 Answer1, Answer2, SqrtInside, SqrtInside2 : Extended;
        Begin
         1:Clrscr;
         Write('Now for this, ill need a b and c.');
         Readln;
         ClrScr;
         Write('Please enter a:');
         Readln(A);
         Write('Please enter b:');
         Readln(B);
         Write('Please enter c:');
         Readln(C);
         MinusB := 0-B;
         InsideRoot := (B*B) - (4*A*C);
         SqrtInside := Sqrt(InsideRoot);
         SqrtInside2 := Round(SqrtInside);
         Answer1 := (-B + SqrtInside2) / (2*A);
         Answer2 := (-B - SqrtInside2) / (2*A);
         Writeln('Your answer is: ', Answer1);
         Write('Or: ', Answer2);
         Readln;
         ClrScr;
         Write('Press any key to continue..');
         Readkey;
         Goto 1;
         End.

On which line do you get the error?
You also must test if InsideRoot is negative or not.
Ever took the square root of a negative number? That's where complex numbers creep in...

How do i find what line the error appears on? it doesnt say >.>

(im really bad at this, started today at my teachers suggestion, im doing Computing next year)

Edit: also, how do i check if its negative? Although with quadratics, if its a negative it means its unsolvable or you've typed it in wrong..

The if statement was invented for that.
if InsideRoot < 0 then -->negative, give a message about complex roots and stop(for the moment)
else -->continue the calculation
I don't know what exitcode 217 means, have you documentation about the errors?
Delete line 21, why should you Round SqrtInside?

okay, i've changed it to this...

Program Quadratic;
Uses Crt, Math;
Label 1;
Var
 A, B, C, InsideRoot, BSquared, SecondPart : Real;
 Answer1, Answer2, SqrtInside, SqrtInside2 : Extended;
        Begin
         1:Clrscr;
         Write('Now for this, ill need a b and c.');
         Readln;
         ClrScr;
         Write('Please enter a:');
         Readln(A);
         Write('Please enter b:');
         Readln(B);
         Write('Please enter c:');
         Readln(C);
         BSquared := B*B;
         SecondPart := 4*A*C;
         InsideRoot := BSquared - SecondPart;
         If insideroot <= 0 then
         Begin
         Write('Please make sure you tpyed it in correctly.');
         Writeln('This equation does not work, please try again');
         Readkey;
         Goto 1;
         End;
         If insideroot > 0 then
         Begin
         SqrtInside := Sqrt(InsideRoot);
         Answer1 := ((0-B) + SqrtInside) / (2*A);
         Answer2 := ((0-B) - SqrtInside) / (2*A);
         Writeln('Your answer is: ', Answer1);
         Write('Or: ', Answer2);
         Readln;
         ClrScr;
         Write('Press any key to continue..');
         Readkey;
         Goto 1;
         End;
End.

The exception has gone, but now it ALWAYS says the equation does not work...

NEVERMIND, i got the equation wrong, it should be -4*A*C :)

Thanks Dude, helped me get it working.

Check your if statements once again. 0 is a perfectly legal value to calculate here, it will give you two equal roots which have the value -b/2a.
Glad to be of help, mark this as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.