C# C# Basics (Retired) Perfect Wrap Up

Trouble dealing with error Format.Exception when string input by user

This is before my changes to code will work with integers then I input a string and error code

{ string number; double total = 0; int numbers = 0; while ((number = Console.ReadLine()) != "") { total += double.Parse(number); numbers++; }

        Console.WriteLine("The average of the numbers entered is {0}", total / numbers);
        Console.ReadLine();
      }

This is after my attempt to address the issue.

namespace Averager { class Program { static void Main(string[] args) { string number; double total = 0; int numbers = 0; bool keepGoing = true; string notNumber = "";

        while (keepGoing)
        {
            if (notNumber == "")
            {
                keepGoing = false;
            }
            total += double.Parse(number);
            numbers++;

        }

        Console.WriteLine("The average of the numbers entered is {0}", total / numbers);
        Console.ReadLine();
      }  
 }  

}

1 Answer

Steven Parker
Steven Parker
170,377 Points

I'm not entirely sure what you are attempting, but I notice a few things in your "after" code:

  • you need a "using System;"
  • you no longer have a call to Console.Readline to get input
  • you attempt to parse number but it has never been assigned
  • you mentioned dealing with exceptions but you have no try or catch