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 Final

Why can't I pass this object?

I found no bug in my code and the Checking doesn't let me through. Please help me.

Program.cs
using System;


namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            int count;
            while (true)
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                try
                {
                    count = int.Parse(Console.ReadLine());
                    break;
                }
                catch (FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                }
                catch (ArgumentNullException)
                 {
                    Console.WriteLine("You must enter a whole number.");
                }
            }

            for (int i = 0; i < count; i++)
            {
                Console.Write("Yay!");
            }

        }
    }
}

1 Answer

Steven Parker
Steven Parker
229,744 Points

You have an outer loop that prevents the program from ending. A program built according to the instructions will prompt and accept input one time, and based on the input it will either print "Yay!"s or an error mesage. Either way, it should end.

Also, a negative input won't cause an exception when parsed. You'll need another method to detect that.

And the error message for a negative input needs to be different.