#1 12 Aug 2020 08:01

Minecraft49
Member
From: Poland
Registered: 08 Aug 2020
Posts: 9
Website

A auto-sum batch program

Today, I want to show you my batch file that sums prices to validate the receipt. It is:

@echo off

set sum_d=0&set sum_c=0

:autosum
if %sum_c% lss 10 echo Sum= %sum_d%.0%sum_c% $
if %sum_c% geq 10 echo Sum= %sum_d%.%sum_c% $
set d=0&set c=0
set /p "d=dollars (q to quit): "
if "%d%"=="q" exit /b %sum_d%
set /p "c=cents: "
set /a sum_d=sum_d+d
set /a sum_c=sum_c+c
if %sum_c% gtr 99 (
 set /a sum_d=sum_d+1
 set /a sum_c=sum_c-100
)
if %sum_c% lss 0 (
 rem negative number bug fixed
 set /a sum_d=sum_d-1
 set /a sum_c=sum_c+100
)
goto autosum

The sum is displayed while typing the prices. It is also a simple method for fraction calculation in Batch. Know any bugs?

Last edited by Minecraft49 (13 Aug 2020 14:35)

Offline

#2 12 Aug 2020 17:54

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,114

Re: A auto-sum batch program

No handling for negative values (probably best to reject them unless you want them for some reason, but this would rather complicate things).
Values of "c" >= 100 would give undesirable results.

Last edited by bluesxman (12 Aug 2020 17:55)


cmd | *sh | ruby | chef

Offline

#3 12 Aug 2020 18:36

RG
Member
From: Minnesota
Registered: 18 Feb 2010
Posts: 357

Re: A auto-sum batch program

Probably should reject all non-numeric chars.
Most don't hurt anything, but may not give desired results when user fat-fingers.
Comma causes undesirable results (1,000) for dollars.


Windows Shell Scripting and InstallShield

Offline

#4 13 Aug 2020 06:50

Minecraft49
Member
From: Poland
Registered: 08 Aug 2020
Posts: 9
Website

Re: A auto-sum batch program

bluesxman Yes, it does not supports negative numbers (sales). I gonna fix this, thanks for a tip.
RG Non numeric chars will return 0 when using set /a, there is not problem.

Offline

#5 13 Aug 2020 11:17

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,114

Re: A auto-sum batch program

You should do some clean up of zero padding of input (or just reject the values with a leading zero, if you're feeling user unfriendly), like if the user tries to type "08" (for example), you will get an error due to octal notation.

Side note: you are actually invoking octal interpretation by setting "sum_c=00" at the top, but it won't have any impact because 00-07 in octal are the same values as 0-7 in decimal.
—————
You need to reset your input variables at each iteration, because if the user provides no input (say there are no cents on the value, so they just press enter) then the previously entered cents value will still be in the variable.

So like this:

:autosum
set d=0
set c=0

Alternatively, you could mandate that input must always be provided by checking the errorlevel of the "set /p" commands.
—————
You should add leading zero padding to the display of "sum_c", otherwise you'll see output like:

Sum= 15.7 $

Which could be interpreted as 70¢, when in fact it is 7¢

Last edited by bluesxman (13 Aug 2020 16:32)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by FluxBB