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

Matthew Farrell
Matthew Farrell
7,215 Points

How do I end this infinite loop?

Hey all. The compiler says the code takes too long to run. I think I am stuck in an infinite loop cycle. Any advise on how to fix this script would be greatly appreciated.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            int numTotal = 0;

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

                try
                {
                    int num = int.Parse(entry);

                    if(numTotal <= num)
                    {
                        Console.WriteLine("\"Yay!\"");
                        numTotal++;

                    }
                    else
                    {
                        break;
                    }


                }
                catch(ArgumentNullException)
                {
                    continue;
                }


            }


        }
    }
}

1 Answer

Antonio De Rose
Antonio De Rose
20,885 Points

Yes it is kind of a infinite loop, while loop comes, and the prompt comes to ask how many times, which in turn is changing the num variable, every time the loop goes, I would suggest you to the other thing would be the initialization, if the initialization is is zero, the condition cannot '<=' if at all for the print to have exact of what is given in the input, your case it will print one additional check for the exception, question had been asked to put 'FormatException', no need of continue

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            int numTotal = 0;


                Console.Write("Enter the number of times to print \"Yay!\": ");
                string entry = Console.ReadLine();

                try
                {
                    int num = int.Parse(entry);
                    while(true){    
                    if(numTotal < num)
                    {
                        Console.WriteLine("\"Yay!\"");
                        numTotal++;

                    }
                    else
                    {
                        break;
                    }


                }
                }
                catch(FormatException)
                {
                    Console.Write("You must enter a whole number.");
                }

        }
    }
}