1.

Solve : lil help?

Answer»

Hi im new at BATCH files and im stuck i read up and everything but it still wont work heres my script so far...


echo off
echo v1.2
color 1e
echo . .
echo . RaNdOm FACTS .
echo . .
echo Hello welcome to Random Facts
echo press A-Z to continue to facts or X to exit
echo have fun
if %option%==a goto a
if %option%==b goto b
:a hello
:b hi
pause


any ideas?? please

p.s its saying right fast then close that "goto is unsuspected at this time".:loop
set /p option=Your choice A -Z (X to exit)
if "%option%"=="a" goto a
if "%option%"=="b" goto b
if "%option%"=="x" exit
echo Wrong input!
goto loop
:a
echo hello
:b
echo hi



Better...

:loop
set /p option=Your choice A -Z (X to exit)
if "%option%"=="a" goto a
if "%option%"=="b" goto b
if "%option%"=="x" goto end
echo Wrong input!
goto loop

:a
echo hello
goto end
:b
echo hi
goto end

:z
echo last label
:end




Like Salmon Trout is showing, what seems to be missing from your code is the ability to input the 'option' variable. Without "set"ting the variable, it will not register that the variable even EXISTS and you run into the error you are seeing. The /p flag tells the script that it is waiting for user input for the variable, which is why you can put text after the = and it will show in the program rather than setting the variable to equal that.

Example:

set /p option=Your choice A -Z (X to exit)

willl generate this in the program:

Your choice A -Z (X to exit) |

where the | would be the cursor for the user to type at and the 'option' variable would equal their input.
If however you typed the following into the code:

set option=Your choice A-Z (X to exit)

the option variable would equal "Your" and the rest of the line would be dismissed and there would be no option for the user to input anything. Batch is very finnicky so a lot of things are learned through trial and error, and of course picking other people's brains.



Discussion

No Comment Found