Reading an integer from user input
You can convert the string to integer using Convert.ToInt32() function
int intTemp = Convert.ToInt32(Console.ReadLine());
How to read an integer input from the user input in dart
The readLineSync() method returns a String?
. The ?
symbol indicates this variable may be null.
On the other hand, the int.parse() method expects a String
, without the ?
symbol. This means it doesn't know how to handle if the result from the readLine method comes null.
The easiest way to solve this is to give a default value in case the result comes null:
int num1 = int.parse(stdin.readLineSync() ?? '0');
The ??
operator makes the expression evaluates to the right side, if the left side is null. So giving it a default value it won't have to bother with a nullable type.
There are other operators you can use. You can read more about it in the Dart documentation about it.
How can I read inputs as numbers?
Solution
Since Python 3, input
returns a string which you have to explicitly convert to int
s, with int
, like this
x = int(input("Enter a number: "))
y = int(input("Enter a number: "))
You can accept numbers of any base and convert them directly to base-10 with the int
function, like this
>>> data = int(input("Enter a number: "), 8)
Enter a number: 777
>>> data
511
>>> data = int(input("Enter a number: "), 16)
Enter a number: FFFF
>>> data
65535
>>> data = int(input("Enter a number: "), 2)
Enter a number: 10101010101
>>> data
1365
The second parameter tells what is the base of the numbers entered and then internally it understands and converts it. If the entered data is wrong it will throw a ValueError
.
>>> data = int(input("Enter a number: "), 2)
Enter a number: 1234
Traceback (most recent call last):
File "<input>", line 1, in <module>
ValueError: invalid literal for int() with base 2: '1234'
For values that can have a fractional component, the type would be float
rather than int
:
x = float(input("Enter a number:"))
Differences between Python 2 and 3
Summary
- Python 2's
input
function evaluated the received data, converting it to an integer implicitly (read the next section to understand the implication), but Python 3'sinput
function does not do that anymore. - Python 2's equivalent of Python 3's
input
is theraw_input
function.
Python 2.x
There were two functions to get user input, called input
and raw_input
. The difference between them is, raw_input
doesn't evaluate the data and returns as it is, in string form. But, input
will evaluate whatever you entered and the result of evaluation will be returned. For example,
>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
The data 5 + 17
is evaluated and the result is 22
. When it evaluates the expression 5 + 17
, it detects that you are adding two numbers and so the result will also be of the same int
type. So, the type conversion is done for free and 22
is returned as the result of input
and stored in data
variable. You can think of input
as the raw_input
composed with an eval
call.
>>> data = eval(raw_input("Enter a number: "))
Enter a number: 5 + 17
>>> data, type(data)
(22, <type 'int'>)
Note: you should be careful when you are using input
in Python 2.x. I explained why one should be careful when using it, in this answer.
But, raw_input
doesn't evaluate the input and returns as it is, as a string.
>>> import sys
>>> sys.version
'2.7.6 (default, Mar 22 2014, 22:59:56) \n[GCC 4.8.2]'
>>> data = raw_input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <type 'str'>)
Python 3.x
Python 3.x's input
and Python 2.x's raw_input
are similar and raw_input
is not available in Python 3.x.
>>> import sys
>>> sys.version
'3.4.0 (default, Apr 11 2014, 13:05:11) \n[GCC 4.8.2]'
>>> data = input("Enter a number: ")
Enter a number: 5 + 17
>>> data, type(data)
('5 + 17', <class 'str'>)
Reading an integer from input and assigning it to a variable
I don't have a Rust compiler on this machine, but based in part on this answer that comes close, you want something like...
let user_val = match input_string.parse::<i32>() {
Ok(x) => x,
Err(_) => -1,
};
Or, as pointed out in the comments,
let user_val = input_string.parse::<i32>().unwrap_or(-1);
...though your choice in integer size and default value might obviously be different, and you don't always need that type qualifier (::<i32>
) for parse() where the type can be inferred from the assignment.
Reading an integer from standard input
http://golang.org/pkg/fmt/#Scanf
All the included libraries in Go are well documented.
That being said, I believe
func main() {
var i int
_, err := fmt.Scanf("%d", &i)
}
does the trick
Reading user input as an integer
what happends when you call this
; getting user input
mov rax, 0
mov rdi, 0
mov rsi, number
mov rdx, 100
syscall
is, that your entry ( e.g. "1004") is written to the memory at "number", character per character. Now you have the exact opposite problem you intended to solve:
"how to convert an ASCII string into binary value"
the algorithm for this new problem could look like this:
(assuming char_ptr points to the string)
result = 0;
while ( *char_ptr is a digit )
result *= 10;
result += *char_ptr - '0' ;
char_ptr++;
Related Topics
Filtering Datagridview Without Changing Datasource
Conditional Operator Cannot Cast Implicitly
Entity Framework - Include Multiple Levels of Properties
Multiline String Literal in C#
Still Confused About Covariance and Contravariance & In/Out
How to Make a Window Always Stay on Top in .Net
How to Update an Observablecollection Via a Worker Thread
Unity: Null While Making New Class Instance
Execute Powershell Script from C# With Commandline Arguments
C# Constructor Execution Order
Why Doesn't C# Allow Static Methods to Implement an Interface