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

Eugene Lee
Eugene Lee
11,890 Points

on C# for final coding on first segment that uses try and catch, i think i got right answer, but autograder is unhappy

on c#, i got to fix coding with try and catch as well as fixing while loop. i did everything i could to get to fix. but i am not getting right answer. i am trying to find out the issue

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {   try {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string input = Console.ReadLine();

            int count = int.Parse(input);

            int i = 0;
            while(i < count)
            {
                i++;
                count = i + 1;
                Console.WriteLine("Yay!");


            } } catch(FormatException) { Console.WriteLine("You must enter a whole number.");  }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,084 Points

The problem is due to a line you added to the inner loop of the provided code:

                while(i < count)
                {
                    i++;
                    count = i + 1;     // why?

By setting "count" to one more than "i", then "i" will always be less than "count", which means the loop condition will always be true and the loop will run forever.

Without that line, the exception handling additions work as required and you'll pass task 1.