Computer Science Canada Code abruptly stopping for no reason. |
Author: | Jari.C [ Thu Mar 20, 2014 10:07 pm ] | ||
Post subject: | Code abruptly stopping for no reason. | ||
What is it you are trying to achieve? I am trying to write a +,- and * quiz. What is the problem you are having? There is a part in the code where it asks for + - or * and when I select addition the code works fine but when I select subtraction the code just suddenly ends. Describe what you have tried to solve this problem I've done general troubleshooting Post any relevant code (You may choose to attach the file instead of posting the code if it is too long) var Level, Level2 : string var answer, counter, num1, num2, num3, num4, wins, answer2 : int counter := 0 wins := 0 %asking what level they want put "Choose multiplaction, subtraction, or addition " .. %getting the level get Level % running the thing they wanted the additon level if Level = "addition" then %looping the program loop % getting the numbers for the randints num1 := Rand.Int (0, 30) num2 := Rand.Int (0, 30) %displaying the addition question put num1, "+", num2 %asking for the question get answer if answer = num1 + num2 then put "You're correct" counter := counter + 1 wins := wins + 1 else put "You were wrong" wins := wins + 0 counter := counter + 1 exit when counter = 20 if Level = "subtraction" then loop num1 := Rand.Int (0, 30) num2 := Rand.Int (0, 30) put num1, "-", num2 get answer if answer = num1 - num2 then put "You're correct" counter := counter + 1 wins := wins + 1 else put "You're wrong" wins := wins + 0 counter := counter + 1 end if end loop put "You won ", wins, " out of 20 games" end if end if end loop end if
Please specify what version of Turing you are using 4.1.1 Edit: I haven't written the * part yet. |
Author: | Raknarg [ Thu Mar 20, 2014 10:14 pm ] |
Post subject: | RE:Code abruptly stopping for no reason. |
Look at your code. There's a huge block of an if statement that says if Level="Addition" then do stuff. Thats it. If you dont write addition, nothing happens. |
Author: | Jari.C [ Thu Mar 20, 2014 10:18 pm ] |
Post subject: | RE:Code abruptly stopping for no reason. |
What do you mean? I also have a if Level = "subtraction" thing, how do I make it so I can choose multiple options. Edit : Sorry for being so new at this |
Author: | Raknarg [ Thu Mar 20, 2014 11:08 pm ] | ||
Post subject: | RE:Code abruptly stopping for no reason. | ||
Look how your code indents:
It will look at this line: if Level = "addition" then If it is true it will run all that stuff inside. If it is not true, it will find the first end if on the same level (the very end of your program) |
Author: | Tony [ Thu Mar 20, 2014 11:48 pm ] | ||||
Post subject: | RE:Code abruptly stopping for no reason. | ||||
Raknarg got it covered. I'd like to share a style that I find useful. It's hard to make sense of deeply nested logic. This part?
Very difficult to make changes to correctly. Indentation takes you a long way there; more so with an editor that will match up the indentation levels... I like to label my terminating statements
Don't have to label everything. Just enough to help you navigate. |
Author: | Jari.C [ Fri Mar 21, 2014 7:13 am ] |
Post subject: | RE:Code abruptly stopping for no reason. |
So is there a way to make it so it will run subtraction or addition? Instead of only running if addition is chosen. |
Author: | Raknarg [ Fri Mar 21, 2014 10:03 am ] |
Post subject: | RE:Code abruptly stopping for no reason. |
If the addition clause fails, then maybe you should include a subtraction clause as well |
Author: | Jari.C [ Fri Mar 21, 2014 10:09 am ] |
Post subject: | RE:Code abruptly stopping for no reason. |
Oh I understand it now. Thank you guys for your help |