Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Can anyone help me with this real# problem??
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chrisc5553




PostPosted: Fri Oct 10, 2008 8:26 am   Post subject: Can anyone help me with this real# problem??

Here is the program that i got for getting integer, but im hving trouble with changing it to get Real number.
Turing:

function getInteger : int
   const ENTER  : char := chr (10) % Constant ASCII code for the Enter Key
   var   ch     : string (1) := 'x'
   var   number : int := 0
   var   digits : int := 0
   var   negative : boolean := false
   
   put "Please enter a Integer : " ..
   
   loop
      if hasch then
         getch (ch)
         
         if negative = true then
            number := -1 * strint(ch) + number * 10 
            digits := digits + 1
           
         elsif negative = false then
            if ch >= '0' and ch <= '9' then
               number := number * 10 + strint (ch)
               digits := digits + 1
           
            elsif ch = '-' and digits = 0 then
               negative := true
               digits := digits + 1
           
            elsif ch not= ENTER then
               digits := 0
               number := 0
               cls
               put "** Invalid Entry Attempted**"
               put "Please enter any Integer: " ..
            end if
         
         elsif ch not= ENTER then
            digits := 0
            number := 0
            cls
            put "** Invalid Entry Attempted**"
            put "Please enter any Integer: " ..
         end if

   end if 
   exit when ch = chr (10) or digits >= 9         
   end loop     result number

end getInteger

% Test Main
var num : int
num := getInteger
put ""
put "You entered ", num," as your number"


Mod Edit: Code Tags! Last warning.
Sponsor
Sponsor
Sponsor
sponsor
CodeMonkey2000




PostPosted: Fri Oct 10, 2008 9:57 am   Post subject: RE:Can anyone help me with this real# problem??

Your program still crashes for negative integers. For real numbers you just ignore the decimal. and use strreal.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 2 Posts ]
Jump to:   


Style:  
Search: