-3

This code won't work. This is supposed to generate a name with random traits and backstories about them. I don't know why it won't work.

@echo off
:top
echo ----------------------------------------
echo   random  name genorator
echo ----------------------------------------
echo       1   genorate name
echo       2   add more info
echo       3   what is this
set /p rand=
if %rand% == 1 goto first
if %rand% == 2 goto 2
if %rand% == 3 goto 3
:first
if %random% == 0 set fname=tony
if %random% == 1 set fname=pamb
if %random% == 2 set fname=ape
if %random% == 3 set fname=bob
if %random% == 4 set fname=jonathan
if %random% == 5 set fname=dave
if %random% == 6 set fname=avery
if %random% == 7 set fname=felica
if %random% == 8 set fname=herman
if %random% == 9 set fname=elana
cls
echo your new name is %fname%
pause
:2
exit
:3
exit

When I press 1 it says "your new name is ".

halfer
  • 19,824
  • 17
  • 99
  • 186
john
  • 7
  • 1
  • What made you think it would magically generate a number from 0 to 9? – Squashman Dec 15 '17 at 05:13
  • 1
    Your question is not being well received, possibly for two reasons. The first is that a title that is 0% detail and 100% pleading is likely to annoy readers, who will not find that helpful. The second is that if a question appears to have been composed on a mobile phone (with abbreviated words and all in lower case) people will think that no effort went into it. From there, they will wonder how much effort went into the debugging. Remember that **effort attracts effort** when you're addressing volunteers - show people you actually care about your problem. – halfer Dec 15 '17 at 10:03
  • See [Debugging a batch file](https://stackoverflow.com/a/42448601/3074564). Instead of `set /p rand=` use command __CHOICE__. Run in a command prompt window `choice /?` for help on this command. Below this command you can use `goto Label%ERRORLEVEL%` to continue batch file execution on label `:Label1`, or `:Label2`, or `:Label3`. – Mofi Dec 15 '17 at 12:18
  • Sorry about the bad post, I was like 10 and had no idea what I was doing with code. – john Jul 20 '20 at 15:28

1 Answers1

3

Type in:

echo %random%

and see what it gives you. It will most likely give you a number higher than 9. For example:

c:\pax> echo %random%
1644

Now think about what your name will be set to in that case. Because none of the if conditions will be true, it won't be changed at all so it will remain whatever it was before you ran your script.

If you want to get a random number in the range zero through nine inclusive and set the name based on that, you can use:

set /a "num = %random% %% 10"
if %num% == 0 set fname=tony
rem ... and so on

Also keep in mind that, if your name list is likely to get longer, you may want to use the more advanced features of cmd(a) such as "arrays". For example, the following script shows how to do this:

@echo off
setlocal enableextensions enabledelayedexpansion

rem This is the list of names to use.

set namelist=tony pam george bob john dave avery felica herman
set namelist=!namelist! elana pax guido henry fortescue

rem Construct the array, consisting of a base name with "_<index>" suffix.

set /a "count = 0"
for %%n in (!namelist!) do (
    set fname_!count!=%%n
    set /a "count = count + 1"
)

rem Use modulo to select random index.

set /a "idx = !random! %% count"

rem Use double indirection to get actual name.

for /f %%a in ('echo fname_!idx!') do echo !%%a!

endlocal

(a) Yes, I know it's hard to think of the words cmd and advanced being used in the same sentence, but it does actually have some useful, if little-known, features :-)

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953