Solved Numerical input from user only

April 4, 2015 at 00:24:58
Specs: Windows 7
Hi all,
I am struggling with this program at the moment. Program brief:

The program is meant to first ask you for the total length of material in millimetres. Next, the program will ask you for up to 8 values to process. The program adds up the values entered and subtracts it from the first value entered (Total Length). The program is meant to display a negative number if the values exceed the first value given.
How do I make sure the user can only enter a number to input?

I am sorry if I haven't posted code correctly.

@ECHO OFF
TITLE 189282

:main
COLOR 0F
CLS
DATE [/t]
TIME [/t]
ECHO.
ECHO (1) Program Instructions
ECHO (2) Add To DataBase
ECHO (3) Material Wasted
ECHO (4) View Log Files
ECHO (5) Find
ECHO (6) Add Notes
ECHO (7) Save And Exit

SET /p para1= Select from above options:
IF NOT DEFINED para1= (
GOTO main
)
IF %para1% == 1 GOTO pageNumber1 //
IF %para1% == 2 GOTO pageNumber2 //
IF %para1% == 3 GOTO pageNumber3 //
IF %para1% == 4 GOTO pageNumber4 //
IF %para1% == 5 GOTO pageNumber5 //
IF %para1% == 6 GOTO pageNumber6 //
IF %para1% == 7 GOTO pageNumber7 //
IF %para1% == 8 GOTO pageNumber8 //
IF %para1% == { GOTO pageNumber9 //
IF %para1% == ! GOTO pageNumber10 //
IF %para1% == @ GOTO pageNumber11 //
IF %para1% == # GOTO pageNumber12 //
IF %para1% == $ GOTO pageNumber13 //
IF %para1% == [ GOTO pageNumber14 //
IF %para1% == B GOTO pageNumber15 //
IF %para1% == C GOTO pageNumber16 //
IF %para1% == D GOTO pageNumber17 //
IF %para1% == E GOTO pageNumber18 //
IF %para1% == F GOTO pageNumber19 //
IF %para1% == G GOTO pageNumber20 //
IF %para1% == O GOTO pageNumber21 //
IF %para1% == I GOTO pageNumber22 //
IF %para1% == J GOTO pageNumber23 //
IF %para1% == A GOTO pageNumber24 //
GOTO main

:pageNumber1
CLS
TITLE Enter Data
COLOR 1F

:level1
CLS
ECHO Only Numerical Data
ECHO.
SET /P valueA= " Part A "
IF NOT DEFINED valueA= (
GOTO level1
)
:level2
CLS
SET /P valueB= " Part B "
IF NOT DEFINED valueB= (
GOTO level2
)
:level3
CLS
SET /P valueC= " Part C "
IF NOT DEFINED valueC= (
GOTO level3
)
:level4
CLS
SET /P valueD= " Enter D "
IF NOT DEFINED valueD= (
GOTO level4
)
:level5
CLS
SET /P valueE= " Enter E "
IF NOT DEFINED valueE= (
GOTO level5
)
:level6
CLS
SET /P valueF= " Enter F "
IF NOT DEFINED valueF= (
GOTO level6
)
:level7
CLS
SET /P valueG= " Enter G "
IF NOT DEFINED valueG= (
GOTO level7
)
:level8
CLS
SET /P valueH= " Enter H "
IF NOT DEFINED valueH= (
GOTO level8
)
CLS
set /a A1=%numvalue1%+%valueA%
set /a A2=%numvalue2%+%valueB%
set /a A3=%numvalue3%+%valueC%
set /a A4=%numvalue4%+%valueD%
set /a A5=%numvalue5%+%valueE%
set /a A6=%numvalue6%+%valueF%
set /a A7=%numvalue7%+%valueG%
set /a A8=%numvalue8%+%valueH%


SET /A Total=%A2%+%A3%+%A4%+%A5%+%A6%+%A7%+%A8%
SET /A OverAll=%A1%-%Total%

ECHO Total Size = %Total%
ECHO OverAll = %OverAll%
PAUSE >NULL
GOTO pageNumber24

:pageNumber2
:pageNumber3
:pageNumber4
:pageNumber5
:pageNumber6
:pageNumber7

GOTO exit
:pageNumber8
:pageNumber9
:pageNumber10
:pageNumber11
:pageNumber12
:pageNumber13
:pageNumber14
:pageNumber15
:pageNumber16
:pageNumber17
:pageNumber18
:pageNumber19
:pageNumber20
:pageNumber21
:pageNumber22
:pageNumber23
:pageNumber24

:exit
ECHO Exit Program
PAUSE> EXIT

message edited by WhiteBoard


See More: Numerical input from user only

Report •


✔ Best Answer
April 5, 2015 at 20:09:05
another way: do a test-arithmatic?
@ECHO OFF & SETLOCAL
:1
SET K=
SET /P K=INPUT
IF NOT DEFINED K GOTO :EOF
set /a K+=0
if %K% LEQ 0 (
echo input is zero or neg or notnumber
) ELSE (
ECHO NUMBER: %K%
)
GOTO :1
::=========
but i haven't done much testing...

message edited by nbrane



#1
April 4, 2015 at 01:17:09
I don't know if win7 has choice.

Try this:

choice /?

You either get a help msg or the bad command msg.

=====================

M2 Get custom script or take private lessons


Report •

#2
April 4, 2015 at 01:52:36
Hi Mechani,

Win7 gives help message for command choice /?


Report •

#3
April 4, 2015 at 02:51:48
Your life just got easier.

choice /C:0123456789

Will accept only those chars.

And set errorlevel, so you can code

choice /C:0123456789
call :!ERRORLEVEL!
goto :eof

:1
echo here's 1
goto :eof

... and so on

=====================

M2 Get custom script or take private lessons


Report •

Related Solutions

#4
April 4, 2015 at 10:42:04
Run my code as a filename.bat

If you enter (1) the program will run.

- You need to Enter the largest size known first.
- Input users measurements
- Output users Material
- Output users Waste
- Output users Yield
- Output Total Lost

Work in progress atm.


Report •

#5
April 5, 2015 at 20:09:05
✔ Best Answer
another way: do a test-arithmatic?
@ECHO OFF & SETLOCAL
:1
SET K=
SET /P K=INPUT
IF NOT DEFINED K GOTO :EOF
set /a K+=0
if %K% LEQ 0 (
echo input is zero or neg or notnumber
) ELSE (
ECHO NUMBER: %K%
)
GOTO :1
::=========
but i haven't done much testing...

message edited by nbrane


Report •

#6
April 7, 2015 at 00:32:02
Hi nbrane,

That's pretty smooth.

The approach I was taking was way too clumsy.

=====================

M2 Get custom script or take private lessons


Report •

Ask Question