Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

Problem with the Final of C# Basics(the second challenge of three). I tested my code on Visual Studio and it seemed OK.

When I ran my code in Visual Studio it seemed to give me the correct results for the challenge yet it doesn't pass on the Final. Any help would be appreciated. Thanks.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while (true)
            {
                int result = 0;
                Console.Write("Enter the number of times to print \"Yay!\": ");
                try
                {
                    result = int.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                }
                int counter = 1;
                while (counter < result + 1)
                {
                    Console.WriteLine("yay");
                    counter++;
                    if (counter == result + 1)
                    {
                        Console.ReadLine();
                        Environment.Exit(0);
                    }
                }
            }

        }
    }
}

1 Answer

Steven Parker
Steven Parker
215,972 Points

:warning: Be careful about testing a challenge in an external REPL or IDE.

If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.

In this case, you have added an extra loop that prevents the program from ending if bad values are entered..

A program that follows the challenge instructions will only ask for input one time, and either print "Yay!"s or an error message. But either way it should end.