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

Abdullah Mohamed
Abdullah Mohamed
17,323 Points

Can't pass this task....

Task 3/3

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        { 
          var i=0;
          Console.Write("Enter the number of times to print \"Yay!\": ");                    
          try
         {
            var entry=Int32.Parse(Console.ReadLine());
            while(i<entry)
            {

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

              Console.WriteLine("Yay!");
               i++;
            }
          }
          catch (FormatException)
         {
          Console.WriteLine("You must enter a whole number.");
          }  
        }
    }
}

1 Answer

Hi,

You have got the right code, but you just need to reorganise it a bit. The main issue is that you are using

while(i<entry)

before you test for a negative value of entry. You earlier set i to 0, so if entry is -1, then you will never enter your while loop .... so you will never run the enclosed test for a positive number.

So I would move the test for a positive number to immediately after you have read the user input. Then you can use an 'else' to run the rest of the code.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        { 
          var i=0;
          Console.Write("Enter the number of times to print \"Yay!\": ");                    
          try
         {
            var entry=Int32.Parse(Console.ReadLine());
            if (entry<0) {
                Console.WriteLine("You must enter a positive number.");
              } else {
              while(i<entry)
            {

              Console.WriteLine("Yay!");
               i++;
            }
              }

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