0

So the thing is that i made variable "x" and set it as "0", i wanted to make it longer while pressing "d" and shorter while pressing "a", but i think that it shouldn't look like that, here's the code:

@echo off
set x=
set/a xnum=0

:m
set x=%x%0
cls
echo %x%
choice /c ad /n
if %errorlevel% equ 1 goto a
if %errorlevel% equ 2 goto d
goto m

:a
if %x% gtr 1 set /a pos-=1
:d
if %x% lss 17 set /a pos+=1
goto m
  • 2
    You aren't changing the value of `%x%` anywhere, you're just updating the value of some unrelated `%pos%` variable. – SomethingDark Jun 05 '22 at 23:41
  • 3
    Batch has no concept of "sections", "functions", "procedures" or "paragraphs". A label is simply a reference point. Execution does not stop when a label is reached, it simply continues through, line by line, until it reaches end-of-file, a **CALL** , a **GOTO** or an **EXIT** Hence, suppose `x` is 7, pressing `a` will goto `:a`, subtract 1 from `pos`, then add 1 to `pos` and goto `:m`, and extend the string `x` by `0`. You need to work out what variable is doing what; `xnum` isn't used, neither is `pos` and `x` will only ever be a sequence of `0`s. – Magoo Jun 05 '22 at 23:50
  • as you said, @SomethingDark i changed all of %pos% variabels to %x%, but it makes bigger mess than before, because after pressing a and d some times i get 21474836460 – PhilippePetain Jun 06 '22 at 00:21
  • `set /a` only does math. How can you do math with a string? Look into how you do substrings in batch. – SomethingDark Jun 06 '22 at 00:48
  • Please read [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) The option `/A` of command __SET__ is for evaluation of an __arithmetic expression__. Please use always a space between a command like __SET__ (argument 0) and its arguments like `/A` (argument 1) and the arithmetic expression (arguments 2 to n). – Mofi Jun 06 '22 at 08:45

1 Answers1

0

What you are currently doing is to mathematically subtract from the value by using set /a

Instead, you need to either add/remove characters from the variable:

@echo off
set x=0

:choices
cls
echo(%x%
choice /c ad /n >nul
if %errorlevel% equ 1 set "x=%x%0"
if %errorlevel% equ 2 if defined x set "x=%x:~0,-1%"
goto :choices
Gerhard
  • 22,678
  • 7
  • 27
  • 43