Here is my code:

@echo off

title Gag Calculator

set /p "ans1=Please enter the first gag's damage."

set var1=%ans1%

set /p "ans2==Please enter the second gag's damage."

set var2=%ans2%

set var3=0.20

set var4=%var1%+%var2%*%var3%

set var5=%var1%+%var2%+%var4%

echo.%var5%

pauseMy problem is that it gives me this error when I put the first number in:

Please enter the first gag's damage.1

The syntax of the command is incorrect.

1++1+*0.20

Press any key to continue . . .Whats wrong? I want the calculator to add the first number and the second number together then multiply it by 0.20[var3](20%, which is the amount of synergy bonus it gives), then add the synergy to the sum by adding the synergy (var4) and the other values(var1 and var2). Also I am planning on adding an option for 3 and 4 gags also so could someone tell me how I could have someone select how many gags(2,3, or 4) then do the math and print the product? Thanks

✔ Best Answer

I did my tests with small numbers and got the expected results every time. I tested again with the numbers you provided and got the same wierd results that you did. This really had me intrigued. It seemed to be remembering the previous value of ansN when assigning varN. I think with compound statements you have to use delayed variable expansion. I am curious where you came up with your syntax for your SET comands? set /p "ans1=Please enter the first gag's damage." set var1=%ans1%I have never seen it used that way before, but since it seemed to be working in my tests, I thought maybe you knew something I didn't. I'm not sure why you wanted to create working variables for all of your prompt variables, but I think that was the source of the problems. I did "SET /?" and rewrote all the SET /P commands using the recommended syntax. I used your numbers and checked results with a calculator.

::@echo off title Gag Calculator set /p varA=Please enter the number of gag's (2, 3 or 4). set /p var1=Please enter the first gag's damage. set /p var2=Please enter the second gag's damage. if "%varA%"=="3" set /p var3=Please enter the third gag's damage. if "%varA%"=="4" set /p var3=Please enter the third gag's damage. if "%varA%"=="4" set /p var4=Please enter the fourth gag's damage. echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy% if "%varA%"=="2" set /a sum=(var1 + var2) * 120 & set /a synergy=(var1 + var2) * 20 echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy% if "%varA%"=="3" set /a sum=(var1 + var2 + var3) * 120 & set /a synergy=(var1 + var2 + var3) * 20 echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy% if "%varA%"=="4" set /a sum=(var1 + var2 + var3 + var4) * 120 & set /a synergy=(var1 + var2 + var3 + var4) * 20 echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy% set sum=%sum:~0,-2%.%sum:~-2% set synergy=%synergy:~0,-2%.%synergy:~-2% echo.sum=%sum% synergy=%synergy% set varA= set var1= set var2= set var3= set var4= set sum= set synergy=It'a a good idea to clean up your environment (eg. deleting all variables) at the end of any batch program. It's also a good idea to test that your variables are not already in use at the beginning of any batch program like this:

if not "%varA%"=="" goto errmsgNNI also initialize all variables as a matter of habit. For example:

set varA=The bigger your program gets, the more important this becomes.

By debugging, the most common methods are to leave echo on by commenting out "@Echo off" with "::" or "REM", and putting in some echo statements to tell you what's going on, like:

echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy%If you have a complicated program, you can create a log of what happens by running your program like this (with echo on):

math1 > math1.logI hope this addresses all of your original question. If not, post issues here. Go ahead and try to make the other modifications you had planned, and if there is anything you cant get to work, post your problem in a new thread.

A lot more is wrong than would appear on the surface. The obvious is that you need "SET /A" to do math. The less obvious is that SET will only do integer math, and the "0" in "0.20" tell SET that you want to do octal arithmetic. Also, with "SET /A" you don't need all the percent signs (but OK if you want). The syntax error was from the "==" in the second "SET /P". Here is what I have come up with, but if you want to enter floating point numbers, it's going to be a lot more complicated! @echo off title Gag Calculator set /p "ansA=Please enter the number of gag's (2, 3 or 4)." set varA=%ansA% set /p "ans1=Please enter the first gag's damage." set var1=%ans1% set /p "ans2=Please enter the second gag's damage." set var2=%ans2% if "%varA%"=="3" (set /p "ans3=Please enter the third gag's damage." set var3=%ans3% ) if "%varA%"=="4" (set /p "ans3=Please enter the third gag's damage." set var3=%ans3% ) if "%varA%"=="4" (set /p "ans4=Please enter the fourth gag's damage." set var4=%ans4% ) echo %var1% %var2% %var3% %var4% if "%varA%"=="2" set /a var5=(var1 + var2) * 120 if "%varA%"=="3" set /a var5=(var1 + var2 + var3) * 120 if "%varA%"=="4" set /a var5=(var1 + var2 + var3 + var4) * 120 set var5=%var5:~0,-2%.%var5:~-2% echo.%var5% pause

Thanks for the reply but that doesn't seem to work with anything but 2 numbers. Two questions though, in the code I see that this is the same, but with different options: if "%varA%"=="3" (set /p "ans3=Please enter the third gag's damage."

set var3=%ans3%

)

if "%varA%"=="4" (set /p "ans3=Please enter the third gag's damage."

set var3=%ans3%

)Why is this? Also in this:

if "%varA%"=="2" set /a var5=(var1 + var2) * 120

if "%varA%"=="3" set /a var5=(var1 + var2 + var3) * 120

if "%varA%"=="4" set /a var5=(var1 + var2 + var3 + var4) * 120I see that its times "120", why is it 120 when it really does 0.20, meaning 20%?

PS another thing is that I want to be able to press enter to start over once I get my answer, which I accomplished by adding ":start" at the beginning and "cls goto start" after pause at the end. Try that with the code you gave me and see what happens, it seems to continually set values to 4 numbers. Could you make it reset the numbers and work properly? After the 2nd time of going through the same run it finally sets values to 3. Forgive me if this is confusing, let me know if it is and I'll try to explain it better.

ALSO I want the program to tell me what level the combined damage will defeat. So if the combined damage does 6 OR MORE total with synergy I want it to echo "This will defeat a level 1." I can do that but the next level requires 12 damage to defeat, so if the damage is more than 6 but less than 12 it would say "this will defeat a level 1". But if it was more than 12 and less than the next number(20) it would say "This will defeat a level 2", and ignore the level 1 phrase. Could you show me an example of that, if I see it I can copy and make the rest of the levels so I don't waste too much more of your time:)

It worked for me - 2, 3 or 4 gags, whichever - ????? "

Why is this?"

That's a logical OR (if varA=3 OR varA=4 do same thing)."

I see that its times "120", why is it 120 when it really does 0.20, meaning 20%?"A + B + (A + B) * 0.20 = (A + B) * 1.20 = (A + B) * 120 / 100

"SET /A" can't do floating point arithmetic, so to get the two decimals, I multiplied everything by 100, then moved the decimal point two places to the left in the result.

If I have not fully addressed your original question, please let me know specifically what is not working right (in this thread) and I will help you work out the bugs. If your original question has been fully addressed, then please show your appreciation by setting one of my replies as the best answer. This goes a long way toward getting more quality help from everyone on this forum.

This is sounding a lot like a school project. Please do your own debugging and create a new posting when you have a specific question. Please be specific.

OK, I see that you also wanted to know what the synergy was. I updated the script to give you both numnbers. If you have specific questions about this script, post them here. If you want to modify it to do other things, please post specific questions in new threads. @echo off title Gag Calculator set /p "ansA=Please enter the number of gag's (2, 3 or 4)." set varA=%ansA% set /p "ans1=Please enter the first gag's damage." set var1=%ans1% set /p "ans2=Please enter the second gag's damage." set var2=%ans2% if "%varA%"=="3" (set /p "ans3=Please enter the third gag's damage." set var3=%ans3% ) if "%varA%"=="4" (set /p "ans3=Please enter the third gag's damage." set var3=%ans3% ) if "%varA%"=="4" (set /p "ans4=Please enter the fourth gag's damage." set var4=%ans4% ) if "%varA%"=="2" set /a sum=(var1 + var2) * 120 & set /a synergy=(var1 + var2) * 20 if "%varA%"=="3" set /a sum=(var1 + var2 + var3) * 120 & set /a synergy=(var1 + var2 + var3) * 20 if "%varA%"=="4" set /a sum=(var1 + var2 + var3 + var4) * 120 & set /a synergy=(var1 + var2 + var3 + var4) * 20 set sum=%sum:~0,-2%.%sum:~-2% set synergy=%synergy:~0,-2%.%synergy:~-2% echo.sum=%sum% synergy=%synergy% pause

This does not work for me at all. This is what happensPlease enter the number of gag's (2, 3 or 4).4

Please enter the first gag's damage.25

Please enter the second gag's damage.26

Please enter the third gag's damage.25

Please enter the fourth gag's damage.26

sum=61.20 synergy=10.20

Press any key to continue . . .25+25+26+26 is 102 not 51 and the synergy should be 20.40 not 10.20.... Totaling 122.40... Seems like its only counting the first two.

