RSS Forums RSS
Please support our Pascal and Delphi advertiser: Programming Forums

Pascal : Variable for both integer & string?

Join Date: Apr 2007
Location: Glasgow
Posts: 32
Reputation: adotl is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
adotl's Avatar
adotl adotl is offline Offline
Light Poster

Solution Re: Pascal : Variable for both integer & string?

  #2  
Apr 24th, 2007
Seems theres a Val feature that basically converts strings into integers, Str does the opposite. Val in this example also has the third parameter as the error display.
program TriangleWait (input, output);
   var
      b, h: integer; {Base & Height}
      A: real; {Area}
      h_string : string; {Input string for Height}
      Err : integer; {Error flag set by Val()}
   begin
      {------------Introduction & Setup------------------}
      writeln('This program calculates and prints the area of a triangle');
      writeln('after you enter their dimensions.');
      writeln('When asked to, type in a dimension and hit the ENTER key.');
      writeln;
      {-------------------Inputting Data------------------}
      {------User-Friendly------}
      Repeat
         write('What is the triangle''s base? ');
         readln(b);
         if b <= 0 then
            writeln('The base needs to be GREATER than zero.')
      Until b > 0;
      {------VERY User-Friendly------}
      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);
      {-------------------Calculation------------------}
      A := b * h / 2;
      {-------------------Printing Results------------------}
      writeln('The area of a triangle with a base of ', b:0, ' units and');
      writeln('a height of ', h:0, ' units is ', A:0:1, ' square units.'); 
end.
Last edited by adotl : Apr 24th, 2007 at 6:35 pm.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 9:22 pm.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC