Getting code to repeat

Previous topic - Next topic

Sev

How do I get the code (in the actual code box) below to only do a sum ten times?

Can you use this: GLOBAL counter_Times=0


Code (glbasic) Select

WHILE TRUE

LOCAL num1% //Defines a number
LOCAL num2% //Defines another number
LOCAL answer% //Holds our answer
STATIC numcorrect% = 0  //Holds how many we have correct

num1 = RND(8)  //Generate a random number between 0 and 25 for our first number
num2 = RND(8) //Generate a random number between 0 and 25 for our second number
redo: //This is a label. You can use GOTO to hop back to here in code.

PRINT "Welcome to Sums! " + numcorrect + " correct so far.",5,5 //Print our welcome message with the number we have completed
PRINT num1 + " + " + num2 + " = ",5,35 //Displays our problem to solve. As you can see, it calls variables num1 and num2
INPUT answer,160,35 //This is the simple INPUT command to draw user input.

IF INTEGER(num1 + num2) = answer //This IF statement checks whether or not num1+num2 = answer
CLEARSCREEN //Clears the screen
PRINT "CORRECT!",5,5 //Prints CORRECT!0
INC numcorrect,1 //Increase correct problems by 1
SHOWSCREEN //Draw the screen
SLEEP 1000 //Freezes the screen for 2 seconds
ELSE //ELSE... means the user failed the question. num1 + num2 is not answer.
CLEARSCREEN //Clears the screen
PRINT "SORRY! TRY AGAIN!",5,5 //Prints SORRY! TRY AGAIN!
SHOWSCREEN //Draw the screen
SLEEP 2000 //Freezes the screen for 2 seconds
GOTO redo //GOTO our redo label we made earlier to retry the question
ENDIF //ENDIF ends the IF statement

SHOWSCREEN

WEND


Thanks
Red Text

erico

Hi Sev.

That code of yours is quite linear.
While a linear code can work for a lot of stuff, it makes for a lot of work if what you are up too is complex, your code may come to have thousands of lines.

You must understand the concept of a loop. It is a bit abstract but it is nothing out of this world.

Best start with the GLB demos on the help files, specially the PONG game example.
It comes good to give a general read on the GLB commands just to get an average idea.

Here is a good start:
http://www.glbasic.com/xmlhelp.php?lang=en&id=130&action=view

Here the PONG one:
http://www.glbasic.com/xmlhelp.php?lang=en&id=127&action=view

That is a very good path in case you have no prior experience in coding BASIC.

I can help with your current code, but the loop is a must know abstract concept, it is always best to understand it by one self.
Check those tutorial files on the main site, if you can´t understand it, count me in to help out!

cheers!

Ian Price

Not sure if you mean that it only asks 10 sums (no matter how many you get right or wrong), or if you mean you have to get 10 sums correct to continue. I've used the latter - you must get 10 sums correct in this minor adaptation of your code.

Code (glbasic) Select

LOCAL num1% //Defines a number
LOCAL num2% //Defines another number
LOCAL answer% //Holds our answer
STATIC numcorrect% = 0  //Holds how many we have correct

WHILE numcorrect<10   // Moved. Don't place your LOCAL variables in the loop. Changed from TRUE to numcorrect<10

num1 = RND(8)  //Generate a random number between 0 and 25 for our first number
num2 = RND(8) //Generate a random number between 0 and 25 for our second number
redo: //This is a label. You can use GOTO to hop back to here in code.

PRINT "Welcome to Sums! " + numcorrect + " correct so far.",5,5 //Print our welcome message with the number we have completed
PRINT num1 + " + " + num2 + " = ",5,35 //Displays our problem to solve. As you can see, it calls variables num1 and num2
INPUT answer,160,35 //This is the simple INPUT command to draw user input.

IF INTEGER(num1 + num2) = answer //This IF statement checks whether or not num1+num2 = answer
CLEARSCREEN //Clears the screen
PRINT "CORRECT!",5,5 //Prints CORRECT!0
INC numcorrect,1 //Increase correct problems by 1
SHOWSCREEN //Draw the screen
SLEEP 1000 //Freezes the screen for 2 seconds
ELSE //ELSE... means the user failed the question. num1 + num2 is not answer.
CLEARSCREEN //Clears the screen
PRINT "SORRY! TRY AGAIN!",5,5 //Prints SORRY! TRY AGAIN!
SHOWSCREEN //Draw the screen
SLEEP 2000 //Freezes the screen for 2 seconds
GOTO redo //GOTO our redo label we made earlier to retry the question
ENDIF //ENDIF ends the IF statement

SHOWSCREEN

WEND


All I've done is changed the WHILE bit. I've moved it (so that you aren't constantly declaring variables within the loop and added "numcorrect<10" - this basically means that this whole WHILE/WEND loop will repeat until numcorrect=10. That only occurs once you have answered 10 questions correctly.
I came. I saw. I played.

Sev

Thanks Ian, your adaption works.

Erico, sorry im not able to do any tutorials as this is for an assignment that is due in two days. But thanks anyway for your willingness to help.

Thanks.
Red Text

Moru

That is a very interesting viewpoint. Your teacher is happy as long as you deliver something, no matter if you learned something or not?  :help:


Ian Price

Hmmmm... If I had known it was for an assignment I would have given pointers rather than the actual code to do this task.

I hope that you at least understood what changes I made and how they make the code do what you wanted it to.
I came. I saw. I played.

mentalthink

Take a look to While and If ... this it's common in a lot of lenguages, when you understand this don't needs complete codes...

Ian Price

Everybody's got to start somewhere and to some coding is harder than others. Someone with a low post-count here may not be a noob, but still stuck. I'd like to think we'd all try to help everyone, but sometimes, as in this case, it's better to let the user learn a bit more for themselves. I'm pretty sure that most new users don't just come here for help with their assignments though. :P
I came. I saw. I played.