EDIT it seems like it IS only adding the first two:

Please enter the number of gag's (2, 3 or 4).3

Please enter the first gag's damage.99

Please enter the second gag's damage.99

Please enter the third gag's damage.32434

sum=237.60 synergy=39.60

Press any key to continue . . .

By the way this isn't a school project, and I don't know how to debug unless you mean leaving the echo on.

I did my tests with small numbers and got the expected results every time. I tested again with the numbers you provided and got the same wierd results that you did. This really had me intrigued. It seemed to be remembering the previous value of ansN when assigning varN. I think with compound statements you have to use delayed variable expansion. I am curious where you came up with your syntax for your SET comands? set /p "ans1=Please enter the first gag's damage." set var1=%ans1%I have never seen it used that way before, but since it seemed to be working in my tests, I thought maybe you knew something I didn't. I'm not sure why you wanted to create working variables for all of your prompt variables, but I think that was the source of the problems. I did "SET /?" and rewrote all the SET /P commands using the recommended syntax. I used your numbers and checked results with a calculator.

::@echo off title Gag Calculator set /p varA=Please enter the number of gag's (2, 3 or 4). set /p var1=Please enter the first gag's damage. set /p var2=Please enter the second gag's damage. if "%varA%"=="3" set /p var3=Please enter the third gag's damage. if "%varA%"=="4" set /p var3=Please enter the third gag's damage. if "%varA%"=="4" set /p var4=Please enter the fourth gag's damage. echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy% if "%varA%"=="2" set /a sum=(var1 + var2) * 120 & set /a synergy=(var1 + var2) * 20 echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy% if "%varA%"=="3" set /a sum=(var1 + var2 + var3) * 120 & set /a synergy=(var1 + var2 + var3) * 20 echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy% if "%varA%"=="4" set /a sum=(var1 + var2 + var3 + var4) * 120 & set /a synergy=(var1 + var2 + var3 + var4) * 20 echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy% set sum=%sum:~0,-2%.%sum:~-2% set synergy=%synergy:~0,-2%.%synergy:~-2% echo.sum=%sum% synergy=%synergy% set varA= set var1= set var2= set var3= set var4= set sum= set synergy=It'a a good idea to clean up your environment (eg. deleting all variables) at the end of any batch program. It's also a good idea to test that your variables are not already in use at the beginning of any batch program like this:

if not "%varA%"=="" goto errmsgNNI also initialize all variables as a matter of habit. For example:

set varA=The bigger your program gets, the more important this becomes.

By debugging, the most common methods are to leave echo on by commenting out "@Echo off" with "::" or "REM", and putting in some echo statements to tell you what's going on, like:

echo varA=%varA% var1=%var1% var2=%var2% var3=%var3% var4=%var4% sum=%sum% synergy=%synergy%If you have a complicated program, you can create a log of what happens by running your program like this (with echo on):

math1 > math1.logI hope this addresses all of your original question. If not, post issues here. Go ahead and try to make the other modifications you had planned, and if there is anything you cant get to work, post your problem in a new thread.

OK thank you very much! The program works 100% but the math1 thing doesnt, it makes a math1.log file thats blank, after I run the whole program through. And if I set that as best answer can you still reply here?

Selecting a "Best Answer" doesn't do much outside of adding [Solved] to the title and adding a green check mark.

OK, I set as best answer! Thanks for your help sburtchin! And thanks for replying Razor2.3!

Ugh, sorry one more thing. When I add this to the code, everything else works but look what happens:

CODE added

if %sum% GEQ 6 echo.This will kill a level 1

if %sum% GEQ 12 echo.This will kill a level 2

if %sum% GEQ 20 echo.This will kill a level 3

if %sum% GEQ 30 echo.This will kill a level 4

if %sum% GEQ 42 echo.This will kill a level 5

if %sum% GEQ 56 echo.This will kill a level 6

if %sum% GEQ 72 echo.This will kill a level 7

if %sum% GEQ 90 echo.This will kill a level 8

if %sum% GEQ 110 echo.This will kill a level 9

if %sum% GEQ 132 echo.This will kill a level 10

if %sum% GEQ 156 echo.This will kill a level 11

if %sum% GEQ 182 echo.This will kill a level 12Output:

Please enter the number of gag's (2, 3 or 4). 4

Please enter the first gag's damage. 50

Please enter the second gag's damage. 50

Please enter the third gag's damage. 50

Please enter the fourth gag's damage. 50

Total=240.00 Total Synergy=40.00

This will kill a level 2

