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

I keep getting an error that says this "No enclosing loop out of which to break or continue"

I keep getting this error on this code challenge and have no idea how to fix it or what to do.

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);

                if(count <= 0)
                {
                    Console.WriteLine("You must enter a positive number.");
                }
                else
                {
                int i = 0;
                while(i < count)
                {
                    i += 1;   
                    Console.WriteLine("Yay!");


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

        }
    }
}
Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

Hi Kendall,

If that is a copy and paste of your code ... I can say that you're missing a couple of closing braces '}' and some indentation (for readability).

Let's hope that's all it is :) Best of luck,

Dave.

1 Answer

Dave Harker
PLUS
Dave Harker
Courses Plus Student 15,510 Points

So new to the site, I've been posting 'answers' as 'comments' ... second time's a charm!

I just realised I can click the 'view challenge' :) With the code they have there as a template, this is how I'd handle the challenge (although not how I'd do it in an actual app)

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;
                bool isNum = Int32.TryParse(input, out count);

                if(isNum) {
                    int i = 0;
                    while(i < count)
                    {
                        i += 1;   
                        Console.WriteLine("Yay!");
                    }
                } else throw new FormatException();

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

Best of luck,

Dave.

Thanks a lot for the help Dave!

Dave Harker
Dave Harker
Courses Plus Student 15,510 Points

Kendall Armalin — Glad to help. You can mark the question solved by choosing a "Best Answer".
And happy coding! :dizzy: