0

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

3 Answers 3

1

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

0

You need a space between the b and the (

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

2
  • @PeterWrite how do i make it so if the user doesn't choose "b" it does "echo %COUNTER" Commented Jul 18, 2013 at 3:27
  • Your code works and echoes the value of counter (0) if the user doesn't choose b. HOWEVER, it's probably better to use if /i "%answer01%"=="b" (" to allow the user to simply press ENTER. Provided the user doesn't TRY to break the program, the code will work quite happily - IF you add the SPACE between the b` and the ( as I've advised.
    – Magoo
    Commented Jul 18, 2013 at 3:36
0

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
1
  • Um, no - not the case. You only need delayedexpansion if the variable is altered WITHIN a "block" (a sequence of parenthesised statements) and you want to access the altered value (when you need to use !var! instead of %var% - !var! for the altered value, %var% for the value before the block was entered)
    – Magoo
    Commented Jul 18, 2013 at 3:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.