How to change the type of a variable

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
SoKeT
Newbie
Posts: 6
Joined: Thu Jun 14, 2012 1:48 pm

How to change the type of a variable

Post by SoKeT »

Hi. I'm doing a clock wich first shows the current time and then it keeps working until you press ESC.

h$ = MID$(TIME$, 1, 2) 'Gets the value of the hour
x = COS((45 - h$) * 6 * 3.1415926# / 180)
y = SIN((45 - h$) * 6 * 3.1415926# / 180)
LINE (320, 240)-(320 - 175 * x, 240 +175 * y), 0 'Creates a line from the middle, which indicates the hour

The problem I have is in this piece of code. When I try to run the program, it gives me an error in the bolded part. If I put "h" instead of "h$" then the error is not showed, but then it would be different from the variable that gets the value of the hour. But if it is not string it gives me an error too.

So now that I've explained the problem, I'm looking for an instruction that lets me change the type of a variable.

Hope you guys can understand my question. If you don't, you can ask me for more details.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

h$ is a string value that cannot be read by SIN or COS. Use VAL to convert the hour value to a number.

Code: Select all

h = VAL(MID$(TIME$, 1, 2)) 'Gets the value of the hour
x = COS((45 - h) * 6 * 3.1415926# / 180)
y = SIN((45 - h) * 6 * 3.1415926# / 180)
LINE (320, 240)-((320 - 175) * x, (240 + 175) * y), 10 'Creates a line from the middle, which indicates the hour
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
SoKeT
Newbie
Posts: 6
Joined: Thu Jun 14, 2012 1:48 pm

Post by SoKeT »

Thank you very much, that solved my problem.
SoKeT
Newbie
Posts: 6
Joined: Thu Jun 14, 2012 1:48 pm

Post by SoKeT »

Hi again. I have a new problem, and this is the only thing that separates me from having my program completely done. I'll put the part of the code where I have the problem:

Code: Select all

DO
LINE (320, 240)-(320 - 150 * xs, 240 + 150 * ys), 0     'Erases the previous seconds hand
s$ = MID$(TIME$, 7, 2)
s = VAL(s$)
xs = COS((45 - s) * 6 * 3.1415926# / 180)
ys = SIN((45 - s) * 6 * 3.1415926# / 180)
LINE (320, 240)-(320 - 150 * xs, 240 + 150 * ys)     'Draws the seconds hand with 6 more degrees (1 second more)
LOOP UNTIL INKEY$ = CHR$(27)     'Loops until you press Esc
You have to try this code to understand my problem. When you run it, you see the seconds hand moving each second, like a real clock. The problem is, it is blinking all the time, and that doesn't look nice. I tried to solve it this way:

Code: Select all

DO
LINE (320, 240)-(320 - 150 * xs, 240 + 150 * ys), 0     'Erases the previous seconds hand
FOR i = 1 TO 2000
NEXT i
s$ = MID$(TIME$, 7, 2)
s = VAL(s$)
xs = COS((45 - s) * 6 * 3.1415926# / 180)
ys = SIN((45 - s) * 6 * 3.1415926# / 180)
LINE (320, 240)-(320 - 150 * xs, 240 + 150 * ys)     'Draws the seconds hand with 6 more degrees (1 second more)
FOR i = 1 TO 2000
NEXT i
LOOP UNTIL INKEY$ = CHR$(27)     'Loops until you press Esc
Both FOR-NEXT makes the loop go slower, so it just blinks once, according to the time it lasts to move the seconds hand. Try this one too.

What I'd like to get is the hand moving without blinking. I think I could solve this if there was an instruction to erase a previous drawed LINE, in which case I'd like to know the name of it. I can't think of any other way of solving this, so please if you know what I could do, tell me.

EDIT: I forgot to comment something. With the second code, sometimes it skips 1 second (it jumps from 30 to 32, for example). And sometimes it draws the same second twice.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

Instead of the FOR delay loop, try a DO loop like this:


DO
LOOP UNTIL MID$(TIME$, 7, 2) <> S$

This loop tells the program when the second value has changed.

BTW, those kind of counter delay loops will vary on every computer. Use a TIMER loop instead when creating program delays. Then every computer will execute the delay the same amount of time according to the internal clock.

Code: Select all

delay = 1 'seconds
start! = TIMER
DO WHILE start! + delay! >= TIMER
IF start! > TIMER THEN start! = start! - 86400
LOOP
The 86400 value is the number of seconds in a day and is used to correct for midnight TIMER values of 0.

Use the top loop for a clock as TIMER values may vary from the actual TIME$ values.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
SoKeT
Newbie
Posts: 6
Joined: Thu Jun 14, 2012 1:48 pm

Post by SoKeT »

Okay, I solved it myself. I thank you for your last advice, I'm sure it will help me in the future. Here is how I solved it:

Code: Select all

DO
LINE (320, 240)-(320 - 150 * xs, 240 + 150 * ys), 0     'Erases the previous seconds hand
s$ = MID$(TIME$, 7, 2)
s = VAL(s$)
xs = COS((45 - s) * 6 * 3.1415926# / 180)
ys = SIN((45 - s) * 6 * 3.1415926# / 180)
LINE (320, 240)-(320 - 150 * xs, 240 + 150 * ys)     'Draws the seconds hand with 6 more degrees (1 second more)
SLEEP 1
LOOP UNTIL INKEY$ = CHR$(27)     'Loops until you press Esc
As simple as a SLEEP, it just erases the LINE once per second, so the transition is smoth.

Thank you very much for your responses. Best regards.

EDIT: In the original code, the loop is "UNTIL s = 0", but I wrote it like that here so you could easily exit the program if you tried it.
User avatar
burger2227
Veteran
Posts: 2466
Joined: Mon Aug 21, 2006 12:40 am
Location: Pittsburgh, PA

Post by burger2227 »

A key press will interrupt SLEEP.
Please acknowledge and thank members who answer your questions!
QB64 is a FREE QBasic compiler for WIN, MAC(OSX) and LINUX : https://www.qb64.org/forum/index.php
Get my Q-Basics demonstrator: https://www.dropbox.com/s/fdmgp91d6h8ps ... s.zip?dl=0
Post Reply