Computer Science Canada

Negatives

Author:  AK.E [ Fri Jan 19, 2007 2:10 pm ]
Post subject:  Negatives

I have a program in which I want to get a number but prevent them from entering negatives. I know I can simply use if number < 0 then... but that only works for the first time. Let's say someone enters -5.. it won't except it.. but when they enter 100 then -5 it will because the number will be 95 which is above 0.

Here's my code:
Turing:

var numberOfBars : string
var numberOfBarsInt : int
var barX : int := 0

get numberOfBars                                 


        if strintok (numberOfBars) then       

            numberOfBarsInt := strint (numberOfBars) + barX     

            put numberOfBarsInt                             


        else

            put " "

            put "Please insert a valid integer." 

Author:  Clayton [ Fri Jan 19, 2007 4:02 pm ]
Post subject:  Re: Negatives

use natural numbers instead of integers

Turing:

var my_nat : nat := 5

Author:  Cervantes [ Fri Jan 19, 2007 5:03 pm ]
Post subject:  Re: Negatives

There are strnat and strnatok functions, yes? Using those would solve your problem.


: