Solved Beginner needs help with Pascal

September 9, 2011 at 17:13:03
Specs: Windows 7, i7-2600 3.4 / 8 gigs
I keep getting "Error 26 - Type mismatch". Tell me please, where did I make a mistake.

Oh, and basic function of this program is to calculate the length of hypotenuse in right triangle using pythagoras' theorem.

program triangle;
var
a, b, c, d : integer;
begin
writeln ('Type the value of first side');
read(a);
if a < 0 then
begin
writeln ('Not possible');
end;
writeln ('Type the value of second side');
read(b);
if b < 0 then
begin
writeln ('Not possible');
end;
c := sqr(a) + sqr(b);
d := sqrt(c);
writeln (' Hypotenuse is', d, 'centimeters long');
end.

And sorry about poor english. :<
Feel free to point out any other error I've made.


See More: Beginner needs help with Pascal

Report •

#1
September 9, 2011 at 22:53:38
You are trying to assign a real (the result of sqrt()) to an integer (d). That's not allowed, hence the type mismatch.

Report •

#2
September 11, 2011 at 03:28:23
✔ Best Answer
As ijack pointed out you have to declare d as real; better you declare all variables as real since you can type integer values as input too. To get a readable display for d the layout must be added d:7:2 otherwise its output is in exponential format (scientific notation, not suited for a beginner).

Here your code slightly revised and rearranged.


program triangle;
var
  a, b, c, d: real;

begin
writeln; write ('Type the value of first side>'); readln(a);
if a <= 0 then begin
  writeln; writeln ('  Not possible!');
  halt
end;

writeln; write ('Type the value of second side>'); readln(b);
if b <= 0 then begin
  writeln; writeln ('  Not possible!');
  halt
end;

c:= sqr(a) + sqr(b);
d:= sqrt(c);

writeln; writeln ('Hypotenuse is ', d:7:2, ' centimeters long');
end.


Report •

#3
September 13, 2011 at 15:09:37
Thank You. big time. I won't ask of You to tell me in details as to why there's d:7:2 but could You like point me in right direction, as to where I can learn about that?
And one more thing, I don't really understand "halt" function in this program, as to why it's needed and why there's need for "write" after "writenl".
Non the less, thank You again.

Report •

Related Solutions

#4
September 14, 2011 at 10:17:57
Well, blankpicture, I shall answer your questions clearly as far as I can, but tomorrow since now in Italy where I live is late afternoon and tonight I have to attend a dinner with my wife (a mathematician teaching Geometry at college).

Please, stay in touch and be patient for now.


Report •

#5
September 14, 2011 at 12:31:08
I will be waiting for Your reply then. I've all the time in world. Always eager to learn more, even if it comes with waiting.

Report •

#6
September 15, 2011 at 07:56:29
Here I am back. Let me start by answering your questions about halt and write/writeln.

Halt builtin function breaks the execution of the program and returns control to the operating system. The same happens at the end of the source code when the compiler gets the end. statement: a hidden halt is inseterted. Do not interpret end; as a halt directive: end marks the conclusion of a paragraph started by coding begin but does not break program execution. If not present halt, the program continues to execute after a negative side is entered.

WriteLn without parameters displays a blank line. I added that to format a nice, more readable screen; Write (without ln) displays the parameters and does not force a line feed so the next Readln accepts the value just on the same screen line.

About d:7:2 you can find a good explanation in the following on-line tutorial

http://www.taoyue.com/tutorials/pascal

select Input and Output then Formatting Output. There are a lot of Pascal tutorials on line, but many are more oriented to expert programmers than a novice. I suppose however you have a Pascal textbook.

I am happy you have all the time in the world but I guessed you are a student and the time is never enough at school. Why are you interested in Pascal, a language almost died today as ancient Greek or Latin?

Feel free to contact me if you need support, maybe using the Private Message system of Computing.net.


Report •

Ask Question