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

Luis Santos
Luis Santos
3,566 Points

Stuck again, I tried the try-catch method but somehow I managed to confuse myself.

I must be missing something in the code, but for the life of me I can't figure out what it is. Any kind soul on Treehouse willing to give me a hand with this? It will be greatly appreciated, Thank you.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            System.Console.WriteLine("Enter the number of times to print \"Yay!\": ");

          var numOfTimes = System.Console.ReadLine();
try
{
          int num = int.Parse(numOfTimes);

          for (int i=0; i<num; i++) {

          System.Console.WriteLine("\"Yay!\"");

          }

          catch (FormatException){

          System.Console.WriteLine("You must enter a whole number.");

          }
        }
    }
}
}

1 Answer

Florian Tรถnjes
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Florian Tรถnjes
Full Stack JavaScript Techdegree Graduate 50,856 Points

Hey Luis,

your code is alright but one bracket was wrong. Also always make sure to indent your code correctly, this will make it easier to see such errors.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
          System.Console.WriteLine("Enter the number of times to print \"Yay!\": ");

          var numOfTimes = System.Console.ReadLine();
          try
          {
            int num = int.Parse(numOfTimes);

            for (int i=0; i<num; i++) {
              System.Console.WriteLine("\"Yay!\"");
            } // Bracket was missing right here!

          }
          catch (FormatException)
          {

            System.Console.WriteLine("You must enter a whole number.");

          }
        }
    }
    // One bracket too much here!
}