Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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
231,236 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