| 1. |
Solve : Fixing Enter Goes To Random Thing.? |
|
Answer» Okay, i have this menu: Strangely the pause doesn't work and it goes to somewhere random... 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: 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. Good. Quote 4. You should test to see whether or not an entry was made, even if it was wrong. Very good. Quote 5. Add this code at the end of the original code you posted. Good. Quote 6. Add single quotation marks around the variables in the "if - goto" statements. 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 AMHow about the option to exit when you type 'exit.Quote2. Add choice #7 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. |
|