0

How can I make the sum of the imputed numbers given by the user from the keyboard ? I tried a for loop but is endless. I cannot figure out a break statement.

::@ECHO OFF
setlocal EnableDelayedExpansion
set /p X=
SET /A suma=0
:Loop
FOR %%G IN (%X%) DO (
    IF %%G GTR 0 (
        SET /A suma=%suma%+%%G
    
    )

)
SHIFT
GOTO Loop
:completed
echo %suma%

What i want to do is to make the program more user friendyl like echo somthing for the user to know it is supposed to write somthing. I know the correct code for this question, but the numbers are given as parameters in the command (syntax: script.bat parameter1 parameter2 parameter3).

::@ECHO OFF
SET /A suma=0
:Loop
IF "%1"=="" GOTO completed
FOR %%F IN (%1) DO (
    IF %1 GTR 0 (
        SET /A suma=%suma%+%1
    )
)
SHIFT
GOTO Loop
:completed
echo %suma%

Please give me any idea for the breaking statement in the first example or how can I modify the code to take the numbers from the users imput variable and not as paramaters.

Thank you.

3
  • I would advise that you use a more appropriate method of addition, SET /A suma += %%G, or in the lower version SET /A suma += %~1. That way, you will not need to enable delayed expansion or change =%suma%+ to =!suma!+. Commented Oct 28, 2022 at 18:49
  • Thank you for the advaice, the problem is I am a noob and I started a postuniversity school 3 weeks ago and I don't know many things. Excuse me but, I didn't get it. Please, if you have time explain me what I am supposed to use ? Commented Oct 29, 2022 at 10:54
  • As with almost any command, just type the command name followed by its help option in a Command Prompt window, then press your [ENTER] key, to find out how to use it. In this case set /? should provide output under the /A option section which shows the += assignment operator, along with others. It should also tell you "Any non-numeric strings in the expression are treated as environment variable names whose values are converted to numbers before using them. This allows you to do arithmetic with environment variable values without having to type all those % signs to get their values.". Commented Oct 29, 2022 at 11:08

1 Answer 1

2
SET /A suma=0
set "X=%*"
if not defined X set /p "X=No parameters detected "
if not defined X ECHO No user-input detected&goto completed
FOR %%G IN (%X%) DO (
    IF %%G GTR 0 (
        SET /A suma=suma+%%G
    
    )

)
:completed
echo %suma%

%* means the command-line tail. If no tail, then ask user for input. If still no data, issue message and show result.

shift moves the command line tail one position, so %2 becomes %1, etc. You haven't provided a test to exit the routine - and the command-line tail is presumably empty, so you are shifting it (which does nothing) and looping.

The for...%%G.. processes the entire list %X%, so there's no point in the shift/loop - the data has already been processed.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.