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

Albert González
Albert González
22,953 Points

No enclosing loop ¿? What is wrong?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
         {

            var counter = 0;

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

            try
            {
                var times = int.Parse(input);

                while (counter < times)
                {
                    Console.Write("Yay!");
                    counter += 1;
                }

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

            }
            catch (ArgumentNullException)
            {
                Console.WriteLine("You must enter a positive number");
                continue;
            }    

            Console.ReadLine();
        }   
    }
}

2 Answers

Kristian Gausel
Kristian Gausel
14,661 Points

You are close but you need to change your check for negative numbers to something like this:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
         {

            var counter = 0;

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

            try
            {
                var times = int.Parse(input);

                if(times < 0){
                    Console.WriteLine("You must enter a positive number");
                } else {

                    while (counter < times)
                    {   
                        Console.Write("Yay!");
                        counter += 1;
                    }  
                }

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

            }

            Console.ReadLine();
        }   
    }
}
Albert González
Albert González
22,953 Points

Oh, I don't saw it! Thank you very much for your help!

Kristian Gausel
Kristian Gausel
14,661 Points

If this answer is satisfactory, please mark it as the best answer so we can close the thread =)

Allan Blain
Allan Blain
2,418 Points

I had this problem after adding the Console.ReadLine(); it still failed but afer copying and pasting the whole thing exactly it passed