help regarding doing IO

I am trying to build a simple guessing game using elixir to learn about the fundamentals. I am trying to get the input taken from the console , using IO.gets() and write back to the console using IO.puts(), but when I run that using mix ,it compiles and exits and does not wait for input.

defmodule GuessingGame do
  use Application

  def start(_type, _args) do
    GuessingGame.main()
    Supervisor.start_link([], strategy: :one_for_one)
  end

  def input do
    IO.gets("> ") |> String.trim()
  end

  def main do
    # randomly 8
    correct = 8
    inp = input()
    IO.puts("You have entered #{inp}")

    if String.to_integer(inp) === correct do
      IO.puts("You win !")
    else
      IO.puts("You lose!")
    end
  end
end

Have you defined your application in mix.exs? Also, that isn’t the best approach to writing CLI apps, as Application is meant more for long-running applications.

1 Like

thanks @hauleth ; yeah i forgot to define the module in mix.exs .