-2

I found a problem - when I run this program, Visual Studio throws an exception - Format exception: Input string was not in a correct format.

        string name = Console.ReadLine();
        int age = int.Parse(Console.ReadLine());
        int id = int.Parse(Console.ReadLine());
        double salary = double.Parse(Console.ReadLine());
        Console.WriteLine($"Name: {name}");
        Console.WriteLine($"Age: {age}");
        Console.WriteLine($"Employee ID: {id:D8}");
        Console.WriteLine($"Salary: {salary:F2}");

Unfortunately, I can't find a mistake here. Can you help me, please? Or this is a bug in Visual Studio?

16
  • You are trying to convert what is in the Colsole.ReadLine to an int, but do you know if it is an int before you try to convert it? It could be somethign else Commented Apr 13, 2018 at 14:50
  • Exception has been thrown by one of .Parse(Console.ReadLine()): to reproduce just put bla-bla-bla when int or double required Commented Apr 13, 2018 at 14:51
  • Can you share your input? Commented Apr 13, 2018 at 14:54
  • 1
    You got an exception but you made no effort to find out what it means. You don't even know which line threw the exception. You pass strings to int.Parse() and double.Parse(), but you don't appear to have any idea what strings you're passing. You can't find a mistake because you didn't try to look for one. The exception message is not very informative, but Google will tell you exactly what it means. Didn't you bother? Commented Apr 13, 2018 at 14:57
  • 1
    Please read minimal reproducible example guide on posting code - in this case you should provide exact input as constant string and not ReadLine. Also find more suitable search engine than disney.com. Google, Bing would instantly give you explanation of the exception, searching on MSDN or just clicking F1 in Visual Studio would have helped with explanation of Console.WriteLine or Int32.Parse. So far this post does not bring any new information for SO in addition to thousands existing variant of this excepotn. Commented Apr 13, 2018 at 15:04

1 Answer 1

0

There is something wrong with whatever your inputting. So you may be trying to input a double into an int which would throw this error. Try using int.TryParse().

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.