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

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




PostPosted: Wed Oct 08, 2008 3:16 pm   Post subject: Can any1 help me with this Integer problem please??

Hers the the problem that i got for get postive integers, and i want to change it to get intger, so it can hv negative number

Turing:

function getPositiveInteger : 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

    put "Please enter a positive Integer : " ..
   
    loop
        if hasch then
            getch (ch)

            if ch >= '0' and ch <= '9' then
                number := number * 10 + strint (ch)
                digits := digits + 1
            elsif ch not= ENTER then
                digits := 0
                number := 0
                cls
                put "** Invalid Entry Attempted**"
                put "Please enter an Integer greater than or equal to zero: " ..
            end if

        end if

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

    result number
end getPositiveInteger

% Test Main
var num : int
num := getPositiveInteger
put ""
put "You entered ", num," as your number"
Sponsor
Sponsor
Sponsor
sponsor
Clayton




PostPosted: Wed Oct 08, 2008 3:51 pm   Post subject: RE:Can any1 help me with this Integer problem please??

Look into String manipulation and strintok(). The Turing Walkthrough is an excellent resource for something like this.
Tony




PostPosted: Wed Oct 08, 2008 3:53 pm   Post subject: RE:Can any1 help me with this Integer problem please??

So what's the problem?

Besides checking for digits ch >= '0' and ch <= '9' and ENTER key, you will also need to handle the negative sign.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
chrisc5553




PostPosted: Thu Oct 09, 2008 8:20 am   Post subject: RE:Can any1 help me with this Integer problem please??

The problem is I want to enter negative or postive integers, because the program i got here wont accept negative integers
Tony




PostPosted: Thu Oct 09, 2008 9:51 am   Post subject: RE:Can any1 help me with this Integer problem please??

Turing:

elsif ch = '-' then
   % number has a negative sign
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
chrisc5553




PostPosted: Fri Oct 10, 2008 8:16 am   Post subject: RE:Can any1 help me with this Integer problem please??

Thankyou
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  [ 6 Posts ]
Jump to:   


Style:  
Search: