1

Im just learning how to code in cmd, but for some reason, the tutorials that are out there aint working on my computer. I was trying to run the following program, but it just keeps failing on me.

::My first program

@echo off
:main menu
cls
echo what are you doing?
echo.
echo 1) Nothing :-P
echo 2) dancing
echo. 
set /p var="Choose answer:" 
if %answer% == 1 goto nothing
if %answer% ==2 goto dancing

:nothing
echo ok...chill

:dancing
echo nice

echo on
Ken White
  • 123,280
  • 14
  • 225
  • 444
user3604362
  • 361
  • 1
  • 5
  • 19
  • yeah, sorry, that is my mistake, but it still doesnt work. I have tried that before, then changed it to var, but forgot to update the file. I shall do that. – user3604362 May 07 '16 at 21:16
  • ah, now it works. loaded the wrong file. sorry – user3604362 May 07 '16 at 21:28
  • 2
    I rolled back your edit, as you removed the problem that existed when you made that edit. You can't substantially change your question after you receive answers to it, as you can invalidate those answers with your change. – Ken White May 07 '16 at 22:06
  • 1
    `::` is a label. The comment command is `REM` for remark. Start off doing things correctly not incorrectly. –  May 07 '16 at 22:25
  • @Noodles `::` is idiomatically a comment in batch files, functions as such in nearly all cases, and is arguably much more readable. See https://stackoverflow.com/questions/16632524. Pedantry based on language purity is slightly absurd in a language as anachronistic as batch. People come to batch to get things done, not to write beautiful code. – Ryan Bemrose May 08 '16 at 01:22
  • Show me the documentation. As you can't, it is wrong. Why do things wrong when you can do them right. Teaching people how to do things wrong is wrong. –  May 08 '16 at 01:39
  • 1
    Right, and you can't show me anywhere in "the documentation" that it's *not* supported either. That's because the language has extremely poor documentation (indeed, the ONLY official documentation comes in the `help` command - even ss64 is unofficial). See, I too can shift the burden of proof, though that doesn't make it any less a fallacious argument. I gave you a link to one of the best explanations available. If you understand the caveats outlined in that question, you know enough to use either option. – Ryan Bemrose May 08 '16 at 02:14
  • 2
    I concede that there are cases where `rem` works and `::` does not, and as with any language, one should be aware of the pitfalls before using them. But `::` has been a well-understood and accepted way to add a comment in a batch file for decades. While you may consider using `::` in certain cases to be shooting yourself in the foot, it's hardly a mortal wound in a language that allows you so many ways to metaphorically blow your own leg off. – Ryan Bemrose May 08 '16 at 02:14
  • 2
    In fact, with so many better alternatives to batch pre-installed on Windows (VBScript, PowerShell, bash soon), and so many others a quick install away, it can be argued that *Teaching people to do things in batch is wrong*, and we should be warning people away from the language completely, not quibbling over the purity of its comment statements. – Ryan Bemrose May 08 '16 at 02:14
  • @Noodles - http://ss64.com/nt/rem.html – SomethingDark May 08 '16 at 03:42
  • From that page *The double-colon is not documented as a comment command, it is a special case of a label that acts as a comment.* then the page talks about all the ways it won't work. Also that page is not documentation. –  May 08 '16 at 03:44

4 Answers4

1

You don't have %answer% to compare, because you input the answer of your question into var. Either change set /p to use answer

set /p answer="Choose answer:" 
if %answer% == 1 goto nothing
if %answer% ==2 goto dancing

or change your two if statements to use var:

set /p var="Choose answer:" 
if %var% == 1 goto nothing
if %var% ==2 goto dancing
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • Yeah, sorry, I had answer before, it didnt work, so I changed it to var, but it still wasnt working, and then I forgot to update it when uploading it here. sorry. Its not properly updated, but it aint working still – user3604362 May 07 '16 at 21:18
  • Ah! now it works.... had loaded wrong file. thanks – user3604362 May 07 '16 at 21:28
1

As others have pointed out here, your problem is that you set a variable called var and then tried to read a variable called answer. The root fix is to pick one name and stick with it. However, I want to offer two other pointers.

First, when debugging your batch file, remove the line @echo off from the top of your file. With echo on, you can see exactly which line was executing when you got your error, which will point you to the solution much more quickly.

Second, the reason your batch file terminated was a syntax error. With the answer variable unset, your if statement evaluated to

if == 1 goto nothing

This is not valid batch syntax, which is what caused the error "goto was unexpected at this time", and caused your batch file to terminate. Your code will still get this error if the user enters a blank string, even after fixing the issue of the wrong variable name. To correctly handle empty variables in an IF statement, place brackets or quotes around each side of the == like this:

if [%answer%]==[1] goto nothing

This way, even if the variable is the empty string, the IF evaluates to if []==[1] goto nothing, which is still a valid statement and thus won't crash your script.

Ryan Bemrose
  • 9,018
  • 1
  • 41
  • 54
0
::My first program

@echo off
:mainmenu
cls
echo what are you doing?
echo.
echo 1) Nothing :-P
echo 2) dancing
echo. 
set /p answer="Choose answer: " 
if %answer%==1 goto nothing
if %answer%==2 goto dancing

:nothing
echo ok...chill
pause
goto mainmenu

:dancing
echo nice
pause
goto mainmenu

This should work.

Jelly
  • 13
  • 1
  • 3
-2

Try this: Here I have set var for input and comparison.

  : : My first program
  @echo off
  :main menu
  cls
  echo .
  echo .
  echo what are you doing?
  echo 1) Nothing :-P
  echo 2) dancing
  echo .
  echo .
  echo choose answer:
  set /p var=
  IF %var%== 1 goto nothing
  IF %var%== 2 goto dancing

  :nothing
  echo ok...chill

  :dancing
  echo nice

  echo on
Himanshu Gupta
  • 305
  • 3
  • 12