Need help with Changing variables
Author |
Message |
supersaiyan1
|
Posted: Thu Nov 30, 2006 8:24 pm Post subject: Need help with Changing variables |
|
|
code: | var score : real
for test_num : 1 .. 5
put "Enter the score for Test ", test_num, "."
loop
get score
exit when score >= 0 and score <= 100
put "The mark is out of range..."
put "Please enter a valid mark."
end loop
put "Your mark for Test ", test_num, " is ", score / 100 : 4 : 1
end for |
k my program
when i enter letter instead of number, it crashes
is there a way to make it stop crashing
if yes can u plz explain me
Code tags added by your friendly neighborhood spiderm... err... mod. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
uberwalla
|
Posted: Thu Nov 30, 2006 8:50 pm Post subject: (No subject) |
|
|
ok i cant really think of it right now but im pretty sure its possible, ima go watch t.v and ill try to think about it a bit. and by the way when u put ur code in ur post plz make use of the code tags and indenting ur code in turing so that it is easier to read. |
|
|
|
|
|
[Gandalf]
|
Posted: Thu Nov 30, 2006 9:07 pm Post subject: (No subject) |
|
|
Welcome to compsci.ca!
The solution to your problem lies in error proofing, which was covered in Cervantes' String Manipulation Tutorial. In essence, you will read in the integer as a string, and then convert that string back to an integer if it doesn't contain any non-numerical characters. Read through it, and you should be able to accomplish what you seek.
And yes, next time make sure to put your code inside [ code ] [ /code ] tags (without the spaces). |
|
|
|
|
|
sweetiechic
|
Posted: Sat Dec 02, 2006 12:49 am Post subject: (No subject) |
|
|
k well this is the way I learned how to bulletproof my work:
code: | procedure getscore(var number: int)
var input: string
loop
get input
exit when strintok (input)
put "Not a possible score. Please enter a test score:"..
end loop
number:= strint(input)
end getscore |
and then when u want to get a score use 'getscore' instead of 'get' |
|
|
|
|
|
Clayton
|
Posted: Sat Dec 02, 2006 2:04 am Post subject: (No subject) |
|
|
that is in essene correct, perhaps you should put that into a function, generally, whenever you can use a function over a procedure, do it. |
|
|
|
|
|
sweetiechic
|
Posted: Sat Dec 02, 2006 8:43 pm Post subject: (No subject) |
|
|
ohh ok, I thought you couldn't output anything in a function though..? and there's a put statement in the procedure |
|
|
|
|
|
Silent Avenger
|
Posted: Sat Dec 02, 2006 9:37 pm Post subject: (No subject) |
|
|
Functions do have outputs, otherwise they wouldn't be of much use. |
|
|
|
|
|
[Gandalf]
|
Posted: Sun Dec 03, 2006 12:42 am Post subject: (No subject) |
|
|
Silent Avenger wrote: Functions do have outputs, otherwise they wouldn't be of much use.
This statement is no doubt confusing.
While it is possible to output something in a function, the main point of a function is to return a value. So instead of having:
code: | proc timesTwo (n : int)
put n, " times two is: ", n * 2
end timesTwo
timesTwo(5) |
You would have:
code: | fcn timesTwo (n : int) : int
result n * 2
end timesTwo
put "5 times two is: ", timesTwo(5) |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
|
|