79Views2Replies

Author Options:

Batch programming: this doesn't work? Answered

Ok, so I've been making a program that tells jokes (just as a test). What I'm trying doesn't work for some reason, and I don't know why. Can you tell me how to fix this, or give me a new code that does the same thing but without a separate .txt file?

Here's the code:
@echo off
title Joke Test
color 0a

:joke
set /a joke=%random% %% 6+1
if %joke%==1 do (
cls
echo.
echo           What is the stupidest animal in the jungle?
pause > nul
echo.
echo                       The polar bear.
pause > nul
goto menu
)
if %joke%==2 do (
cls
echo.
echo            Where did Mary go after the explosion?
pause > nul
echo.
echo                       Everywhere!                     
pause > nul
goto menu
)
if %joke%==3 do (
cls
echo.
echo                  Knock knock.
pause > nul
echo.
echo                  Who's there?
pause > nul
echo.
echo                  Europe.
pause > nul
echo.
echo                  Europe who?
pause > nul
echo.
echo                  No ur a poo!
pause > nul
goto menu
)
if %joke%==4 do (
cls
echo.
echo       A man walks into a zoo. The only animal there is a dog.
pause > nul
echo.
echo                       It's a shitzhu.
pause > nul
goto menu
)
if %joke%==5 do (
cls
echo.
echo           What do you call a kid with facial deformation?
pause > nul
echo.
echo                           Names.
pause > nul
goto menu
)
if %joke%==6 do (
cls
echo.
echo           What did the pirate say on his 80th birthday?
pause > nul
echo.
echo                          Aye matey!
pause > nul
goto menu
)

Discussions

0
None
TheBigRoosterYo

3 years ago

Ok, I've found a way to fix this. Don't know why this works, but it does.

@echo off

title Joke Test

color 0a

:joke

set /a joke="%random% %% 6+1"

if %joke%==1 goto joke1

if %joke%==2 goto joke2

if %joke%==3 goto joke3

if %joke%==4 goto joke4

if %joke%==5 goto joke5

if %joke%==6 goto joke6

:joke1

cls

echo.

echo What is the stupidest animal in the jungle?

pause > nul

echo.

echo The polar bear.

pause > nul

goto joke

:joke2

cls

echo.

echo Where did Mary go after the explosion?

pause > nul

echo.

echo Everywhere!

pause > nul

goto menu

:joke3

cls

echo.

echo Knock knock.

pause > nul

echo.

echo Who's there?

pause > nul

echo.

echo Europe.

pause > nul

echo.

echo Europe who?

pause > nul

echo.

echo No ur a poo!

pause > nul

goto menu

:joke4

cls

echo.

echo A man walks into a zoo. The only animal there is a dog.

pause > nul

echo.

echo It's a shitzhu.

pause > nul

goto menu

:joke5

cls

echo.

echo What do you call a kid with facial deformation?

pause > nul

echo.

echo Names.

pause > nul

goto menu

:joke6

cls

echo.

echo What did the pirate say on his 80th birthday?

pause > nul

echo.

echo Aye matey!

pause > nul

goto menu

0
None
Downunder35mTheBigRoosterYo

Answer 3 years ago

It works because you now have defined IF functions and you use "jumps" (goto).
Before you created a random number to select a joke without creating a loop for it so the program would only run once.
Rocketsurgery had a great idea with the arrays, you should incorportate it as it makes the code easier to change for new jokes.
The best option however would be to actually use an external textfile and variables for the text array and length.
So your first variable is read from the amount of lines within the textfile and sets the array for the ramndom number.
The next would be the one for actual joke, but if in doubt you can use the first for this as well.
And instead of the "IF - GOTO" routines you read the lines for joke from the textfile directly based on the random number.
This way you can add to the textfile without the need for any code changes.
If you want to keep all within a single file you can use the arrays as a block, read it out and use variables in a similar way but the coding is a bit more complex for this.

0
None
rocketsurgery

3 years ago

You could also use arrays to store your jokes

SET Joke[0,0]=How many surrealists does it take to change a lightbulb?

The answer would be...

SET Joke[0,1]=The Fish.

You would then randomly choose the joke and for loop until the joke was complete...

using something like echo !Joke[%JokeSelected%,%LineNumber%]!

See below as an example using separate arrays for question and answer (I would prefer the 2d array as above)...

@echo off

setlocal EnableDelayedExpansion

Set NumberOfJokes=5

SET Question[0]=What is a physicist's favourite food?

SET Answer[0]=Fission Chips.

SET Question[1]=What do you call two crows on a brach?

SET Answer[1]=Attempted Murder

SET Question[2]=My band is called 1023MB

SET Answer[2]=We haven't yet had any Gigs.

SET Question[3]=A photon hops on a plane with no luggage...

SET Answer[3]=He was travelling light.

SET Question[4]=Hear of the man who was cooled to absolute zero?

SET Answer[4]=He's 0K now

SET /A JokeSelected=(%RANDOM% %% %NumberOfJokes%)

echo !Question[%JokeSelected%]!

pause > nul

echo !Answer[%JokeSelected%]!

pause > nul