-1

I'm trying to make this guessing game with only 10 tries in batch but whatever number I guess it says the number is smaller and I can't find the problem

set /a var=%random%
echo %var%
for /l %%x in (1, 1, 10) do (

set /p guess="Try to guess the number: "
if "%guess%" equ "%var%" (goto 1)
if "%guess%" gtr "%var%" (echo Your number is greater)
if "%guess%" lss "%var%" (echo Your number is smaller)

)

echo You lost
pause
exit
:1
echo you guessed the number

1 Answers1

0

Maybe you have missed the code: SETLOCAL ENABLEDELAYEDEXPANSION.

And while comparing the size of the number, it is not necessary to use quotes.


Or if you don't wish to use it, you can make a more complicated loop.

@echo off
set /a var=%random%
rem I don't know why you would add the next line but I'll still let it stay there.
echo %var%
set l=0
goto guess

:guess
set /a l=l+1
set guess=-1
set /p guess="Try to guess the number: "
if %guess% equ %var% (goto correct)
if %guess% lss 0 (echo The input should be an integer which is not less than 0)
if %guess% gtr %var% (echo Your number is greater) else (echo Your number is smaller)
if "%l%" == "10" (goto fail) else (goto guess)

:fail
echo You lost
pause
exit

:correct
echo you guessed the number
pause
exit
Community
  • 1
  • 1
  • Thank you I'll just use your code – AAAAAAAAAAAAA Oct 19 '18 at 15:37
  • @user8361286, yes this works, but please read the duplicate question a few of us have upvoted. You will learn a lot from that. – Squashman Oct 19 '18 at 15:41
  • @Arnold you kind of contradicted yourself. You comment that it is not necessary to use quotes when comparing the size of the number but you then do it to when checking the amount of tries that have occurred. – Squashman Oct 19 '18 at 15:43
  • @Squashman I read the other post but Arnolds solution is a lot simpler – AAAAAAAAAAAAA Oct 19 '18 at 15:47
  • You may want to make two additions to the code: 1) After the label `guess` make the variable `guess` undefined by doing this: `set "guess="`. 2) After the `SET /P` line add another line to check if the variable `guess` has a variable assigned to it: `IF NOT DEFINED guess goto guess`. This solves two problems. On the first try if the user just hits enter, the variable guess becomes undefined. If the variable is undefined the `IF` conditions will fail with a syntax error. On the second try, if the user just hits enter, the variable guess is still assigned the previous guess. – Squashman Oct 19 '18 at 15:54
  • 1
    @TopHatLemur, so adding one line of code: `setlocal enabledelayedexpansion` and then changing `%guess%` to `!guess!` is not simple? – Squashman Oct 19 '18 at 15:57
  • I do agree with you but I am used to check whether the number of looped times has reached the required number in a string format. Therefore I didn't compared the number of tries, in numerical method. :) But thanks for your suggestion. –  Oct 19 '18 at 16:17
  • For the syntax error, I have recently edited the codes, you can check it out to see if it works. –  Oct 19 '18 at 16:18
  • @ArnoldLai, so every time they just hit enter at the input prompt it will guess -1 for them. – Squashman Oct 19 '18 at 16:32
  • Logically, yes. I think that should also be counted as one attempt of guessing. –  Oct 19 '18 at 16:44
  • @Squashman I tried adding and replacing but it seemed inconsistent – AAAAAAAAAAAAA Oct 20 '18 at 03:08
  • @TopHatLemur what do I know. I have only been doing this for 35 years. – Squashman Oct 20 '18 at 04:52
  • @Squashman It was probably me doing something wrong I’ll use your solution next time I need to use batch – AAAAAAAAAAAAA Oct 21 '18 at 02:50