This will kill a level 3

This will kill a level 9

This will kill a level 10

This will kill a level 11

This will kill a level 12

Please enter the number of gag's (2, 3 or 4).What the heck is wrong now? Why is it randomly picking a few of those phrases and printing them. Its doing the math right, but I just want it to print one line, say if the sum=200 it will print "this will kill a level 12" and NOTHING else. Or if sum=20 print "this will kill a level 3" and NOTHING ELSE. I am having trouble doing this, any ideas?

Why is it randomly picking a few of those phrases and printing them

I'm not going to bother looking at the code, so does %sum% include the decimal mark? If it does, you're performing a text comparison, and "2" < "6" (lvl 1), but "2" > "1" (lvl 2), and "24" > "20" (lvl 3), etc.

print . . . and NOTHING ELSE.

Your problem is you're looking at the set of statements as a whole. That's not what CMD does. It looks at each line and evaluates it before moving onto the next. The easiest way to fix this would be to have some variable that keeps track of the level, then just print that level.SET lvl= if %sum% GEQ 6 SET lvl=1 . . . . if %sum% GEQ 182 SET lvl=12 IF DEFINED lvl echo.This will kill a level %lvl%

I try that and get: Please enter the number of gag's (2, 3 or 4). 4

Please enter the first gag's damage. 1

Please enter the second gag's damage. 1

Please enter the third gag's damage. 1

Please enter the fourth gag's damage. 1

Total=4.80. Total Synergy=.80.

This will kill a level 12

Press enter to continue.Thats not right, it shouldn't even get a level 1..... At least its only printing one "this will kill..." phrase now.

EDIT: and yes, %sum% includes a decimal point.

Then you're doing a text comparison ("4.80" vs "182"), and not a number / value comparison (4.80 vs 182). The rules are different. 4.8 might be less than 182, but the character "4" is greater than the character "1", so "4.80" > "182". EDIT: The solution is to use some integer value CMD recognizes as a number.

What about 182.00?

EDIT: That did NOT work. Still says level 12 when I put it in so that it should barely kill a level 3.

What about 182.00?

"4" is still greater than "1". I get the feeling you're not listening here, and that makes me sad.

OH I see now, how do I make it understand that 4 is less than 1(82)?

QUOTE:

"but the math1 thing doesnt, it makes a math1.log file thats blank'Generally you would want to do this with echo ON as I said ("

common methods are to leave echo on by commenting out "@Echo off""). Even with echo OFF it should not have been completely blank. With all the prompting you just have to wing it on the input. With no promtps on the screen, I entered 3<Enter> 2<Enter> 3<Enter> 4<Enter> and all the prompts and results were in the log file as expected.

how do I make it understand that 4 is less than 1(82)?Remember what I said: "

The less obvious is that SET will only do integer math". This is also true of IF. CMD doesn't understand that 4.80 is a number because integers don't have decimal points. 180 can be a number or a string. You wanted two decimal places in the math. Did you follow why I multiplied everything by 100? Hint: use just the integer part of the string sum to do your GEQ comparisons.Computers can't think. If you want to do floating point arithmetic in a language that only supports integers, then YOU have to do all the adjustments for the decimal point.

EDIT:

The computer interprets one statement at a time. It has no memory of what statements it has seen in the past, and has no idea what statements it might see in the future.CMD /?, SET /?, IF /?, ECHO /? - Everything you need to know to write this program.

More to the point, do the checks to %sum% before adding a decimal point to %sum%. (I'm not sure why you're adding decimal points to %sum% to begin with.)

Did you follow why I multiplied everything by 100?YES thank you! I did that here:

set /a sum=sum * 100

if %sum% GEQ 600 SET lvl=1

if %sum% GEQ 1200 SET lvl=2

if %sum% GEQ 2000 SET lvl=3

if %sum% GEQ 3000 SET lvl=4

if %sum% GEQ 4200 SET lvl=5

if %sum% GEQ 5600 SET lvl=6

if %sum% GEQ 72000 SET lvl=7

if %sum% GEQ 9000 SET lvl=8

if %sum% GEQ 11000 SET lvl=9

if %sum% GEQ 13200 SET lvl=10

if %sum% GEQ 15600 SET lvl=11

if %sum% GEQ 18200 SET lvl=12

Thank you so much it works 100% now. I think I learned alot about if and set during this whole thing. Thank you both very much!

Did you follow why I multiplied everything by 100?

YES thank you! I did that here:

set /a sum=sum * 100

if %sum% GEQ 600 SET lvl=1I get the impression you missed the point. It was all about managing the decimal point in this integer and string universe. With my hint I was hoping you would see that the solution was already there:

set sum=%sum:~0,-2%.%sum:~-2%You just needed to interpolate:

set intsum=%sum:~0,-2% set sum=%sum:~0,-2%.%sum:~-2%You should understand why this manipulation of the string 'sum' is the same as integer division by 100 of the value 'sum'. Then:

if %intsum% GEQ 6 SET lvl=1I don't know why you would want to multiply everything by 100 again because now all your level tests are all 100 times greater than reality. Beyond being a neat exercise in string processing, I would have to agree with Razor2.3 that with all your levels requiring such large integers in comparison to numbers less than 1, there was never a need for floating point math calculations. On the other hand, this looks more like an exercise in pleasure. That being said, I think it would be a good learning experience for you to modify your program to accept real numbers as input, use real numbers for the levels, and output likewise.

Hint: Try writing it to

just acceptonly real numbers with exactly 2 decimal places. Add complexity in small steps. Don't bite off more than you can chew! Remember always that computers have ZERO capacity to think.

I just started programming, and I'm only 14, so I'm sorry if I dont understand the set intsum=%sum:~0,-2%

set sum=%sum:~0,-2%.%sum:~-2%stuff. What do "~0,-2" and "~-2" do?

I understand the "if %intsum% GEQ", just not how the intsum line works; and I feel like I just used you guys to build the program if I dont/cant understand how it works 100%.

Again thank you guys sooo much for the help.

Open a Command Prompt, type in set /?, and start reading. It's how the rest of us learned.(We're technically doing fixed point math.)

(This is how currency should be saved and calculated in programs.)

Okay Razor2.3 I did that, read it top to bottom, and still arent 100% sure what this does: set sum=%sum:~0,-2%.%sum:~-2%.

Based on my reading of that Im understanding that it gets 2 numbers before 1 or zero? Then it does something with subtracting a 2.... I'm guessing I'm wrong...

Let me quote the relevant part: May also specify substrings for an expansion. %PATH:~10,5% would expand the PATH environment variable, and then use only the 5 characters that begin at the 11th (offset 10) character of the expanded result. If the length is not specified, then it defaults to the remainder of the variable value. If either number (offset or length) is negative, then the number used is the length of the environment variable value added to the offset or length specified. %PATH:~-10% would extract the last 10 characters of the PATH variable. %PATH:~0,-2% would extract all but the last 2 characters of the PATH variable.In short, he's grabbing all but the last 2 characters, and he's grabbing the last 2 characters.

~~Ok, and doing what with them?~~Edit: Holy cow I think I understand...?

If I do 25 + 25 + 25 + 25 = 100.

In " if "%varA%"=="4" set /a sum=(var1 + var2 + var3 + var4) * 120 "

Then 100 * 120 = 12000.

Then set sum=%sum:~0,-2%.%sum:~-2%, which takes the 120 part. Out of 12000... right?

But what does it do with the two zeros?(Last characters.)120(or 12000)= The same thing as I did: 25+25+25+25=100*0.20=20+100=120.

Except the obvious that the 12000 method actually works.My only question is what does he do with the last 2 characters?

in the example of sum=12000

%sum:~0,-2%.%sum:~-2%

the part before the . is %sum% without the last two

120

then the

.

the part after the . is the last two of the number

00

so this is 120.00

if you just wanted the 120 you could use set sum=%sum:~0,-2%mike

OHHHHHHHHHHHHHHHHH WOW *facepalm* I cant believe I couldnt figure that out. Thanks so much mikelinus!

Took me a while too, The other trick is to remember the start is 0, my brain wants to start at 1. To be fair much of the stuff I have picked up has been reading this sites other posts(most of the other guys that are here are far smarter than I am), and of course searching. mike

Yes I agree, and me too Ive learned alot from these guys.

QUOTE:

"Except the obvious that the 12000 method actually works. My only question is what does he do with the last 2 characters?"I think maybe this is what you are referring to. At this point, sum is 100 times larger than it is supposed to be. If these were real numnbers, I could divide by 100, but since we are doing 'GEQ' comparisons to integers, any decimal amount would always be irrelevant. So when creating the string intsum, the last two characters are just not needed or used. You could say figuratively that they were thrown in the trash (unless you kept the stuff that prints out sum with two decimal palces).

set intsum=%sum:~0,-2% . . . if %intsum% GEQ 6 SET lvl=1NOTE: CMD automatically converts the string intsum to a value before making the GEQ comparison to the value 6, and so on.

Ask Your Question

Weekly Poll