Negatives
Author |
Message |
AK.E
|
Posted: 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."
|
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Fri Jan 19, 2007 4:02 pm Post subject: Re: Negatives |
|
|
use natural numbers instead of integers
Turing: |
var my_nat : nat := 5
|
|
|
|
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Fri Jan 19, 2007 5:03 pm Post subject: Re: Negatives |
|
|
There are strnat and strnatok functions, yes? Using those would solve your problem. |
|
|
|
|
![](images/spacer.gif) |
|
|