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

omer syed
omer syed
8,634 Points

Not sure why my code is not running.

Hi, I'm on challenge Task 3 of 3 for the final project in C# Basics and I'm not sure why my code isn't running. I'm not getting any errors so i was just curious if i could get another set of eyes to look over it.

Thanks!

Program.cs
using System;

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

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

                for(var i = 0; i <= howManyTimes-1; i++)
                {
                    Console.WriteLine("Yay!");
                }
            }
            catch(FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,096 Points

You have an extra loop, and it never ends.

When you check the value to see if it is negative, you only need a conditional ("if") expression. But you have a loop instead. Since nothing the loop is testing is changed inside, the loop will never end.

Just convert that loop into a simple test and you should pass.

omer syed
omer syed
8,634 Points

Hey Steven thanks for replying! So i went ahead and changed the While loop to an If Statement and im still not sure why it wont run. I ran the code in Visual Studio And it works perfectly.

Steven Parker
Steven Parker
231,096 Points

I just tried it myself and it passed the challenge. Perhaps you should show the code as it is now. Be sure to format it correctly using Markdown! (the "get help" button did that for you the first time).