Using parameters -- Infinite Loops
Author |
Message |
ToTNuim
|
Posted: Sat Mar 13, 2010 11:41 am Post subject: Using parameters -- Infinite Loops |
|
|
For some reason, my code does not accept the parameters (20, 200) or any parameter that's not (negative, positive) and will go into a infinite loop.
Please help!
I just started computer science and I don't know how to "look for problems" in codes!
Turing: |
function getInteger (low, high : int) : int
const ENTER : char := chr (10)
var ch : string (1) := 'x'
var number : int := 0
var digits : int := 0
var Negative : boolean := false
put "Please enter an Integer between ", low, " and ", high, " : " ..
loop
loop
if hasch then
getch (ch )
if ch >= '0' and ch <= '9' then
number := number * 10 + strint (ch )
digits := digits + 1
put ch ..
elsif ch = '-' and digits = 0 then
Negative := true
digits := digits + 1
put ch ..
elsif ch not= ENTER then
digits := 0
number := 0
cls
put "** This is not a integer **"
put "Please enter an Integer: " ..
end if
end if
exit when number >= low and number <= high
digits := 0
number := 0
Negative := false
cls
put "** This number is not between ", low, " and ", high, " **"
put "Please re-enter: " ..
end loop
exit when ch = chr (10) or digits >= 9 and number >= low and number <= high
end loop
if Negative then
result number * - 1
else
result number
end if
end getInteger
% Test Main
var num : int
var win : int
win := Window.Open ("screen:10;70,cursor,echo")
num := getInteger (20, 200)
put ""
put "You entered ", num, " as your number"
|
Please specify what version of Turing you are using
Turing 4.1.1 |
|
|
|
|
|
Sponsor Sponsor
|
|
|
DtY
|
Posted: Sat Mar 13, 2010 11:55 am Post subject: RE:Using parameters -- Infinite Loops |
|
|
Is that big loop just reading a number?
You can already do that:
Turing: | var number: int
get number |
|
|
|
|
|
|
USEC_OFFICER
|
Posted: Sat Mar 13, 2010 5:09 pm Post subject: RE:Using parameters -- Infinite Loops |
|
|
You don't need hasch. A simple get statement is what you need. hasch only works if there is input. If not, the program will skip over that part, without stopping. That could be the problem.
Also like DtY says, you already have a get statement. Why replicate it? (Which is what you are doing) |
|
|
|
|
|
ToTNuim
|
Posted: Sat Mar 13, 2010 5:54 pm Post subject: Re: Using parameters -- Infinite Loops |
|
|
Yeah, a "get" statement works
But, the assignment was that I use this code and add "parameters" to it.
Anymore suggestions? |
|
|
|
|
|
TheGuardian001
|
Posted: Sat Mar 13, 2010 6:05 pm Post subject: Re: Using parameters -- Infinite Loops |
|
|
What initial value is given to the variable number (right before the start of your loop)? Will this number ever be between low and high when low is positive? |
|
|
|
|
|
USEC_OFFICER
|
Posted: Sat Mar 13, 2010 7:01 pm Post subject: RE:Using parameters -- Infinite Loops |
|
|
So the problem is to figure out how to modify the code to add parameters? |
|
|
|
|
|
TheGuardian001
|
Posted: Sat Mar 13, 2010 7:13 pm Post subject: Re: Using parameters -- Infinite Loops |
|
|
Did you read the initial post where it said that the problem was that it wouldn't accept parameters other than (negative number, positive number)? |
|
|
|
|
|
Clayton
|
Posted: Sat Mar 13, 2010 10:34 pm Post subject: RE:Using parameters -- Infinite Loops |
|
|
To further expand on TheGuardian001's hint: At various points in your code, output the value of each of your variables, see what they are, and what the resulting action in your code is because of that. You should find your answer. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
ToTNuim
|
Posted: Sun Mar 14, 2010 11:08 am Post subject: Re: Using parameters -- Infinite Loops |
|
|
Oh! Now I see! Thank you TheGuardian001 and Clayton !
It WAS my initial value for "number".
But ... ,instead of zero, what should I put as my initial? |
|
|
|
|
|
USEC_OFFICER
|
Posted: Sun Mar 14, 2010 3:36 pm Post subject: RE:Using parameters -- Infinite Loops |
|
|
What ever number works of course!
@TheGuardian001: I didn't read it fully. |
|
|
|
|
|
|
|