# \[Help\] adding two numbers

**URL:** <https://forum.crystal-lang.org/t/help-adding-two-numbers/3635>\
**Category:** Help & Support\
**Created:** [August 25, 2021, 4:50am UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635 "2021-08-25T04:50:24Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Penfore](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Penfore](https://forum.crystal-lang.org/u/Penfore)\
**Post date:** [August 25, 2021, 4:50am UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/1 "2021-08-25T04:50:24Z")

</div>

I’m a begginer to programming languages, I studied C++ and Java.

I know Crystal has its codding foundations in Ruby, but I’m not familiar to the Ruby syntax.

My problem is quite simple. I need to get two integers input from the user and then add it to a third variable.

```auto
A = gets
B = gets

X = A + B

puts "X = #{X}"

```

I get this error message:

```auto
4 | X = A + B
           ^
Error: undefined method '+' for Nil (compile-time type is (String | Nil))

```

Can someone help on this?

---

<div class="post-metadata">

**Author:** ![vlazar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/vlazar/32/31_2.png) [@vlazar](https://forum.crystal-lang.org/u/vlazar)\
**Post date:** [August 25, 2021, 6:02am UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/2 "2021-08-25T06:02:05Z")

</div>

The reason is that Crystal performs NULL checks at compile time. The compiler is telling you that when you are trying to add 2 values there can be NULL values there (`nil` in Crystal and `Nil` is corresponding type).

You are using `gets` method which can return `nil` [IO - Crystal 1.1.1](https://crystal-lang.org/api/1.1.1/IO.html#gets(limit:Int,chomp=false):String?-instance-method) (see method return type is `String?` which is shortcut for writing union type explicitly like `String | Nil`). That’s why you get this error.

To make sure your code works you need to add explicit checks for case when values can be `nil`. You can use `if/else` for that.

Also in Crystal local variable names are lowercase like `local_var_name` and names starting with capital letters are for constants, classes, etc.

So use something like that:

```auto
a = gets
b = gets

if a && b
  x = a + b
else
  x = nil
end

p! x

```

And here is an example output:

```auto
1
2
x # => "12"

```

You get `"12"` which is string because `gets` returns strings and strings can be concatenated with `+`.

See also how `p! x` prints both the expression `x` and it’s value `"12"`. You might use `p`, `p!`, `pp`, and `pp!` macros for debugging purpose. See for example [Top Level Namespace - Crystal 1.1.1](https://crystal-lang.org/api/1.1.1/toplevel.html#p!(*exps))-macro

---

<div class="post-metadata">

**Author:** ![jzakiya](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jzakiya/32/278_2.png) [@jzakiya](https://forum.crystal-lang.org/u/jzakiya)\
**Post date:** [August 25, 2021, 8:04am UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/3 "2021-08-25T08:04:43Z")

</div>

> [@Penfore](#):
>
> My problem is quite simple. I need to get two integers input from the user and then add it to a third variable.

Assuming you are using `gets` to receive input from a terminal, just do this:

```auto
a = gets.to_x
b = gets.to_x
...

```

where `to_x` is the numerical type of your input values.

So depending on the number types do: `to_i32`, `to_u32`, etc, etc

But to do numerical math you need to convert the `strings` returned by `gets` into specific numerical types, because Crystal is a `Typed` language.

(You don’t need to care, too much, about numerical value types in Ruby.)

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [August 25, 2021, 12:45pm UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/4 "2021-08-25T12:45:14Z")

</div>

`a = gets.to_x` wouldn’t work because `gets` returns `String?`. You would have to do `gets.not_nil!.to_x`.

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [August 25, 2021, 12:59pm UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/5 "2021-08-25T12:59:07Z")

</div>

Also something else I want to mention: one way to make `gets` return `nil` is by pressing `ctrl+D` on the terminal while being asked for an input. Another way is to pipe an empty file to the process.

There’s also `readline` which is equivalent to `gets.not_nil!` (though I personally don’t like it and I’d like it to be removed from the language)

---

<div class="post-metadata">

**Author:** ![RespiteSage](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/respitesage/32/793_2.png) [@RespiteSage](https://forum.crystal-lang.org/u/RespiteSage)\
**Post date:** [August 25, 2021, 2:10pm UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/6 "2021-08-25T14:10:12Z")

</div>

This isn’t the main issue, but something else to be aware of is that `A`, `B`, and `X` are [constants](https://crystal-lang.org/reference/syntax_and_semantics/constants.html) because they begin with capital Latin characters. I’m not sure, but I think that could be an issue for your code.

---

<div class="post-metadata">

**Author:** ![Penfore](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Penfore](https://forum.crystal-lang.org/u/Penfore)\
**Post date:** [August 25, 2021, 5:04pm UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/7 "2021-08-25T17:04:14Z")

</div>

> [@vlazar](#):
>
> The reason is that Crystal performs NULL checks at compile time. The compiler is telling you that when you are trying to add 2 values there can be NULL values there (`nil` in Crystal and `Nil` is corresponding type).
> 
> You are using `gets` method which can return `nil` [IO - Crystal 1.1.1](https://crystal-lang.org/api/1.1.1/IO.html#gets(limit:Int,chomp=false):String?-instance-method) (see method return type is `String?` which is shortcut for writing union type explicitly like `String | Nil`). That’s why you get this error.

I think I’m not familiar with these concepts yet.

So the behavior of gets is always assumying the input is a String? In other cases will I always have to specify the type like

```auto
a = gets.to_i

```

?

The code that worked for the little exercise was

```auto
a = gets.not_nil!.to_i
b = gets.not_nil!.to_i

x = a + b

puts "X = #{x}"

```

Does the not\_nil! works in a similar way to what you said?

---

<div class="post-metadata">

**Author:** ![Penfore](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Penfore](https://forum.crystal-lang.org/u/Penfore)\
**Post date:** [August 25, 2021, 5:12pm UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/8 "2021-08-25T17:12:51Z")

</div>

Cheking some solutions to this problem in Ruby I saw

```auto
a = gets.chomp.to_i

```

What does the chomp do? When searching for [gets](https://crystal-lang.org/api/1.1.1/IO.html#gets(limit:Int,chomp=false):String?-instance-method) in the crystal-lang API I saw there was a parameter chomp .

---

<div class="post-metadata">

**Author:** ![Penfore](https://avatars.discourse-cdn.com/v4/letter/p/ea5d25/32.png) [@Penfore](https://forum.crystal-lang.org/u/Penfore)\
**Post date:** [August 25, 2021, 5:18pm UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/9 "2021-08-25T17:18:11Z")

</div>

> [@Blacksmoke16](#):
>
> `a = gets.to_x` wouldn’t work because `gets` returns `String?`. You would have to do `gets.not_nil!.to_x`.

Yes, the code that worked for me was

```auto
a = gets.not_nil!.to_i

```

---

<div class="post-metadata">

**Author:** ![Blacksmoke16](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/blacksmoke16/32/1241_2.png) [@Blacksmoke16](https://forum.crystal-lang.org/u/Blacksmoke16)\
**Post date:** [August 25, 2021, 5:18pm UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/10 "2021-08-25T17:18:47Z")

</div>

> [@Penfore](#):
>
> So the behavior of gets is always assumying the input is a String

Yes, but could also be `nil` as @asterite pointed out.

> [@Penfore](#):
>
> Does the not\_nil! works in a similar way to what you said?

`not_nil!` is mainly used for cases where you can be 100% sure a value won’t be `nil` and the compiler cannot determine that. It’s usually suggested to not use it everywhere and instead use more robust `nil` handling logic. Such as one of the forms of `if` mentioned in [if - Crystal](https://crystal-lang.org/reference/syntax_and_semantics/if.html) or [Object - Crystal 1.11.0-dev](https://crystal-lang.org/api/master/Object.html#try(&)-instance-method).

> [@Penfore](#):
>
> What does the chomp do?

I’m pretty sure it just removes the trailing `\n` from the string read in from `STDIN`.

---

<div class="post-metadata">

**Author:** ![vlazar](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/vlazar/32/31_2.png) [@vlazar](https://forum.crystal-lang.org/u/vlazar)\
**Post date:** [August 26, 2021, 1:18pm UTC](https://forum.crystal-lang.org/t/help-adding-two-numbers/3635/11 "2021-08-26T13:18:35Z")

</div>

> [@Penfore](#):
>
> ```auto
> a = gets.not_nil!.to_i
> b = gets.not_nil!.to_i
> 
> x = a + b
> 
> puts "X = #{x}"
> 
> ```
> 
> Does the not\_nil! works in a similar way to what you said?

Normally you shouldn’t use `not_nil!` in your code. It’s an escape hatch for rare occasions.

Try running this program and hot `Ctrl + D`. You’ll see this runtime exception:

```auto
Unhandled exception: Nil assertion failed (NilAssertionError)
  from /usr/local/Cellar/crystal/1.1.1/src/nil.cr:108:5 in 'not_nil!'
  from h.cr:1:5 in '__crystal_main'
  from /usr/local/Cellar/crystal/1.1.1/src/crystal/main.cr:110:5 in 'main_user_code'
  from /usr/local/Cellar/crystal/1.1.1/src/crystal/main.cr:96:7 in 'main'
  from /usr/local/Cellar/crystal/1.1.1/src/crystal/main.cr:119:3 in 'main'

```

So by using `not_nil!` you’ve lost compile time guarantees Crystal gives you against `nil` values.

Try running this and also hit `Ctrl + D`:

```auto
a = gets
b = gets

if a && b
  x = a + b
else
  x = nil
end

p! x

```

So use if/else to explicitly handle cases when Crystal compiler tells you that there could be `nil` value and you have’t handled such case in your code:

```auto
Error: undefined method '+' for Nil (compile-time type is (String | Nil))

```

Like already mentioned here:

> [@Blacksmoke16](#):
>
> `not_nil!` is mainly used for cases where you can be 100% sure a value won’t be `nil` and the compiler cannot determine that. It’s usually suggested to not use it everywhere and instead use more robust `nil` handling logic. Such as one of the forms of `if` mentioned in [if - Crystal](https://crystal-lang.org/reference/syntax_and_semantics/if.html) or [Object - Crystal 1.2.0-dev](https://crystal-lang.org/api/master/Object.html#try(&)-instance-method).
