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

pet
pet
10,908 Points

I have looked at several "Final" Questions but am still stuck on challenge task 2 of "Final".

My 1st code passes challenge task 1, but even looking at the community I can't figure out how to make my second code pass task 2. Both of my codes are below. The problematic one is second.

using System;

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

            try
            {
                int count = int.Parse(input);

                int i = 0;
                while(i < count)
                {
                    i += 1;   
                    Console.WriteLine("Yay!");
                }
            }

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

            }
        }
    }
}

Task Two:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string input = Console.ReadLine();
            int count = int.Parse(input);
            int test = int.Parse(input);
            while(true)
            {
                if(test < 0)
                        {
                                Console.WriteLine("You must enter a positive number.");
                                break;

                        } 
                else
                {


                    try
                    {



                        int i = 0;
                        while(i < count)
                        {
                            i += 1;   
                            Console.WriteLine("Yay!");
                        }
                    }

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

Thank you anyone who helps.

1 Answer

Patricia Hector
Patricia Hector
42,901 Points

First of all, you are creating an infinite loop with the while(true) statement. Be careful when using loops without a condition that will never be false, because the only way that the loop will end is if you put the break keyword somewhere inside the loop; otherwise, this will run forever. Secondly, you don't need the loop for this challenge. The other thing Task one is no longer passing. This is happening because your try{} block has to contain all your code, and you moved it inside the else in the loop. This code passed for me.

using System;

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

            int count = int.Parse(input);
            if(count > 0){
                int i = 0;
            while(i < count)
            {
                i += 1;   
                Console.WriteLine("Yay!");
            }
            }
                else{
                    Console.WriteLine("You must enter a positive number.");
                }

            }
            catch(FormatException){
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}
pet
pet
10,908 Points

Thank you for your response. I followed your code and was able to see, line by line, my mistakes.