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

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
229,606 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!