Money Maker - Having Problem with Converting Strings

Hey there! I am new here (to the forum and to C#) and I am having some run errors with Money Maker.

I am new to C# and this might be an idiot question, but here goes:

I am having errors running C# Money Maker.

Errors:

Unhandled Exception: System.FormatException: Input string was not in a correct format.
   at System.Number.ParseDouble(ReadOnlySpan`1 value, NumberStyles options, NumberFormatInfo numfmt)
   at System.Convert.ToDouble(String value)
   at MoneyMaker.MainClass.Main(String[] args) in /home/ccuser/workspace/csharp-money-maker/Program.cs:line 18

and the code:

using System;

namespace MoneyMaker
{
  class MainClass
  {
    public static void Main(string[] args)
    {
      Console.WriteLine("---WELCOME TO MONEY MAKER---");
      Console.WriteLine("---WRITTEN BY EAMON WALL---");
      Console.WriteLine("---GAME VERSION v0.1---");
      Console.WriteLine("---PRESS ANY KEY TO START---");
      int key = Console.Read();
      Console.WriteLine("---PROGRAM NOW STARTING---");
      Console.WriteLine("What is the amount to convert into coins?");
      string toConvertAsString = "";
      Console.Read();
      double toConvertAsDouble = Convert.ToDouble(toConvertAsString);
      Console.WriteLine($"---NOW CALCULATING WHAT {toConvertAsString} IS IN THE LEAST AMOUNT OF COINS---");
    }
  }
}

and line 18 being

      double toConvertAsDouble = Convert.ToDouble(toConvertAsString);

thank you.

Hello @ajax2872111171, welcome to the forums!

What does this do? Should this be on its own line, or should you set a variable equal to this? Also consider using Console.ReadLine() instead, as it will read the whole line, not just the first character. Here is a good article on the difference.

I hope this helps!

1 Like