0

So for school I'm making a project with small basic, idea is very simple, just like my question. I'm making a sort of gambling simulator.

ready:
TextWindow.WriteLine("Do you want to bet? yes(1) or no(2).")
answer = TextWindow.ReadNumber()
If answer = 1 Then
  Goto start
Else 
  Goto ready
  EndIf
  start:
TextWindow.WriteLine("Ok lets go.")
points = 100
var1 = Math.GetRandomNumber(10)
If 5 < var1 < 9 Then 
  points = points + 100
ElseIf 9 < var1 < 10 then
  points = points + 200
Elseif 1 < var1 < 5 then
  points = points - 100
EndIf

TextWindow.WriteLine("You rolled " + var1 + ". You now have " + points + " points.")

The problem im facing is that whatever happens to my var1 100 gets added to my points, even when var1 is e.g. 1 or 10.

I've asked my teacher, he couldnt explain to me what was wrong. what am I to do?

thanks in advance

  • Problem is in `Goto ready`. There is no `ready:` block so code continues without a jump and `points=100` gets executed all the time. Good luck and happy programming! – Imran Saeed Mar 23 '17 at 10:18
  • isn't `ready` block on the first line? – koceeng Mar 23 '17 at 10:20
  • I'm not a smallbasic expert but after googling for a while, I think `If x < y < z then` is not a valid if statement. Because in all example I encounter (and all other programming language I know), you can only compare 2 variables on if. Sorry if this doesn't help – koceeng Mar 23 '17 at 10:22

1 Answers1

0

You had three issues.

  1. As suggested by +koceeng, Small Basic dose not support the "x < y < z" structure. Making it worse is Small Basic's tendency to fail safe, meaning that this will compile, but i won't give the expected result. The correct structure for this logic is "x < y and y < z". In other words, all logic is binary and comparisons should only happen two by two.
  2. You have a logic bug as you go from if to if-else. "5 < var1 and var1 < 9" combined with "9 < var1 and var1 < 10" has a hole in it. Nine drops out of the logic and the if statement never matches on it. The same thing will happen to the 5. The correct combination uses an equals sign on one end of the logic or the other: "9 <= var1 and var1 < 10".
  3. Your goto loop was making my head hurt. unless you are doing something simple like creating an infinite loop, jumping from the bottom of your program to the top, you should usually avoid the goto statement entirely. A while loop is a better choice for the type of input protection you are doing.

Here is a corrected version of your code:

answer = 2
While answer <> 1 'Input Protection
TextWindow.WriteLine("Do you want to bet? yes(1) or no(2).")
answer = TextWindow.ReadNumber()
EndWhile

TextWindow.WriteLine("Ok lets go.")
points = 100
var1 = Math.GetRandomNumber(10)
If 5 <= var1 And var1 < 9 Then 
  points = points + 100
ElseIf 9 = var1 or var1 = 10 then
  points = points + 200
Elseif 1 <= var1 and var1 < 5 then
  points = points - 100
EndIf

TextWindow.WriteLine("You rolled " + var1 + ". You now have " + points + " points.")
codingCat
  • 2,396
  • 4
  • 21
  • 27