user3265987
user3265987

Reputation:

If/Else Statements In Batch

I'm trying to create an RPG game in Batch/CMD. I want to make it so that when the user enters a number that isn't one of the ones listed it just reloads from the last marker. Here is the code:

if %1st%==1 %atk%+25 
if %1st%==2 %def%+25 
if %1st%==3 %hp%+25 
if %1st%==4 %iq%+25 
if %1st%==5 %luck%+25 

if not %1st%==1 goto 1st
if not %1st%==2 goto 1st
if not %1st%==3 goto 1st
if not %1st%==4 goto 1st
if not %1st%==5 goto 1st

Upvotes: 1

Views: 107

Answers (1)

Alex
Alex

Reputation: 917

It's a good practice not to have you variables start with a number, as %1 means the first parameter passed into the script. If you didn't pass any parameters to the script, the interpreter is seeing these statements as if not st%==1 goto 1st and if st%==1 %atk%+25 which are not valid.

You need to replace all %1st% with a proposed %first% and the rest as follows:
%2nd% to %second%
%3rd% to %third%
%4th% to %fourth%
%5th% to %fifth%
%6th% to %sixth%
%7th% to %seventh%
%8th% to %eighth%
%9th% to %ninth%

Here is you code with the changes mentioned above:

if %first%==1 %atk%+25 
if %first%==2 %def%+25 
if %first%==3 %hp%+25 
if %first%==4 %iq%+25 
if %first%==5 %luck%+25 

if not %first%==1 goto first
if not %first%==2 goto first
if not %first%==3 goto first
if not %first%==4 goto first
if not %first%==5 goto first

And I'm not sure how you're adding the values, here is a proposed changes with set /a commands:

if %first%==1 set /a atk=%atk%+25 
if %first%==2 set /a def=%def%+25 
if %first%==3 set /a hp=%hp%+25 
if %first%==4 set /a iq=%iq%+25 
if %first%==5 set /a luck=%luck%+25 

if not %first%==1 goto first
if not %first%==2 goto first
if not %first%==3 goto first
if not %first%==4 goto first
if not %first%==5 goto first

Upvotes: 1

Related Questions