Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 loop
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
jayshaw94




PostPosted: Thu Apr 21, 2011 12:03 pm   Post subject: loop

When running this code below. If the person types in an answer that is within the limit (0,0.5,1,2)
I still get the error message. What am I doing wrong?
Thanks in advance!

var nojp:int
var op1:real
procedure OpeningDeductions
put "Enter the number of Jumbo post(s) required"
get nojp
put "Enter either 0, 0.5, 1, or 2"
loop
get op1
if op1 not = 0 or op1 not = 0.5 or op1 not = 1 or op1 not = 2 then
put "Error please enter the following 0, 0.5, 1, or 2"
put op1
end if
exit when op1 = 0 or op1 = 0.5 or op1 = 1 or op1 =2
end loop
end OpeningDeductions
Sponsor
Sponsor
Sponsor
sponsor
Tony




PostPosted: Thu Apr 21, 2011 12:19 pm   Post subject: RE:loop

Because testing for equality of real values is a bad idea
http://en.wikipedia.org/wiki/Floating_point#IEEE_754:_floating_point_in_modern_computers
Quote:

To a rough approximation, the bit representation of an IEEE binary floating-point number is proportional to its base 2 logarithm, with an average error of about 3%
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
jayshaw94




PostPosted: Thu Apr 21, 2011 12:30 pm   Post subject: Re: loop

sorry Tony not sure what you mean. The program runs but I still get the error message if for instance i type in 5 I'll get the error message as I should. But if i type in one of the options available I get the error message but it continues onto the next line of code
Insectoid




PostPosted: Thu Apr 21, 2011 12:46 pm   Post subject: RE:loop

Say someone enters 1. Now we check.

Does it equal 0? No.
Does it equal 0.5? No.
Does it equal 1? Yes!
The test fails, and we therefore get your error message.

'or' is obviously not working here. Try something else.
Tony




PostPosted: Thu Apr 21, 2011 12:47 pm   Post subject: RE:loop

oh, that.
code:

op1 not = 0 or op1 not = 0.5 or op1 not = 1 or op1 not = 2

is guaranteed to always be true, for any value entered.

edit: damn you Insectoid!
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




PostPosted: Thu Apr 21, 2011 12:48 pm   Post subject: RE:loop

I win again!
jayshaw94




PostPosted: Thu Apr 21, 2011 1:48 pm   Post subject: Re: loop

thanks guys what other syntax would i be able to use?
Tony




PostPosted: Thu Apr 21, 2011 2:00 pm   Post subject: RE:loop

OR could work, if you ask the right question. Keep in mind that this is a mathematical OR. In speech "or" often means "xor" instead, thus the confusion.

Some references
or
xor
and
not
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Sponsor
Sponsor
Sponsor
sponsor
jayshaw94




PostPosted: Thu Apr 21, 2011 3:27 pm   Post subject: Re: loop

Thank you Tony I'll look into it some more
thanks again
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: