1.

Solve : Fixing Enter Goes To Random Thing.?

Answer»

Okay, i have this menu:
Code: [Select]cls
echo.
echo Current Available Regiments:
echo.
echo Rank Name
echo -------------------------------
echo 1 The Dogs
echo -------------------------------
echo 2 British
echo -------------------------------
echo 3 Revolution
echo -------------------------------
echo 4 SWAT
echo -------------------------------
echo 5 Americans
echo -------------------------------
echo. 6 Smiths Unit (Default)
echo -------------------------------
echo.
echo.
echo Type The Rank Of The Regiment You Wish To Join.
echo.
set /p choice= [Regiment Rank]:
if %choice%==1 goto TheDogs
if %choice%==2 goto British
if %choice%==3 goto Revolution
if %choice%==4 goto SWAT
if %choice%==5 goto Americans
if %choice%==6 goto noregiment

When i don't enter anything and just press enter it goes to a random option, how can i fix this please?
(sorry about all the questions i just need to fix a few bugs in my game)Code: [Select]:1
cls
echo.
echo Current Available Regiments:
echo.
echo Rank Name
echo -------------------------------
echo 1 The Dogs
echo -------------------------------
echo 2 British
echo -------------------------------
echo 3 Revolution
echo -------------------------------
echo 4 SWAT
echo -------------------------------
echo 5 Americans
echo -------------------------------
echo. 6 Smiths Unit (Default)
echo -------------------------------
echo.
echo.
echo Type The Rank Of The Regiment You Wish To Join.
echo.
set /p choice= [Regiment Rank]:
if %choice%==1 goto TheDogs
if %choice%==2 goto British
if %choice%==3 goto Revolution
if %choice%==4 goto SWAT
if %choice%==5 goto Americans
if %choice%==6 goto noregiment
cls
echo.
echo. You did not choose anyone
echo.
pause > nul
goto :1Starting to look nice...Post it here when you're done, Jacob After the last possible choice IF test, have a catch-all IF test which directs execution back to the menu possibly via an error message like this...

Code: [Select]:Menu
cls
echo.
echo Current Available Regiments:
echo.
echo Rank Name
echo -------------------------------
echo 1 The Dogs
echo -------------------------------
echo 2 British
echo -------------------------------
echo 3 Revolution
echo -------------------------------
echo 4 SWAT
echo -------------------------------
echo 5 Americans
echo -------------------------------
echo. 6 Smiths Unit (Default)
echo -------------------------------
echo.
echo.
echo Type The Rank Of The Regiment You Wish To Join.
echo.
set /p choice= [Regiment Rank]:
if %choice%==1 goto TheDogs
if %choice%==2 goto British
if %choice%==3 goto Revolution
if %choice%==4 goto SWAT
if %choice%==5 goto Americans
if %choice%==6 goto noregiment
echo.
echo Input not recognised
echo Press a KEY to try again
pause>nul
goto Menu

Wierd:
if i type anything that isn't 1, 2, 3, 4, 5 or 6 such as lol or hehe it works.
if i THEN type nothing and press enter it works.
BUT if i go straight there and enter nothing and press enter it goes random.

very weird...Quote from: Jacob on June 07, 2008, 09:17:10 AM

Strangely the pause doesn't work and it goes to somewhere random...


and i will post the game here when i am done.
i have about 1700 lines of code.

and i have only done around 1.5 missions.

Which version, mine or Carbon's? or both? That code works on my system.
Wierd:
if i type anything that isn't 1, 2, 3, 4, 5 or 6 such as lol or hehe it works.
(AFTER THE ABOVE) if i THEN type nothing and press enter it works.
BUT if i go straight there and enter nothing and press enter it goes random.

very weird...Did you just COPY your previous post? Quote from: Carbon Dudeoxide on June 07, 2008, 09:27:55 AM
Did you just copy your previous post?
he quoted me, so he might not of seen that i changed the old post.
so i posted again just to be sure.
btw,
www.sassurvival.co.nr
I have a few suggestions:
1. Add this code at the beginning of the original code you posted.
(This will allow you to return to the menu if an error is encountered.)
Code: [Select]@echo off
:Start
2. Add choice #7 Exit

3. Add this code before the set /p command in the original code you posted.
(This will remove (or delete) the environment variable choice, if it exists.)
Code: [Select]set choice=
4. You should test to see whether or not an entry was made, even if it was wrong.
If no entry was made, you should set the variable "choice" at this time
so that you can display the proper error to the operator.
Add this code at the end of the original code you posted.
The first line is true if you fail to enter anything at the set /p command
Code: [Select]if '%choice%'=='' set choice=No Selection!
echo.
echo MENU ERROR&echo.&echo.&echo.
echo You must make a VALID selection:&echo.
echo "%choice%" is not valid!&echo.&echo.&echo.
echo Valid selections are: 1 2 3 4 5 6 or 7.&echo.&echo.
echo Type The Rank Of The Regiment You Wish To Join.
echo Press Enter to continue.&echo.&echo.
echo Press any key to return to the menu.&pause >nul
cls&goto Start
5. Add this code at the end of the original code you posted.

Code: [Select]:End
exit
6. Add single quotation marks around the variables in the "if - goto" statements.
I do not know the technical reason for this, but it is shown this way in the examples in
the help on this site. They surely had a good reason for using the single quotes in the
example. I do know this. When I try to remove them from a working test file, it starts
to behave badly. I suggest that you use the quotes as shown.
Note the single quotes in the example from the help page below:
Code: [Select]if '%choice%'=='1' goto hello Microsoft DOS set command

Dias, (If you are not too mad at me from another recent post), could you explain the reason for the eratic behavior without the use of the single quotes.

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Finally, here is your code modified as I suggested, & working good!
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Code: [Select]@echo off
:Start
cls&echo.
echo Current Available Regiments:
echo.
echo Rank Name
echo -------------------------------
echo 1 The Dogs
echo -------------------------------
echo 2 British
echo -------------------------------
echo 3 Revolution
echo -------------------------------
echo 4 SWAT
echo -------------------------------
echo 5 Americans
echo -------------------------------
echo. 6 Smiths Unit (Default)
echo -------------------------------
echo 7. Exit
echo -------------------------------&echo.
echo Type The Rank Of The Regiment You Wish To Join.
echo (Valid selections are: 1 2 3 4 5 6 or 7 - Press Enter to continue.)&echo.
set choice=
set /p choice= [Regiment Rank]:
cls

if '%choice%'=='1' goto TheDogs
if '%choice%'=='2' goto British
if '%choice%'=='3' goto Revolution
if '%choice%'=='4' goto SWAT
if '%choice%'=='5' goto Americans
if '%choice%'=='6' goto SmithsUnit
if '%choice%'=='7' goto End

:: The following line tests if no entry has been made.
if '%choice%'=='' set choice=No Selection!
echo.
echo MENU ERROR&echo.&echo.&echo.
echo You must make a valid selection:&echo.
echo "%choice%" is not valid!&echo.&echo.&echo.
echo Valid selections are: 1 2 3 4 5 6 or 7.&echo.&echo.
echo Type The Rank Of The Regiment You Wish To Join.
echo Press Enter to continue.&echo.&echo.
echo Press any key to return to the menu.&pause >nul
cls&goto Start

:TheDogs
echo.&echo TheDogs&echo.
pause>nul&goto end
:British
echo.&echo British&echo.
pause>nul&goto end
:Revolution
echo.&echo Revolution&echo.
pause>nul&goto end
:SWAT
echo.&echo SWAT&echo.
pause>nul&goto end
:Americans
echo.&echo Americans&echo.
pause>nul&goto end
:SmithsUnit
echo.&echo SmithsUnit&echo.
pause>nul&goto end
:End
exit
Quote from: llmeyer1000 on June 08, 2008, 01:49:29 AM
I have a few suggestions:
1. Add this code at the beginning of the original code you posted.
(This will allow you to return to the menu if an error is encountered.)
Code: [Select]@echo off
:Start

Good. Already suggested. See posts above

Quote
2. Add choice #7 Exit

Good.

Quote
3. Add this code before the set /p command in the original code you posted.
(This will remove (or delete) the environment variable choice, if it exists.)
Code: [Select]set choice=

Good.

Quote
4. You should test to see whether or not an entry was made, even if it was wrong.
If no entry was made, you should set the variable "choice" at this time
so that you can display the proper error to the operator.


Very good.

Quote
5. Add this code at the end of the original code you posted.

Code: [Select]:End
exit

Good.

Quote
6. Add single quotation marks around the variables in the "if - goto" statements.
I do not know the technical reason for this

It is so you can accurately define what you are testing for.

cmd.exe "expands" variables to their value at runtime.

Pressing Enter at the set /p prompt results in an empty string, (i.e. "nothing"), and the only way to test for it is to use delimiter characters.

if %choice% is empty, then...

if %choice%==1 goto label

expands to

if ==1 goto label (causes an error)

whereas, using quotes or other delimiters, it expands to

if ""=="1" goto label (does not cause an error)

You can use single quotes, double quotes (which is what I am used to), in fact any delimiting characters you like, except the usual suspects like <>^&. They must be the same both sides though.

Double quotes have a special meaning in connection with filenames, so sometimes people prefer to avoid them, and in fact many people prefer to use what I call "SPIDER brackets" but I think in the US they are called "braces" - these characters { }.

See here. If quotes are used, note that single on one side and double on the other side will not work as expected, (i.e. the test will give the "not the same" result), and mixing them before/after the variable doesn't work at all, and actually throws an error message

Code: [Select]C:\>set test=egg

C:\>if "%test%"=="egg" (echo yes) else (echo no)
yes

C:\>if '%test%'=='egg' (echo yes) else (echo no)
yes

C:\>if '%test%'=="egg" (echo yes) else (echo no)
no

C:\>if q%test%q==qeggq (echo yes) else (echo no)
yes

C:\>if a%test%b==aeggb (echo yes) else (echo no)
yes

C:\>if {%test%}=={egg} (echo yes) else (echo no)
yes

C:\>if "%test%'=="egg' (echo yes) else (echo no)
(echo was unexpected at this time.

Quote
Dias, (If you are not too mad at me from another recent post), could you explain the reason for the eratic behavior without the use of the single quotes.

I hope you are not too mad at me about the nanosecond thing!! I hope the above explains it.
i will add this at a later date, thanksQuote from: Dias de verano on June 08, 2008, 02:51:50 AM
Quote from: llmeyer1000 on June 08, 2008, 01:49:29 AM
Quote
2. Add choice #7 Exit

Good.
How about the option to exit when you type 'exit.

if %choice%==exit goto :endQuote from: Carbon Dudeoxide on June 08, 2008, 06:05:06 AM
if %choice%==exit goto :end

did you read all of my post?

if {%choice%}=={exit} goto :endUhh......It was too LONG

Sorry man.


Discussion

No Comment Found