Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Discussion / Even numbers only

Author
Message
learner
16
Years of Service
User Offline
Joined: 7th Mar 2009
Location:
Posted: 12th Mar 2009 23:00
In my programme i want the user to enter a number up to 100.
How can i ensure the user is only able to enter even numbers?.

Thanks
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 12th Mar 2009 23:47
Since all even numbers are evenly divisible by 2, we can take advantage of DarkBASIC removing the decimals from normal variables like so (ughhh that sounded WAY too much like a textboox):



Because if you put in 3, a will read as 1 but b# will read as 1.5, so they will trigger the ELSE case. However if, say, 4 is entered, a will read 2 and b# will read 2. Since they are the same, it will trigger the if condition.

\textbook (gotta go play Halo or something to snap out of this)

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
learner
16
Years of Service
User Offline
Joined: 7th Mar 2009
Location:
Posted: 13th Mar 2009 00:54
Oh yes..

Thanks very much.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 13th Mar 2009 22:21
nice bn2
learner
16
Years of Service
User Offline
Joined: 7th Mar 2009
Location:
Posted: 13th Mar 2009 23:44
Well why won`t this work

bak:
cls 0
input number
a=number/2
b#=number/2
if a=b
print "even"
else
print "odd"
endif
wait 2000
goto bak;

Thanks

Attachments

Login to view attachments
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 13th Mar 2009 23:59
it wouldnt work cuz u wrote if a=b rather than a=b#, the # means it can be a decimal and b# is different than b

neway u could simplify it even more by changing this:



for this:

Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 14th Mar 2009 00:35
nice one smart guy hahaha

New Site! Check it out \/
learner
16
Years of Service
User Offline
Joined: 7th Mar 2009
Location:
Posted: 14th Mar 2009 00:39
That won`t work either.
Real numbers seem to be ignored.
I also tried it with the hash.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 14th Mar 2009 02:30
Here's a fast and maybe sneaky way of doing it:



The & sign is a binary AND comparison. In this case, it checks whatever value 'a' has to see if it contains 1 as bit 0. A number is odd if bit 0 is set to 1.

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 14th Mar 2009 02:54
if it works then very nice latch!!!!!! thats almost genius!!!!
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 14th Mar 2009 03:10 Edited at: 14th Mar 2009 03:57
oh and you could make your own input command that only exepted even numbers under 100. so the user wouldn't be able to enter anything else its not to hard.

you would just get the inkey$() then make sure its one of your numbers that are "Approved" and then add inkey$() to your variable(You could do this in a function so it would be the same as the input command) of course you would need a counter otherwise you will do this hundreds of times of a second(Depending on your fps)

Heres a small example:



I couldn't get timer to work i don't know why so you might have to change the counter variables to get the timing right. but that is functional and has backspace and stuff. you could make the last "|" character flash(like in windows) i have done it before but i didnt feel it was needed. be sure to add the if keystate(14) and keystate(15) are equal to zero otherwise you will come out with a block character :/ aaahh lol

New Site! Check it out \/
learner
16
Years of Service
User Offline
Joined: 7th Mar 2009
Location:
Posted: 14th Mar 2009 03:49
YES It works


Thanks Latch
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 16th Mar 2009 08:28
Quote: "you would just get the inkey$() then make sure its one of your numbers that are "Approved" and then add inkey$() to your variable"


That would work, though it might be better to use the entry$() command, as it is a little better than inkey$().

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 16th Mar 2009 20:24
never herd of it. lol i tried it but how do you clear it? because it stores everything you write.

New Site! Check it out \/
Quirkyjim
16
Years of Service
User Offline
Joined: 18th Oct 2008
Location: At my computer
Posted: 16th Mar 2009 22:20
Quote: "but how do you clear it?"




~QJ
That's what they WANT you to think...
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 16th Mar 2009 23:44
oh well that makes sense thanks

New Site! Check it out \/
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Mar 2009 04:17
@learner
if num/2 = int(num/2)
didn't work because DB requires you to use a decimal point in the number you're dividing by for it to return a real number.
if num/2. = int(num/2.)
would work fine, you dont even need the 0 after the point like ive done here.

The Universe has been erased by a mod because it was larger
than 240x80 pixels.
BN2 Productions
21
Years of Service
User Offline
Joined: 22nd Jan 2004
Location:
Posted: 18th Mar 2009 10:26
I have heard that int() is a very slow command? Wouldn't this cause a performance hit if it is being calculated quickly?

Great Quote:
"Time...LINE??? Time isn't made out of lines...it is made out of circles. That is why clocks are round!" -Caboose
Pillarofire
21
Years of Service
User Offline
Joined: 31st Dec 2003
Location: Good Question, <looks around.>
Posted: 18th Mar 2009 13:38 Edited at: 18th Mar 2009 13:39
Try this:

Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 18th Mar 2009 16:31
@bn2

i heard a lot of the dbc math commands were slow! lol

@pillaroffire

nice but is mod a dbp command i have never herd of it.

this is the dbc board haha we can't use the Variable as whatever thats dbp

New Site! Check it out \/
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 18th Mar 2009 19:50
@Caleb
MOD() returns the remainder of a division, ie
mod(7,2) would be 1 because 7/2 is 3 remainder 1. anyway like you said you can't use it in DBC which is a shame as its quite a basic math function.
You could always make one yourself but I can't see it being any faster than int().

To save time we could store the outcome of num/2.0 in a variable then DB only has to calculate it once...


The Universe has been erased by a mod because it was larger
than 240x80 pixels.
Latch
18
Years of Service
User Offline
Joined: 23rd Jul 2006
Location:
Posted: 18th Mar 2009 20:48
I'm getting cranky in my old age...

You can dump the functions and the declared floats and use this faster variation of what That1Smart Guy and Obese87 wrote:

if n/2.0 = n/2



But I think the fastest and most efficient way is

if n & 1 : rem the result is odd

Enjoy your day.
That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 18th Mar 2009 21:40
does the & really work, did ne1 test it?
Pillarofire
21
Years of Service
User Offline
Joined: 31st Dec 2003
Location: Good Question, <looks around.>
Posted: 19th Mar 2009 05:06
Oops, my bad. I thought this was on the DBPro board.
Caleb1994
16
Years of Service
User Offline
Joined: 10th Oct 2008
Location: The Internet you idiot!
Posted: 19th Mar 2009 05:14
why don't you test it and see hahaha

New Site! Check it out \/
Libervurto
18
Years of Service
User Offline
Joined: 30th Jun 2006
Location: On Toast
Posted: 19th Mar 2009 08:02
Sorry Latch didn't see your binary number wang up there

The Universe has been erased by a mod because it was larger
than 240x80 pixels.
TheComet
17
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 19th Mar 2009 18:09
Quote: "if n & 1 : rem the result is odd"


Sneaky and clever... Where did you learn that?

TheComet

Peachy, and the Chaos of the Gems

That1Smart Guy
16
Years of Service
User Offline
Joined: 26th Feb 2009
Location: Somewhere...... yep
Posted: 19th Mar 2009 19:33
yep & works, very nice latch

Login to post a reply

Server time is: 2025-05-16 12:01:33
Your offset time is: 2025-05-16 12:01:33