Computer Science Canada

Need Help With My Turing Quiz Please

Author:  Gildemore [ Wed Jan 21, 2009 1:10 am ]
Post subject:  Need Help With My Turing Quiz Please

Im pretty new to turing and this is for my grade 10 project. I decided to make a small math quiz game thing (some of the answers to the questions are wrong cause i havn't edited them yet) anyways so everything works (i hope) except for the elsif's that i have. cause at the beggining you have a choice of either 1,2,3 or 4 which are difficulties so i used elsif for each one but i guess they are too far apart and didnt connect or whatever. please look at my code and help? thank you all


Turing:
var z : char
var a,b,c,d,e : char
var x : int := 0
const y:=0

put "Welcome to Theodore's math quiz. "
put "Choose a difficulty from 1-4. "

get z

if z = "1" then
put "What is 3+29?"
get a
if a = "32" then
x:=x+1
else x:=x
put "What is 9+99999?"
get b
if b = "100008" then
x:=x+1
else x:=x
put "What is 856-345?"
get c
if c = "511" then
x:=x+1
else x:=x
put "What is 23-765?"
get d
if d = "-742" then
x:=x+1
else x:=x
put "What is 29+34-9?"
get e
if e = "54" then
x:=x+1
else x:=x

put "Your mark is ", x, "out of 5. That is ", 20*x, "percent. Thanks for demonstrating your math skills to a heartless machine."


elsif z = "2" then
put "What is 9*8?"
get a
if a = "72" then
x:=x+1
else x:=x
put "What is 16*5?"
get b
if b = "80" then
x:=x+1
else x:=x
put "What is 800/5?"
get c
if c = "160" then
x:=x+1
else x:=x
put "What is 20/(-4)?"
get d
if d = "-5" then
x:=x+1
else x:=x
put "What is 9*657?"
get e
if e= "5913" then
x:=x+1
else x:=x

put "Your mark is ", x, "out of 5. That is ", 20*x, "percent. Thanks for demonstrating your math skills to a heartless machine."
elsif z = "3" then
put "What is 2^4?"
get a
if a = "16" then
x:=x+1
else x:=x
put "What is 12^7?"
get b
if b = "35831808" then
x:=x+1
else x:=x
put "What is the square root of 256?"
get c
if c = "16" then
x:=x+1
else x:=x
put "What is the square root of 144?"
get d
if d = "12" then
x:=x+1
else x:=x
put "What is the square root of 1296?"
get e
if e = "36" then
x:=x+1
else x:=x
put "Your mark is ", x, "out of 5. That is ", 20*x, "percent. Thanks for demonstrating your math skills to a heartless machine."

elsif z = "4" then
put "What is tan A when a=6 and b=3?"
get a
if a = "2" then
x:=x+1
put "What is sin A when a=1 and b=1?"
get b
if b = "45" then
x:=x+1
put "What is cosA when a=1 and c=1?"
get c
if c = "90" then
x:=x+1
put "What is tanA when b=5 and c= 10?"
get d
if d = "60" then
x:=x+1
put "What is sinC when c=45 and a=45?"
get e
if e = "45" then
x:=x+1

put "Your mark is ", x, "out of 5. That is ", 20*x, "percent. Thanks for demonstrating your math skills to a heartless machine."

else put "That is not a choice, restart"



end if
end if
end if
end if


Mod Edit: Remember to use syntax tags! Thanks Smile
code:
[syntax="turing"]Code Here[/syntax]

Author:  Gildemore [ Wed Jan 21, 2009 1:40 am ]
Post subject:  Re: Need Help With My Turing Quiz Please

okay i fixed that problem but now after it asks the first question of any difficulty it auto skips to like the third or fourth question and the mark you get is always 0 so im not sure whats going on any help? heres the new code


Turing:
var z : char
var a,b,c,d,e : char
var x : int := 0
const y:=0

put "Welcome to Theodore's math quiz. "
put "Choose a difficulty from 1-4. "

get z

if z = "1" then
put "What is 3+29?"
get a
if a = "32" then
x:=x+1
else x:=x
put "What is 9+99999?"
get b
if b = "100008" then
x:=x+1
else x:=x
put "What is 856-345?"
get c
if c = "511" then
x:=x+1
else x:=x
put "What is 23-765?"
get d
if d = "-742" then
x:=x+1
else x:=x
put "What is 29+34-9?"
get e
if e = "54" then
x:=x+1
else x:=x
put "Your mark is ", x, "out of 5. That is ", 20*x, "percent. Thanks for demonstrating your math skills to a heartless machine."
end if
end if
end if
end if
end if

elsif z = "2" then
put "What is 9*8?"
get a
if a = "72" then
x:=x+1
else x:=x
put "What is 16*5?"
get b
if b = "80" then
x:=x+1
else x:=x
put "What is 800/5?"
get c
if c = "160" then
x:=x+1
else x:=x
put "What is 20/(-4)?"
get d
if d = "-5" then
x:=x+1
else x:=x
put "What is 9*657?"
get e
if e= "5913" then
x:=x+1
else x:=x
put "Your mark is ", x, "out of 5. That is ", 20*x, "percent. Thanks for demonstrating your math skills to a heartless machine."
end if
end if
end if
end if
end if
elsif z = "3" then
put "What is 2^4?"
get a
if a = "16" then
x:=x+1
else x:=x
put "What is 12^7?"
get b
if b = "35831808" then
x:=x+1
else x:=x
put "What is the square root of 256?"
get c
if c = "16" then
x:=x+1
else x:=x
put "What is the square root of 144?"
get d
if d = "12" then
x:=x+1
else x:=x
put "What is the square root of 1296?"
get e
if e = "36" then
x:=x+1
else x:=x
put "Your mark is ", x, "out of 5. That is ", 20*x, "percent. Thanks for demonstrating your math skills to a heartless machine."
end if
end if
end if
end if
end if
elsif z = "4" then
put "What is tan A when a=6 and b=3?"
get a
if a = "2" then
x:=x+1
else x:=x
put "What is sin A when a=1 and b=1?"
get b
if b = "45" then
x:=x+1
else x:=x
put "What is cosA when a=1 and c=1?"
get c
if c = "90" then
x:=x+1
else x:=x
put "What is tanA when b=5 and c= 10?"
get d
if d = "60" then
x:=x+1
else x:=x
put "What is sinC when c=45 and a=45?"
get e
if e = "45" then
x:=x+1
else x:=x
put "Your mark is ", x, "out of 5. That is ", 20*x, "percent. Thanks for demonstrating your math skills to a heartless machine."
end if
end if
end if
end if
end if
else put "That is not a choice, restart"
end if



Mod Edit: Syntax tags
code:
[syntax="turing"]Code Here[/syntax]

Author:  Zren [ Wed Jan 21, 2009 3:50 am ]
Post subject:  Re: Need Help With My Turing Quiz Please

Please wrap all your code in:
[ code ] ... [ /code ]

As for you program, it's all in the variable declaration. A char data type only represents 1 character. Now I believe they're only 1 byte in size. So you organized a,b,c,d,e in memory all together.

code:

Memory:
[A][B][C][D][E]


Now when you asked for A with get a, which returned a string. For a string, each letter is 2 bytes in size (I think, at least it proves this theory). So what do we get?

If we enter the answer to Diff level 1 first question "3+29", which has the answer "32". The answer would contain 2 letters, however since it's a string, it would be 4 bytes in size. So here's the problem. The command get sorta has a memory, so if it doesn't get rid of all the data the user inputs, it will hold onto it for each proceeding get command until it's empty. Like so...

code:

get a

Memory:
[A][A][A][A]
-V-
[A][B][C][D][E]

get b

Memory:
[A][A][A][A]
   -V-
[A][B][C][D][E]

get c

Memory:
[A][A][A][A]
      -V-
[A][B][C][D][E]

get d

Memory:
[A][A][A][A]
         -V-
[A][B][C][D][E]


So when we finally get to Question E, we have to get more user input to continue.

code:

get e

Memory:
            [E]
            -V-
[A][B][C][D][E]



This here is also the problem when you have the 0 out of 5 at the end. Since it's always looking at the wrong part of memory.


The way to fix this is to just declare a,b,c,d,e as :string.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Another thing.

x := x

What the hell is this? It's kinda useless. Your telling a variable to stay the same. So why put it in there. If you must, comment it and put "//Score stays the same" or something. If your teacher told you to do this...uhg.

Another thing, you don't really need to embed all the if statements. Unless it's not suppose to ask question 2 unless it's question 1 is done...Okay I see where you went with this, but it's just alot easier to put it like so:

get a
if a = right
score +1
end if

get b
...etc.


: