-1

Hey StackOverflow Community,

I am currently programming my own life simulator called "XinsLife". Currently I want to call a variable that I set before. But it just doesn't work. I tried everything on YouTube and SS64 and could not find what the error was. So I thought I'd ask on StackOverflow. I know this problem is basic and probably everyone can do this, but I am very new to Batch.

Any help would be appreciated.

I am currently at country choosing, so when I choose a country it has to do a variable of what I chose, then goto :savesettings, and display the country that I chose. But it does not work.

C o d e :

:countrychoosing
cls
title New Life -- Country choosing
echo.
echo Choose your country:
call :echo-align center "United States"
call :echo-align center "United Kingdom"
call :echo-align center "China"
call :echo-align center "India"
call :echo-align center "More countries to come soon!"
set /p choosecountry=
if "%choosecountry%" equ 1 (
    set chosecountry = United States
    goto savesettings
)

if "%choosecountry%" equ 2 (
    set chosecountry = United Kingdom
    goto savesettings
)   

if "%choosecountry%" equ 3 (
    set chosecountry = China
    goto savesettings
)

if "%choosecountry%" equ 4 (
    set chosecountry = India
    goto savesettings
)

:savesettings
call %chosecountry%
echo You chose the country "%chosecountry%". Do you want to create a save file?
choice /C:YN
if "%errorlevel%" equ 255 goto savesettings
if "%errorlevel%" equ N goto continuetest
if "%errorlevel%" equ Y goto createsavefiletest
if "%errorlevel%" equ 0 goto savesettings
  • https://stackoverflow.com/a/18046623/2128947 (random selection from responses to this weekly question) - but why use `set/p` when you are also using `choice` (preferred option) to do the same kind of thing? – Magoo Jan 04 '23 at 17:35
  • Doesn't work. I pressed 1, which should be United States, but it says: "India" isn't recognized as a command. You chose the country: "India" – mqnycrptnn Jan 04 '23 at 17:44
  • You don't see a label `echo-align` because I haven't showed you the rest of the code. `echo-align` works perfectly. Its just to align text to the center. – mqnycrptnn Jan 04 '23 at 17:59
  • Oh, and also, the "Why is no string output with 'echo %var%' after using 'set var = text' on command line?" thing. Did not help. It still says "India" isn't recognized as a command. You chose the country: "India". – mqnycrptnn Jan 04 '23 at 18:07
  • You need to match this syntax: ```If "%choosecountry%" == "1" (```, and: ```Set "chosecountry=United States"``` – Compo Jan 04 '23 at 18:53
  • 2
    I know you were given a lot of links, but you actually _do_ need to re-read https://stackoverflow.com/a/26388460/3074564 because you're setting your variables incorrectly. – SomethingDark Jan 04 '23 at 21:03
  • Hi Compo, its like I am at the start again. It said exactly the thing that it said at the start. – mqnycrptnn Jan 08 '23 at 14:16

1 Answers1

0

Why you should use "choice", not "set /p" to accept user-input.

When you use set /p "var=Some user prompt" then var is set to the string entered from the keyboard.

  • But note that if the user's response is simply Enter then var will remain unchanged

All fine in theory, if you can be certain that your users will never enter an unexpected string.

Suppose the user enters 2 to your first prompt

The if statement would be interpreted as if "2" equ 2 (

Since "2" and 2 are different, the if statement will be evaluated as false. You need if "%choosecountry%" equ "2" (

So the code now becomes if "%choosecountry%" equ "2" (

Suppose the user enters United Kingdom to your first prompt, instead of the expected 2.

The if statement would be interpreted as if "United Kingdom" equ "2" ( - well, that could be fixed with some more code.

But suppose the user enters UK -yes. More code...

Suppose the entry was x"y. This would lead to a syntax error - and there are oodles of other examples like %hello which the user could enter but batch would object to.

Well - most, but probably not all of these unexpected input strings could probably be massaged to allow the cmd processor to handle them smoothly, but that's a lot of unnecessary work and code.

The better solution is to use choice which you use (but incorrectly, sorry) later in your code.

If you enter choice /? from the prompt, you get a full description of choice. In summary, you can use the command choice /C:YN where YN can be any string of unique alphamerics - a to z or a digit.

choice waits for a single character and will beep if one of the choices set by the /c: is not used.

If one of the allowed options is used, then it sets the magic variable errorlevel to a value that depends on the position of the character within the allowed string, so pressing N in response to choice /C:YN would set errorlevel to 2; for choice /C:NY to 1; for choice /C:ORANGES to 4.

magic variables are those that are set automatically like DATE, TIME, RANDOM and others. These variables should not be set as part of a program as the value set into the variable then overrides the value that the system sets.

magic variables otherwise work identically to normal user-variables, and ALL VARIABLES are strings. If the string is purely numeric then the set /a command can be used to perform some mathematical operations. (there are some esoteric exceptions to the pure-numeric string requirement - see set /? from the prompt for details)

So, to structure a choice for use from a menu, you might have

choice /C:YN
if "%errorlevel%" equ "255" goto savesettings
if "%errorlevel%" equ "2"   goto continuetest
if "%errorlevel%" equ "1"   goto createsavefiletest

Note that each side of the if operator ("equ") must be quoted.

OR - you may have

choice /C:YN
if errorlevel 255 goto savesettings
if errorlevel 2   goto continuetest
if errorlevel 1   goto createsavefiletest

Note that these tests must be performed in reverse-numerical order as the meaning of if errorlevel n is "if errorlevel is n or greater than n"

You could even use

choice /C:YN
goto point%errorlevel%

where :point1 is located at :createsavefiletest, :point2 at :continuetest and :point255 at :savesettings.

Note that a label is simply a location in a file, so

some code...
:xyz
:abc
:hyg
some more code...

would mean that goto xyz,goto abc andgoto hyg all do exectly the same thing.

This should enable you to solve your problem.

Batch is sensitive to spaces in an ordinary string SET statement. SET FLAG = N sets a variable named "FLAGSpace" to a value of "SpaceN". Remove Space from both sides of the =.

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign " or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.

Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Hey, Thanks for your answer, though it did not work. The "India" is not a recognized command was fixed by your comment, but it still says You chose "". @Magoo – mqnycrptnn Jan 08 '23 at 13:57
  • 1
    In all probability because you haven't fixed your `set` syntax. I've added the `set` sermons. – Magoo Jan 08 '23 at 18:15
  • Thanks, now it works, but its again saying "India" is not a recognized command. EDIT: Now I know, why it didn't work. I used the command call %chosecountry% which is the same result as if I would type into the command prompt "India", I just removed it and now it works. Thanks a lot Magoo. – mqnycrptnn Jan 09 '23 at 14:40