Solved Why does this not work?

December 12, 2014 at 20:07:40
Specs: Windows 7
Here is my script, when it i used I get a missing operand. message, can someone explain why it doesn't work?

@ECHO OFF
:SETTING
SET /A D=1
SET /A P=1
:SETTING2
SET /A D=%D%
SET /A P=%P%
:LOOP
ECHO Type a number for the calculator to process for phi.
SET /P N=
SETLOCAL EnableDelayedExpansion
FOR /L %%A IN (1,1,%N%) DO ( SET /A X=!D! +!P!
SET /A D=%P%
SET /A P=%X%
)
ECHO %X%
PAUSE

I have tried to replace the !s with %s and it still gives me the same error.


See More: Why does this not work?

Report •


#1
December 13, 2014 at 04:14:52
You don't need to call for an arithmatic operation (SET /A) when just defining or re-defining a variable. Just use SET with no parameters.

I removed the "/A" where not needed, worked for me.

@Echo off
:SETTING
SET D=1
SET P=1
:SETTING2
SET D=%D%
SET P=%P%
:LOOP
ECHO Type a number for the calculator to process for phi.
SET /P N=">> "
SETLOCAL EnableDelayedExpansion
FOR /L %%A IN (1,1,%N%) DO (
	SET /A X=%D%+%P%
	SET D=%P%
	SET P=%X%
)
ECHO %X%
PAUSE

If infinity is infinite, is the square root of infinity infinite as well?
- Kurp Von Steiner


Report •

#2
December 13, 2014 at 16:31:01
Thank you, but no matter what I put in it gives me 2, when the answer for a self calculated equation for this would be 377. I think it has something to do with this part:

SET D=%P%
SET P=%X%

Yet I am unsure about that.

message edited by Glacier


Report •

#3
December 13, 2014 at 20:10:32
✔ Best Answer
First get rid of this:
:SETTING2
SET D=%D%
SET P=%P%

Not because it interferes with the logic, but simply on principle. It does nothing but clutter up the code.
The only other thing I had to change was % to ! in the two SETs, from this:
SET /A D=%P%
SET /A P=%X%
to this:
SET D=!P!
SET P=!X!
(I took out the /A because it's not necessary for assigning values, only for doing math)
You don't need the /A in these two steps, (but, SET /A eliminates the need for either % or !, so that's an alternative, in these two lines:
SET /A D=P
SET /A P=X
would also work. (Welcome to batch scripting!)
and you don't need the exclams in the prior SET /A, but that doesn't matter. This worked on my end.

message edited by nbrane


Report •

Related Solutions

#4
December 13, 2014 at 20:21:56
Thank you, yet, as I have stated before, the answer that I should get when I input 12 is 377, but no matter what I enter I get 2 as the answer. Here is my script so far:

@ECHO OFF
:SETTING
SET D=1
SET P=1
:LOOP
ECHO Type a number for the calculator to process for phi.
SET /P N=
SETLOCAL EnableDelayedExpansion
FOR /L %%A IN (1,1,%N%) DO (
SET /A X=%D%+%P%
SET D=!P!
SET P=!X!
)
ECHO %X%
PAUSE

I have already fixed this by using an IF command, but I am doing this to learn how to use FOR commands.

-EDIT-

I tried replacing the %N% in the IN statement, what I found is that it still only gives me 2, so I am thinking that it has something to do with with this:
SET /A X=%D%+%P%

-EDIT 2-

I figured it out, what the problem was is I needed to replace the %D% and the %P% with !D! and !P!, this gives me the needed answer, 377 when input is 12.

message edited by Glacier


Report •

#5
December 13, 2014 at 20:34:24
Thanks to everyone that has helped me here.

Report •

#6
December 13, 2014 at 21:35:02
Yeh, "spot on":
SET /A X=%D%+%P%

should be:
SET /A X=D+P

Rule of thumb, for batch: "set slash A, vars away" (I know, that sucks.) Simply, don't use variable-notation in SET /A statements. SET /A does not do algebra, so it knows that any letter-items are to be expressed as variable-values, without the usual % or !
The only exception is "system" variables, like %%a, %%z, etc. These must have notations, and then be very cautious around modulo! (expressed as %%).

message edited by nbrane


Report •

Ask Question