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

Kimmo Ojala
seal-mask
.a{fill-rule:evenodd;}techdegree
Kimmo Ojala
Python Web Development Techdegree Student 8,257 Points

I don't understand why my loop doesn't work anymore in task 2.

My loop stops working when I place the conversion of the entry into integer within Try {} .

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int counter = 0; 
            string entry = Console.ReadLine();

            while (true)
            {
            try
            {          
            int number = int.Parse(entry);
            counter = counter + 1;

            if(counter > number)
            {
                break;
            }
            Console.WriteLine("Yay!");
            } 
            catch(FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
                continue;
            }
            }    
        }
    }
}

2 Answers

Shadab Khan
Shadab Khan
5,470 Points

Hi Kimmo,

Please tr below :

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int counter = 0;
            string entry = Console.ReadLine();

            while (true)
            {
                try
                {
                    int number = int.Parse(entry);
                    counter = counter + 1;

                    if (counter > number)
                    {
                        break;
                    }
                    Console.WriteLine("Yay!");
                }
                catch (FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }
            }

            Console.ReadLine();
        }
    }
}

You need to enter the 'Console.ReadLine()' line at the end of your code. This freezes your console screen to view the results until you hit the return key.

Hope that helps. All the best!

Thimmy Stenlund
Thimmy Stenlund
7,523 Points

Hi Kimmo,

I tried out the code you supplied and I couldnยดt get an error (it displayed "Yay!" as many times as I entered an integer/number) and your conversion from string to int happens within the try-block. If you have an issue with above code, I would suggest clearing the cache of your browser and try writing the exercise once more. I've had a few tasks act up as it seems to conflict with cached data now and then. Let me know if this fixed your error.