Problem with batch "set" command

Asked

Viewed 524 times

0

Good afternoon. I was doing a very simple programming, in general, serves simply to put the numbers that the user put (10 numbers) in ascending order, but something strange happens already in this part

set /p NM9=
set /p NM10=

:CICLO
if %NM10% EQU %NM9% (
    goto IGUAL1
)
if %NM10% LEQ %NM9% (
    set /a VAR=%NM9%
    set /a NM9=%NM10%
    set /a NM10=%VAR%
)

After giving an echo at the beginning of the cycle, it says that the NM9 and NM10 values are "1". Can you find the problem? I suspect it is the "VAR" part, or maybe some problem in the assembly of the lines. Thank you.

  • Put the whole code ... the only problem that has this code is that if NM10 equals NM9, it will not find the IGUAL1 label, because you did not create it.

  • 1

    Cristian I put as an answer to my question the whole code.

2 answers

0

@echo off
cls
set /a VAR=0
set /p NM1=
set /p NM2=
set /p NM3=
set /p NM4=
set /p NM5=
set /p NM6=
set /p NM7=
set /p NM8=
set /p NM9=
set /p NM10=

:CICLO
if %NM10% EQU %NM9% (
    goto IGUAL1
)
if %NM10% LEQ %NM9% (
    set /a VAR=%NM9%
    set /a NM9=%NM10%
    set /a NM10=%VAR%
    goto CICLO 
)

:IGUAL1
if %NM9% EQU %NM8% (
    goto IGUAL2
)
if %NM9% LEQ %NM8% (
    set /a VAR=%NM8%
    set /a NM8=%NM9%
    set /a NM9=%VAR%
    goto CICLO 
)

:IGUAL2
if %NM8% EQU %NM7% (
    goto IGUAL3
)
if %NM8% LEQ %NM7% (
    set /a VAR=%NM7%
    set /a NM7=%NM8%
    set /a NM8=%VAR%
    goto CICLO 
)

:IGUAL3
if %NM7% EQU %NM6% (
    goto IGUAL4
)
if %NM7% LEQ %NM6% (
    set /a VAR=%NM6%
    set /a NM6=%NM7%
    set /a NM7=%VAR%
    goto CICLO 
)

:IGUAL4
if %NM6% EQU %NM5% (
    goto IGUAL5
)
if %NM6% LEQ %NM5% (
    set /a VAR=%NM5%
    set /a NM5=%NM6%
    set /a NM6=%VAR%
    goto CICLO 
)

:IGUAL5
if %NM5% EQU %NM4% (
    goto IGUAL6
)
if %NM5% LEQ %NM4% (
    set /a VAR=%NM4%
    set /a NM4=%NM5%
    set /a NM5=%VAR%
    goto CICLO 
)

:IGUAL6
if %NM4% EQU %NM3% (
    goto IGUAL7
)
if %NM4% LEQ %NM3% (
    set /a VAR=%NM3%
    set /a NM3=%NM4%
    set /a NM4=%VAR%
    goto CICLO 
)

:IGUAL7
if %NM3% EQU %NM2% (
    goto IGUAL8
)
if %NM3% LEQ %NM2% (
    set /a VAR=%NM2%
    set /a NM2=%NM3%
    set /a NM3=%VAR%
    goto CICLO 
)

:IGUAL8
if %NM2% EQU %NM1% (
    goto IGUAL9
)
if %NM2% LEQ %NM1% (
    set /a VAR=%NM1%
    set /a NM1=%NM2%
    set /a NM2=%VAR%
    goto CICLO 
)

:IGUAL9
cls
echo %NM1% %NM2% %NM3% %NM4% %NM5% %NM6% %NM7% %NM8% %NM9% %NM10%
pause

The whole code.

0


The problem is that you are trying to do value assignment and sum within an argument of a command that uses parentheses, using percentages in the variables, and thus only calls variables outside the parentheses of conditions IF and command FOR

how to solve this?

use this command at the beginning of the code SETLOCAL ENABLEDELAYEDEXPANSION

and change the percentages of the variables you are calling to points exclamation variable means a variable %VAR% will look like this: !VAR!

make the appropriate changes, test the code and comment here if given right, blz?

  • 1

    It worked perfectly now, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.