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

Stepas Loiba
Stepas Loiba
11,180 Points

hey,help handling try,catch loop break,or fixing decimal entry.

can't break loop in my code when putting decimals its repeat everything,but works with entring string.please give some tips where to look in.thank you in advance.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            var index = -1;

            Console.Write("Enter the number of times to print \"Yay!\": ");
            string entry = Console.ReadLine();
            try
            {
                var times = double.Parse(entry);
                while (true)
            {

                var value = ++index;

                if (value == times)
                {
                    break;
                }



                Console.WriteLine("Yay!");
            }
            }
            catch (FormatException)
            {
                Console.WriteLine("not good expresion!try again!");

            }

        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,269 Points

:point_right: It looks like you have two issues:

The challenge says you should check for a FormatExeception "if the user enters a decimal or something that isn’t a number". But double.Parse will handle decimals and not produce an exception. You may want to use int.Parse instead to handle only integers.

Also, the challenge says that an exception should print the message "You must enter a whole number.", but your code would print "not good expresion!try again!" instead.

Stepas Loiba
Stepas Loiba
11,180 Points

Thank you Steve again for your feed back.Once again very useful and constructive feed back.Have nice day.

Steven Parker
Steven Parker
231,269 Points

I'm happy to help. Happy coding! :sparkes: