My result isn't showing and giving me the error from Main function

Hey there,
I got an issue in my code & from a long time trying to fix but still can’t.
I want to store the value in input & then call the result function. After getting the right result I want to send that result to my main() function. In the Main() the input variable is processing in Process() function & my code is giving this error:

warning: variable "input" is unused (if the variable is not meant to be used, prefix it with an underscore)
  a2.ex:10: A2.process/1

When I switch my input variable from process(input) then It shows neither output nor error.

Here’s my code:

defmodule A2 do

  def process(input) do
    input = IO.puts("Sun Mercury") |> String.trim("\n")
    result = result(input)
    IO.puts("#{result}")
  end

  defp result("Sun Mercury"), do: "From Sun to Mercury is 57909227km"
  defp result("Earth Moon"), do: "From Earth to Moon is 384400km"
  defp result("Sun Earth"), do: "From Sun to Earth is 149598262km"
  
  def main() do
    IO.read(:stdio, :all)
    |> process()
    |> IO.write()
  end
end

In

def process(input) do

you’re receiving an input, but then in the very next line, you throw that input away and replace it with something else:

input = IO.puts("Sun Mercury") |> String.trim("\n")

That’s what’s giving the error.

However, I don’t think that

input = IO.puts("Sun Mercury") |> String.trim("\n")

is what you want to do, because IO.puts always returns :ok, which doesn’t make sense to pipe into String.trim

2 Likes

The last statement in a function is what is returned, therefore you want to return result and not having IO.outs:

  def process(input) do
    result(input)
    # Comment out the below line for debugging the returned result
    # |> IO.inspect(label: "Result")
  end

Also you may want to put the main function at the top level of your module, because thats how it’s done in the idiomatic way in Elixir.

Hey @APB9785 @Exadra37
I followed your suggestion. But still can’t show the result as according to input it should show output but my code is now not giving any output. By this code neither I’m receiving any error nor any output :frowning:

Here’s the updated code:

def process(input) do
    result = result(input)
    IO.puts("#{result}")
  end

  defp result("Sun Mercury"), do: "From Sun to Mercury is 57909227km"
  defp result("Earth Moon"), do: "From Earth to Moon is 384400km"
  defp result("Sun Earth"), do: "From Sun to Earth is 149598262km"

  def main() do
    IO.read(:stdio, :all)
    |> process()
    |> IO.write()
  end

I want to give input like this input = IO.puts("Sun Mercury") line in my process function & run in main(). And the process function should find this io.puts value in result function & give the exact one from there. Like this input value should be From Sun to Mercury is 57909227km.
I don’t want to change my main, get input in process function & then show in output from main() function.

In hexdocs, I don’t see that :all is valid for the 2nd parameter of IO.read. I think you want it to look like this (with a bit of debugging output). You need the String.trim to get rid of the trailing newline character.

def main() do
  IO.read(:line)
  |> IO.inspect(label: "raw input")
  |> String.trim()
  |> process()
end

I also recommend changing the process function because IO.puts always returns :ok

def process(input) do
  input
  |> result()
  |> IO.inspect(label: "result within process")
end
2 Likes

Hey @gregvaughn,
Yeah I applied those changes in my code as you suggested. And still My output is blank.
My updated code:

  def process(input) do
    input
    |> result()
    |> IO.inspect(label: "result within process")
  end
  defp result("Sun Mercury"), do: "From Sun to Mercury is 57909227km"
  defp result("Earth Moon"), do: "From Earth to Moon is 384400km"
  defp result("Sun Earth"), do: "From Sun to Earth is 149598262km"

  def main() do
    IO.read(:line)
    |> IO.inspect(label: "Sun Mercury")
    |> String.trim()
    |> process()
  end

Sorry for the dumb question; are you typing your input in the console after you start the program?

1 Like

Hey @dimitarvp
Yeah I was trying to write the input in the process function and wanted to call the result function on the basis of the input value & then the process function should be called out in the Main() function.
Now my code is working by debugging & adding new lines.
Here’s the final code:

defmodule A2 do

  def process() do
    input = "Sun Mercury"
    result = result(input)
    IO.puts("#{result}")
  end

  defp result("Sun Mercury"), do: "From Sun to Mercury is 57909227km"
  defp result("Earth Moon"), do: "From Earth to Moon is 384400km"
  defp result("Sun Earth"), do: "From Sun to Earth is 149598262km"

  def main() do
    process()
    |> IO.write()
  end
end
A2.main()

Here’s my Output in console:

From Sun to Mercury is 57909227km
ok

Also Thanks @APB9785 , @gregvaughn , @Exadra37 for your guidelines too.

Glad you made it work in a way that is good enough for you.

It seemed that initially you wanted to be able to input the parameter in the console, with the keyboard, and then pass it to the result function. And in your last comment you simply pass a hardcoded value.

Not sure if that makes a difference for you but I felt I had to point it out.

3 Likes