Computer Science Canada

Can any1 help me with this Integer problem please??

Author:  chrisc5553 [ 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"

Author:  Clayton [ 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.

Author:  Tony [ 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.

Author:  chrisc5553 [ 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

Author:  Tony [ 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

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

Thankyou


: