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

what is wrong with this?!

what is wrong with this?!

Program.cs
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;
                if (count==0)
                {
                    Console.WriteLine("You must enter a whole number.");
                }
                else
                {
                i += 1;
                while(i < count)
                {
                Console.WriteLine("Yay!");
                }
                }
            }
            catch (FormatException)
            {
            Console.WriteLine("You must enter a whole number.");
            }   
        }
    }
}

4 Answers

James Churchill
STAFF
James Churchill
Treehouse Teacher

The line of code that increments the variable i, i += 1;, needs to be placed inside of the while loop, otherwise the variable i will never be incremented which results in an infinite loop (i.e. it never ends).

This is what your while loop should look like:

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

That'll resolve the error that you're currently receiving. Then you'll need to sort out the conditional expression that you're using to check if the user has entered a negative number.

Good luck!

~James

Steven Parker
Steven Parker
231,248 Points

Perhaps I should've taken a second glance. :smirk:

Steven Parker
Steven Parker
231,248 Points

At first glance, I see three things:

  • zero is a valid entry and should be allowed
  • negative numbers should not be allowed
  • the message given for a negative number should be different from the one given for parse errors

Thank you!

Thank you!