0
class Averager
{
    static void Main()
    {

        var total = 0.0;

        while(true)
        {                                                    

            Console.Write("Enter a number or type \"done\" to see the average: ");

            var entry = Console.ReadLine();

            var addition = double.Parse(entry);

            double counter = 0;

            counter += 1;

            total += addition;


             if(entry.ToLower() == "done")
            {
              Console.WriteLine("Average" + total / counter);
              break;
            }           


        }
    }   
} 

When I prompt "done" I have a Unhandled Exception: System.FormatException: Input string was not in a correct format.

My target is after "done" print sum of all numbers divided by the total by the number of numbers entered. Please help.

  • Of course the string "done" cannot be given as input to double.Parse. You need an if here, but probably a double.TryParse will be better to protect your code from a user that types "ABC" instead of a number – Steve Nov 13 '16 at 22:05
  • before you parse incoming string to double check it. Also use TryParse instead of Parse as what if someone enter abc instead of number ? – Hakunamatata Nov 13 '16 at 22:16

0 Answers0