user2593700
user2593700

Reputation: 1

why is my batch if statement not working?

I have been having trouble with this code for a while. Please take a look and tell me what is wrong so i can fix it. This code will be a part of an xprize competion!

echo [Math]
echo.
echo.
echo.
echo            Question 1
echo.  
echo.
echo.
echo        If Mary goes to the market and buys three 
echo        bananas for two dollars each, how much 
echo        did she spend altogether?
echo.
echo.
echo        (A) $3
echo        (B) $6
echo        (C) $5
echo        (D) $1
echo.
echo.
echo.
set /p ANSWER01=Answer:
cls
goto results


:results
set /a "COUNTER=0"

here is what i cant figure out: i dont know how to change a value of a variable in an if statement

if %ANSWER01%==b(
    set /a "COUNTER=COUNTER+1"
)
echo %COUNTER%
pause

Upvotes: 0

Views: 106

Answers (3)

Jose Rodriguez
Jose Rodriguez

Reputation: 10162

Hi, do need enable a delayed expansion option and give an space to if sentence to a parenthesis:

Your code is something like:

set /a counter=0

setlocal ENABLEDELAYEDEXPANSION

echo [Math]
echo.
echo.
echo.
echo            Question 1
echo.  
echo.
echo.
echo        If Mary goes to the market and buys three 
echo        bananas for two dollars each, how much 
echo        did she spend altogether?
echo.
echo.
echo        (A) $3
echo        (B) $6
echo        (C) $5
echo        (D) $1
echo.
echo.
echo.
set /p ANSWER01=Answer:
cls
goto results

:results

if %ANSWER01%==b (
    set /a counter=counter+1
)
echo %counter%
pause

endlocal

Upvotes: 0

Monacraft
Monacraft

Reputation: 6630

You should use the choice command with:

Choice /c abcd /m "Answer: "
set ANSWER01=%errorlevel%

also for your if statement just do it as so:

if %ANSWER01% equ 2 set /a "COUNTER=COUNTER+1"

That would be easier and better.

Note: the equ 2 is if you use the choice command

Upvotes: 1

Magoo
Magoo

Reputation: 80023

You need a space between the b and the (

AAMOI, if /i will make the match case-insensitive.

Upvotes: 0

Related Questions