Browse Source

Limit the input value in the range [0, 26[

DricomDragon 5 years ago
parent
commit
e552b1e73f
1 changed files with 9 additions and 3 deletions
  1. 9 3
      io_cesar.hs

+ 9 - 3
io_cesar.hs

@@ -8,9 +8,15 @@ cesar n = map (cechar n)
 
 inlinecesar n s = map (chr.(+ ord 'a').(flip mod 26).(+ n).(subtract (ord 'a')).ord) s
 
+getCorrectValue::IO Int
+getCorrectValue = do
+	x <- getLine
+	let i = (read x)::Int
+	if i >= 0 && i < 26 then return i else getCorrectValue
+
 main::IO ()
 main = do
-	n <- getLine
+	n <- getCorrectValue
 	s <- getLine
-	putStrLn (cesar (read n) s)
-	putStrLn (inlinecesar (read n) s)
+	putStrLn (cesar n s)
+	putStrLn (inlinecesar n s)