help using multiple IF statments.

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
andrewg250
Posts: 3
Joined: 19 Aug 2015 19:02

help using multiple IF statments.

#1 Post by andrewg250 » 21 Aug 2015 17:54

im not sure why but i seem to have a problem when the DOS Rpg im making gets to this chunk of code the terminal simply shuts down leaving no time for output. ive tried pausing the terminal in specific areas to see the problem but i cannot seem to find it. hoping i can get some help.

:LevelGauge
if %CharLevel%==1 (
goto LEVEL2
) else if %CharLevel%==2 (
goto LEVEL3
) else if %CharLevel%==3 (
goto LEVEL4
) else if %CharLevel%==4 (
goto LEVEL5
) else if %CharLevel%==5 (
goto LEVEL6
) else if %CharLevel%==6 (
goto LEVEL7
) else if %CharLevel%==7 (
goto LEVEL8
) else if %CharLevel%==8 (
goto LEVEL9
) else if %CharLevel%==9 (
goto LEVEL10
) else (
goto MENU
)
goto MENU

:LEVEL2
if %CharExp% GEQ %CharMaxExp% (
set /a CharExp=%CharExp% -%CharMaxExp%
set CharMaxExp=280
set CharLevel=2
set /a CharAttack=(%CharAttack% * 1.5) + 1
goto LEVELUP
)

:LEVEL3
if %CharExp% GEQ %CharMaxExp% (
set /a CharExp=%CharExp% -%CharMaxExp%
set CharMaxExp=784
set CharLevel=3
set /a CharAttack=(%CharAttack% * 1.5) + 1
goto LEVELUP
) else goto MENU

:LEVEL4
if %CharExp% GEQ %CharMaxExp% (
set /a CharExp=%CharExp% -%CharMaxExp%
set CharMaxExp=1960
set CharLevel=4
set /a CharAttack=(%CharAttack% * 1.5) + 1
goto LEVELUP
) else goto MENU

:LEVEL5
if %CharExp% GEQ %CharMaxExp% (
set /a CharExp=%CharExp% -%CharMaxExp%
set CharMaxExp=3488
set CharLevel=5
set /a CharAttack=(%CharAttack% * 1.5) + 1
goto LEVELUP
) else goto MENU

:LEVEL6
if %CharExp% GEQ %CharMaxExp% (
set /a CharExp=%CharExp% -%CharMaxExp%
set CharMaxExp=5366
set CharLevel=6
set /a CharAttack=(%CharAttack% * 1.5) + 1
goto LEVELUP
) else goto MENU

:LEVEL7
if %CharExp% GEQ %CharMaxExp% (
set /a CharExp=%CharExp% -%CharMaxExp%
set CharMaxExp=8972
set CharLevel=7
set /a CharAttack=(%CharAttack% * 1.5) + 1
goto LEVELUP
) else goto MENU

:LEVEL8
if %CharExp% GEQ %CharMaxExp% (
set /a CharExp=%CharExp% -%CharMaxExp%
set CharMaxExp=11087
set CharLevel=8
goto LEVELUP
) else goto MENU

:LEVEL9
if %CharExp% GEQ %CharMaxExp% (
set /a CharExp=%CharExp% -%CharMaxExp%
set CharMaxExp=14763
set CharLevel=9
set /a CharAttack=(%CharAttack% * 1.5) + 1
goto LEVELUP
) else goto MENU

:LEVEL10
if %CharExp% GEQ %CharMaxExp% (
set /a CharExp=%CharExp% -%CharMaxExp%
set CharLevel=10
set /a CharAttack=(%CharAttack% * 1.5) + 1
goto LEVELUP
) else goto MENU
goto MENU

:LEVELUP
cls
echo You Have Leveled Up.
ping localhost 2 >nul
goto MENU
Last edited by andrewg250 on 21 Aug 2015 19:19, edited 1 time in total.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: help using multiple IF statments.

#2 Post by ShadowThief » 21 Aug 2015 19:16

Your very first else is on a separate line from the rest of the if statement, and that's breaking everything. It's also worth noting that you're going to have problems with both math that is only partially inside of parentheses, and non-integer math in general.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: help using multiple IF statments.

#3 Post by Squashman » 21 Aug 2015 19:22

You do not need any ELSE statements. Just simplify it.

Code: Select all

:LevelGauge
if %CharLevel%==1 goto LEVEL2
if %CharLevel%==2 goto LEVEL3
if %CharLevel%==3 goto LEVEL4
if %CharLevel%==4 goto LEVEL5
if %CharLevel%==5 goto LEVEL6
if %CharLevel%==6 goto LEVEL7
if %CharLevel%==7 goto LEVEL8
if %CharLevel%==8 goto LEVEL9
if %CharLevel%==9 goto LEVEL10
goto MENU

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: help using multiple IF statments.

#4 Post by ShadowThief » 21 Aug 2015 19:38

Or, even shorter,

Code: Select all

if %CharLevel% lss 10 (
    set /a nextLevel=%CharLevel%+1
    goto LEVEL%nextLevel%
)
goto MENU

andrewg250
Posts: 3
Joined: 19 Aug 2015 19:02

Re: help using multiple IF statments.

#5 Post by andrewg250 » 21 Aug 2015 19:43

oh ok thank you. it was so simple i suppose i tend to over complicate things and make extra unnecessary code

andrewg250
Posts: 3
Joined: 19 Aug 2015 19:02

Re: help using multiple IF statments.

#6 Post by andrewg250 » 21 Aug 2015 19:47

ok well. although that should have worked it seems the game still crashes? could it be due to the functions syntax in :LEVEL(2-10) ?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: help using multiple IF statments.

#7 Post by Squashman » 21 Aug 2015 19:56

Again you are not using the correct syntax of the IF ELSE statement nor do you even need it. Just take out the ELSE.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: help using multiple IF statments.

#8 Post by ShadowThief » 21 Aug 2015 19:59

Run the script from the command prompt instead of clicking it. When I ran your code, I get
+ was unexpected at this time

because you can't use parentheses to do math inside of other parentheses (without using the ^ escape character to allow it).

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: help using multiple IF statments.

#9 Post by Squashman » 21 Aug 2015 20:10

Batch can only do Integer match as well. You cannot use SET /A for decimal math.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: help using multiple IF statments.

#10 Post by Squashman » 21 Aug 2015 20:12

ShadowThief wrote:Run the script from the command prompt instead of clicking it. When I ran your code, I get
+ was unexpected at this time

because you can't use parentheses to do math inside of other parentheses (without using the ^ escape character to allow it).


No just use QUOTES.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: help using multiple IF statments.

#11 Post by ShadowThief » 21 Aug 2015 20:47

That works, too. My point is that you can't nest parentheses like how he's doing it.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: help using multiple IF statments.

#12 Post by foxidrive » 21 Aug 2015 21:42

I think this does what your code does. You'll have to verify it.

Code: Select all

:LevelGauge
if %CharLevel% equ 0 goto :MENU
set /a CharLevel+=1
if %CharExp% LSS %CharMaxExp% goto :MENU
set /a CharExp=CharExp - CharMaxExp
if %Charlevel% EQU 2 set CharMaxExp=280
if %Charlevel% EQU 3 set CharMaxExp=784
if %Charlevel% EQU 4 set CharMaxExp=1960
if %Charlevel% EQU 5 set CharMaxExp=3488
if %Charlevel% EQU 6 set CharMaxExp=5366
if %Charlevel% EQU 7 set CharMaxExp=8972
if %Charlevel% EQU 8 set CharMaxExp=11087
if %Charlevel% EQU 9 set CharMaxExp=14763
set /a CharAttack="(CharAttack * 1.5) + 1"
cls
echo You Have Leveled Up.
ping -n 3 "" >nul
goto :MENU

Post Reply