User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Pascal and Delphi section within the Software Development category of DaniWeb, a massive community of 391,913 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,708 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Pascal and Delphi advertiser:
Views: 1788 | Replies: 3 | Solved
Reply
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

Pascal : Variable for both integer & string?

  #1  
Apr 24th, 2007
I was doing some coding today and came across a problem someone else faced and I couldnt answer it for them. I'll explain the concept in the little program below....

 Program between;
uses crt;
Var
   value: integer;

Begin
     clrscr;
     writeln('Please enter value');
     readln(value);
     readln
End.


...Basically what the programmer wanted to do was do an error message if a word or even a char was entered instead of a number so the program doesnt crash like how its setup now.

I started to think well an easy way would be if there was a variable type that stored both integer, char and string. But I dont think there is one?

So the only idea I had was to do an IF Else statement where ELSE displays the error message and asks the user to enter a number this time. However since the value here is stored as an integer I wasnt sure how to get around this.

Any ideas/help would be greatly appreciated.
Last edited by adotl : Apr 24th, 2007 at 10:57 am.
AddThis Social Bookmark Button
Reply With Quote  
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 5:35 pm.
Reply With Quote  
Join Date: Dec 2006
Location: Romania
Posts: 171
Reputation: radu84 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 16
radu84 radu84 is offline Offline
Junior Poster

Re: Pascal : Variable for both integer & string?

  #3  
Apr 25th, 2007
i haven't worked with pascal since 2002, now i'm a delphi happy programmer. in delphi exist what you want, data type is called "variant".
Reply With Quote  
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

Re: Pascal : Variable for both integer & string?

  #4  
Apr 25th, 2007
hehe well unfortunately I dont have a choice at the moment, this year my course is using Pascal
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb Pascal and Delphi Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the Pascal and Delphi Forum

All times are GMT -4. The time now is 7:56 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC