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

Ali Gorur
Ali Gorur
1,073 Points

Hi, Could someone help with my code please?

Can't seem to figure out what to do...

Program.cs
using System;

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

            while(counter < input)
            {
                Console.Write("Yay!");
                counter ++;

        }
    }
}
}

1 Answer

Steven Parker
Steven Parker
216,012 Points

It looks like you have two issues:

  • you declare input inside the try block, but then attempt to reference it outside it (in your loop)
  • you forgot to put a semicolon after the Write statement inside the catch block

So for that first one, be sure to declare input in the scope common to all the places you use it (in this case, the Main block).

Ali Gorur
Ali Gorur
1,073 Points

Hi Steven,

Thanks its sorted now!