I want to input numbers into a program from the command line.
$ ./myprogram
Please iput number:
<do program>
The input number are unsigned 64-bit (uint64)
I tried the following:
let var = uint64(readling(stdin, input)
But I apparently have to convert a string to an uint.
Use parseBiggestUInt proc in strutils?
You can do like this:
import strutils
let theint = stdin.readline.parseBiggestUInt
or
import strutils
var theint = -1'u64
try:
theint = stdin.readline.parseBiggestUInt
except ValueError:
# do something
These don't work.
When the program displays
Enter number value:
it should stay there until I type a number and hit <enter> and then proceed. For both cases shown the program just falls through.
[jzakiya@jabari-pc nim]$ ./myprogram
Enter number value:
Error: unhandled exception: Unknown IO Error [IOError]
Essentially, I want to do the equivalent C++ code below to enter nunbers.
cout << "Enter number value: ";
uint64 val;
cin >> val;
How do you write your code? AFAIK, readLine is blocking.
In my machine, below code is working fine. Using Windows 10 with GCC v 7.1.0
import strutils, typetraits
stdout.write "Input integer number: "
let number = stdin.readline.parseBiggestUInt
echo "You inputed ", number, " with type ", number.type.name
I'm am running Manjaro KDE (gcc 7.1.1, clang 4.0.1) in a VirtualBox VM using another Linux distros as VB host.
When I run this file:
inputtest.nim
import strutils, typetraits
stdout.write "Enter integer number: "
let val = stdin.readline.parseBiggestUInt
echo "You inputed ", val, " with type ", val.type.name
Here are the compile and runtime results
[jzakiya@jabari-pc nim]$ nim c --cc:clang --d:release inputtest.nim
Hint: used config file '/etc/nim.cfg' [Conf]
Hint: system [Processing]
Hint: inputtest [Processing]
Hint: strutils [Processing]
Hint: parseutils [Processing]
Hint: math [Processing]
Hint: algorithm [Processing]
Hint: typetraits [Processing]
CC: inputtest
CC: stdlib_system
CC: stdlib_strutils
CC: stdlib_typetraits
CC: stdlib_parseutils
CC: stdlib_math
CC: stdlib_algorithm
Hint: [Link]
Hint: operation successful (14862 lines compiled; 0.920 sec total; 17.938MiB peakmem; Release Build) [SuccessX]
[jzakiya@jabari-pc nim]$ ./inputtest.nim
bash: ./inputtest.nim: Permission denied
[jzakiya@jabari-pc nim]$
Whoops, sorry, forgot to run the executable (probably should go to bed now)
[jzakiya@jabari-pc nim]$ ./inputtest
Enter integer number: 34422
You inputed 34422 with type BiggestUInt
[jzakiya@jabari-pc nim]$
Made the same mistake on target program, but it works now too. Definitely time to go to bed now.