Member Avatar for Lioshenka

I am writing a program (well, actually I have finished it) using pascal, the program is to read in 3 sides of the triangle and produce a message whether it is right-angled, iscosceles, obtuse etc. Now just to finish it off and make it user friendly i need some sort of function that will check whether the data entered is numeric not letters. If it is letter then it should say "Please re-enter the number" and read it in again.

Thanks in advance.

Member Avatar for Lioshenka

Sorry for double posting, the browser crashes when i try to edit my post. I think there shouldnt be any problems with decimals, but the program must show the error message if comma or any symbol is entered too.

I had this same problem recently and discovered that in pascal there is a 'Val' function (with 3 parameters, 1st for the string, second for integer and third for error message if the 1st parameter is true) for converting a number into a string....your program would work like this...

Repeat
         write('What is the triangle''s height? ');
         readln(h_string);
         val(h_string, h, Err);
         if Err > 0 then
            writeln('An illegal character was found at character #', Err, '.')
         else if h <= 0 then
            writeln('The height needs to be GREATER than zero.')
      Until (h > 0) AND (Err = 0);

i think you can :
//letter is char of input
case letter of
'0'..'9':
//put error handler here
else
//normal code
end;

or you can check after input complete:
for i:= 1 to Length(strInput) do
if strInput in = false then
begin
//put eror handler here
end;

Member Avatar for Lioshenka

thanks for your help

actually adotl your VAL function is really great, it works for me and now I will (with a bit of luck) pass my IT course :) again thanks so much

thanks for your help

actually adotl your VAL function is really great, it works for me and now I will (with a bit of luck) pass my IT course :) again thanks so much

No problem mate!

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.