Computer Science Canada what am i doing wrong here? |
Author: | madry [ Sat Aug 09, 2008 2:57 pm ] | ||
Post subject: | what am i doing wrong here? | ||
ok i am trying to make a coin flip program that will make my coin flip through a procedure and it will now flip when i want it to? here is my code:
|
Author: | Insectoid [ Sat Aug 09, 2008 3:07 pm ] | ||||
Post subject: | RE:what am i doing wrong here? | ||||
Next time use code tags.
Right now, your problem is that you aren't re-initializing variables. for example, 'centery' get manipulated right at the beginning, and never changes back. try putting 'centery' into your flip procedure. |
Author: | madry [ Sat Aug 09, 2008 3:14 pm ] |
Post subject: | Re: what am i doing wrong here? |
its already in there isent it? |
Author: | Tony [ Sat Aug 09, 2008 3:30 pm ] | ||||
Post subject: | RE:what am i doing wrong here? | ||||
...
You know your design is probably wrong when you are 5 loops deep into whatever. |
Author: | Insectoid [ Sat Aug 09, 2008 5:24 pm ] | ||||
Post subject: | RE:what am i doing wrong here? | ||||
I've been 5 loops/for loops deep before, though not very often. you need
|
Author: | andrew. [ Fri Aug 15, 2008 10:29 pm ] | ||
Post subject: | RE:what am i doing wrong here? | ||
Instead of putting "exit when", use an if statement. That way you can have it do something when the condition is met. You should use something like this.
|
Author: | gitoxa [ Fri Aug 15, 2008 11:10 pm ] |
Post subject: | RE:what am i doing wrong here? |
What's wrong with 'exit when ... ' ? And how does that help him in any way, shape, or form? |
Author: | andrew. [ Sat Aug 16, 2008 1:10 pm ] |
Post subject: | RE:what am i doing wrong here? |
Because when he uses "exit when", he can't re-initialize the variables. That's what's causing him the problem. |
Author: | gitoxa [ Sat Aug 16, 2008 6:50 pm ] |
Post subject: | RE:what am i doing wrong here? |
Why not? Stick them right after the end of the loop. Besides, isn't the point of initializing that it happens before anything is done? |
Author: | andrew. [ Sun Aug 17, 2008 3:14 pm ] |
Post subject: | RE:what am i doing wrong here? |
I guess you're right. You can do it both ways. |
Author: | CodeMonkey2000 [ Sun Aug 17, 2008 7:10 pm ] |
Post subject: | RE:what am i doing wrong here? |
Your problem is that the variables used in your procedure are global. This means that what ever the value of the variable is when it exited the procedure, it will stay the same when you reenter the procedure the next time. For example:centery starts out with the value maxy - 40. When you exit the procedure it will have the value: 0, since that is the exit condition. When it enters the procedure again it still has the value 0, so it exits immediately. Try declaring all the variables used in your procedure, within your procedure. That means that those variables are only known within that procedure, ans so are local variables. Local variables get destroyed once you exit the procedure, and get created when you enter them. |
Author: | Saad [ Mon Aug 18, 2008 3:59 am ] |
Post subject: | Re: RE:what am i doing wrong here? |
CodeMonkey2000 @ Sun Aug 17, 2008 7:10 pm wrote: ans so are private variables. Private members get destroyed once you exit the procedure, and get created when you enter them.
Typo, I believe that was supposed to say local and local variables ![]() |
Author: | CodeMonkey2000 [ Mon Aug 18, 2008 3:10 pm ] |
Post subject: | RE:what am i doing wrong here? |
>_< Oh well, it's fixed now. |
Author: | Insectoid [ Mon Aug 18, 2008 4:35 pm ] |
Post subject: | RE:what am i doing wrong here? |
Hmm, I always use global variables and re-initialize at the procedure, but local variables looks quicker and easier! I never really experimented with them. |
Author: | SNIPERDUDE [ Mon Aug 18, 2008 8:28 pm ] |
Post subject: | RE:what am i doing wrong here? |
You tend to learn more about global and local variables more when learning other languages... Most people (noobs rather) don't really know the difference. |
Author: | gitoxa [ Mon Aug 18, 2008 11:02 pm ] |
Post subject: | Re: RE:what am i doing wrong here? |
insectoid @ Mon Aug 18, 2008 4:35 pm wrote: Hmm, I always use global variables and re-initialize at the procedure, but local variables looks quicker and easier! I never really experimented with them.
Be sure to look into parameter passing. ![]() |
Author: | SNIPERDUDE [ Tue Aug 19, 2008 9:29 am ] |
Post subject: | RE:what am i doing wrong here? |
YES! Very effective and useful too. |