skip to Main Content

I’ve been following a C# tutorial video, now building a basic calculator and I’ve run into a problem(sure I’ve installed the right extensions to run C#…)

here’s some code :

  static void Main(string[]Args)
  {

      Console.WriteLine("--------------basic calculator---------------");

      Console.Write("Enter a number : ");
      double num1 = Convert.ToDouble(Console.ReadLine());//stores number
      Console.Write("Enter another number : ");
      double num2 = Convert.ToDouble(Console.ReadLine());//stores second number

      Console.WriteLine(num1 + num2);//adds 'double' variables

    //works the same as the previous program but it can calculate numbers with decimals now
  }

so according to the tutorial, the program is now supposed be able add decimal numbers and print out a decimal answer too, but the vscode says – The input string ” was not in a correct format. it tried calculating whole numbers though and it works it but won’t add any decimal numbers

2

Answers


  1. The error you’re encountering (The input string ” was not in a correct format) is usually caused by Convert.ToDouble() when it encounters a string that it cannot parse into a double.

    This could happen if the input string is empty or contains characters that are not valid for a double, so assuming you are following the tutorial correctly check if you have numlock active or maybe the library can’t handle your keyboards language.

    Lastly, for future testing, you can totally add something like

    Console.WriteLine("You entered number : "); 
    Console.WriteLine(num1);
    
    Login or Signup to reply.
  2. There is no issue in this code it will work properly. I think you are not providing decimal values as input.
    Please do not use single or double quotes when you are providing input values

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search