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

Username:   Password: 
 RegisterRegister   
 what am i doing wrong here?
Index -> Programming, Turing -> Turing Help
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
madry




PostPosted: 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:
code:

var points, final : int := 0%adds points each time you win or lose and keeps track of them during the 15 turns
var guess : string%sees if you guessed T)rue or F)alse nd then checks your answer with the randomizer
var count : int := 0%counts how many turns you have taken and only give 15 turns unless you type and invalid command
var coin : int% represents the coin if you are fliping it
var H, h :string%represents heads on a coin
var T, t : string%represents tails on a coin
var col : int := 4
var centery : int := maxy - 40
var yrad : int := 39
procedure flip
loop
loop
        exit when centery = 0
        cls
        drawoval (maxx div 2, centery, 40, yrad, col)
        drawfill (maxx div 2, centery, col, col)
        centery := centery - 1
        yrad := yrad - 1
        exit when yrad = 0
delay (15)
    end loop
    if col = red then
        col := blue
    elsif col = blue then
        col := red
    end if
    loop
        exit when centery = 0
        cls
        drawoval (maxx div 2, centery, 40, yrad, col)
        drawfill (maxx div 2, centery, col, col)
        centery := centery - 1
        yrad := yrad +1
        delay (5)
        exit when yrad = 40
    end loop
    exit when centery = 0
    delay (15)
    exit when centery = 0
     end loop
end flip
  loop
loop
loop
    randint (coin, 1, 2)%randomizes the copin flipping heads or tails
    flip
    put "I've flipped a coin. H)eads ot T)ails? " ..%asking the question heads or tails
    get guess%getting your guess
    if (guess="H"or guess = "h") and coin=1 then%if its heads then it will see if it is right or wrong
        count := count + 1%taking a turn away because you only have 15 turns
        points := points + 10%adding points if you are right
        put "correct! your score is ", points
         put "this is turn ",count
          delay (1000)
         flip
        elsif (guess="HEADS"or guess = "heads") and coin=1 then%if its heads then it will see if it is right or wrong
        count := count + 1%taking a turn away because you only have 15 turns
        points := points + 10%adding points if you are right
        put "correct! your score is ", points
        put "this is turn ",count
         delay (1000)
        flip
        elsif (guess="t"or guess="T")and coin=2 then
            count := count + 1
            points := points + 10
            put "correct! your score is ",points
            put "this is turn ",count
             delay (1000)
            flip
            elsif (guess="tails"or guess="TAILS")and coin=2 then
            count := count + 1
            points := points + 10
            put "correct! your score is ",points
            put "this is turn ",count
             delay (1000)
            flip
            elsif (guess="h"or guess ="H") and coin =2 then
            count:=count+1
            points:=points-10
            put"you are wrong! your score is ",points
            put"this is turn ",count
             delay (1000)
            flip
            elsif (guess="heads"or guess ="HEADS") and coin =2 then
            count:=count+1
            points:=points-10
            put "you are wrong! your scores is ",points
            put "this is turn ",count
             delay (1000)
            flip
elsif(guess="t"or guess = "T") and coin = 1 then
            count:=count +1
            points:=points - 10
            put "you are wrong! your score is ",points
            put"this is turn ",count
             delay (1000)
            flip
            elsif(guess="tails"or guess = "TAILS") and coin = 1 then
            count:=count +1
            points:=points - 10
            put "you are wrong! your score is ",points
            put"this is turn ",count
             delay (1000)
            flip
            elsif guess="hacker" then
            points:=points+100000000
            count:=count-1
            put"hacks entered you score is now ",points,"!!!"
            else
            put "Bad input ! please choiose H)eads or T)ails "
            put"this is turn ",count
            end if
       end loop
 

        exit when count >= 15
      end loop
  end loop
    final := final + points
    put "the final score is ", final, "/150"
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Sat Aug 09, 2008 3:07 pm   Post subject: RE:what am i doing wrong here?

Next time use code tags.

code:

like this


Turing:

or this


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.
madry




PostPosted: Sat Aug 09, 2008 3:14 pm   Post subject: Re: what am i doing wrong here?

its already in there isent it?
Tony




PostPosted: Sat Aug 09, 2008 3:30 pm   Post subject: RE:what am i doing wrong here?

...
code:

  loop
loop
loop
    randint (coin, 1, 2)%randomizes the copin flipping heads or tails
    flip

code:

procedure flip
loop
loop
        exit when centery = 0


You know your design is probably wrong when you are 5 loops deep into whatever.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Insectoid




PostPosted: 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
code:
centery := maxy-40
in
code:
proc flip
andrew.




PostPosted: 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.
Turing:
if centery = 0 then
    centery := maxy - 40
    yrad := 39
    exit
end if
gitoxa




PostPosted: 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?
andrew.




PostPosted: 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.
Sponsor
Sponsor
Sponsor
sponsor
gitoxa




PostPosted: 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?
andrew.




PostPosted: 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.
CodeMonkey2000




PostPosted: 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.
Saad




PostPosted: 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 Wink
CodeMonkey2000




PostPosted: Mon Aug 18, 2008 3:10 pm   Post subject: RE:what am i doing wrong here?

>_<

Oh well, it's fixed now.
Insectoid




PostPosted: 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.
SNIPERDUDE




PostPosted: 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.
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 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: