sanitize user input
This commit is contained in:
parent
c6e598ff64
commit
5549aa5e28
|
@ -1,5 +1,19 @@
|
|||
import strutils, sequtils
|
||||
|
||||
proc getInt(): int =
|
||||
while(true):
|
||||
try:
|
||||
return parseInt(readline(stdin))
|
||||
except:
|
||||
echo "Please enter an integer, try again."
|
||||
|
||||
proc getFloat(): float =
|
||||
while(true):
|
||||
try:
|
||||
return parseFloat(readline(stdin))
|
||||
except:
|
||||
echo "Please enter a number, try again."
|
||||
|
||||
#? prints a matrix to the standard output
|
||||
proc printMatrix*(matrix: seq[seq[float]]) =
|
||||
let row: int = matrix.len
|
||||
|
@ -11,8 +25,8 @@ proc fillMatrix*(matrix: var seq[seq[float]], row, col: int) =
|
|||
for i in countup(1, row):
|
||||
for j in countup(1, col):
|
||||
echo "Enter item:"
|
||||
var entry: float = parseFloat(readLine(stdin)) # TODO: while loop to check if input is valid
|
||||
matrix[i-1].add(entry)
|
||||
# var entry: float = getFloat() # TODO: while loop to check if input is valid
|
||||
matrix[i-1].add(getFloat())
|
||||
matrix[i-1].delete(0)
|
||||
echo matrix[i-1]
|
||||
|
||||
|
@ -50,9 +64,9 @@ proc dot*() =
|
|||
|
||||
#? record first matrix
|
||||
echo "Enter number of rows in the first matrix:"
|
||||
let r1: int = parseInt(readLine(stdin))
|
||||
let r1: int = getInt()
|
||||
echo "Enter number of columns in the first matrix:"
|
||||
let c1: int = parseInt(readLine(stdin))
|
||||
let c1: int = getInt()
|
||||
var m1 = newSeqWith(r1, newSeq[float](c1))
|
||||
procedures.fillMatrix(m1, r1, c1)
|
||||
|
||||
|
@ -60,9 +74,9 @@ proc dot*() =
|
|||
|
||||
#? record second matrix
|
||||
echo "Enter number of rows in the second matrix:"
|
||||
let r2: int = parseInt(readLine(stdin))
|
||||
let r2: int = getInt()
|
||||
echo "Enter number of columns in the second matrix:"
|
||||
let c2: int = parseInt(readLine(stdin))
|
||||
let c2: int = getInt()
|
||||
var m2 = newSeqWith(r2, newSeq[float](c2))
|
||||
procedures.fillMatrix(m2, r2, c2)
|
||||
|
||||
|
@ -89,12 +103,12 @@ proc cross*() =
|
|||
echo "Enter numbers in the first vector:"
|
||||
for i in 0..2:
|
||||
echo "Enter item:"
|
||||
v1[i] = parseFloat(readLine(stdin))
|
||||
v1[i] = getFloat()
|
||||
|
||||
echo "Enter numbers in the second vector:"
|
||||
for i in 0..2:
|
||||
echo "Enter item:"
|
||||
v2[i] = parseFloat(readLine(stdin))
|
||||
v2[i] = getFloat()
|
||||
|
||||
#? resultent vector
|
||||
let v = calcCross(v1, v2)
|
||||
|
|
Loading…
Reference in a new issue