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

Username:   Password: 
 RegisterRegister   
 Code abruptly stopping for no reason.
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Jari.C




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

Turing:





Please specify what version of Turing you are using
4.1.1



Edit: I haven't written the * part yet.
Sponsor
Sponsor
Sponsor
sponsor
Raknarg




PostPosted: 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.
Jari.C




PostPosted: 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 Sad
Raknarg




PostPosted: Thu Mar 20, 2014 11:08 pm   Post subject: RE:Code abruptly stopping for no reason.

Look how your code indents:

Turing:

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


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)
Tony




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

end if
end loop
put "You won ", wins, " out of 20 games"
end if
end if
end loop
end if

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

code:

                put "You won ", wins, " out of 20 games"
            end if % subtraction
        end if % answer = num1 + num2
    end loop
end if % addition


Don't have to label everything. Just enough to help you navigate.
Latest from compsci.ca/blog: Tony's programming blog. DWITE - a programming contest.
Jari.C




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




PostPosted: 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
Jari.C




PostPosted: 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 Very Happy
Sponsor
Sponsor
Sponsor
sponsor
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 1  [ 8 Posts ]
Jump to:   


Style:  
Search: