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

louie solis
louie solis
1,126 Points

[Solved] Task 1 is no longer passing C#

I'm not sure what is going on. I have build this code from Visual Studio 2017 and its working just fine even without the Console.ReadKey its not quitting out. Negative numbers input will give me a message that I am expecting, to use a positive number. Typing string of letters will ask me to input whole number. And finally typing whole positive number will print the number of Yay! I instructed the program to print. Please help. Thanks

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while (true) 
            {
                string yay = "Yay!";
                int number = 0;
                Console.WriteLine("Enter the number of times to print \"Yay!\": ");
                string userPrint = Console.ReadLine();
                try
                {
                    number = int.Parse(userPrint);
                    if (number <= -1)
                    {
                        Console.WriteLine("You must enter a positive number.");
                    }
                    while (number > 0)
                    {
                        Console.WriteLine(yay);
                        number--;
                    }
                }

                catch (FormatException)
                {

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


            }
        }
    }
}
louie solis
louie solis
1,126 Points

never mind i have this working i just remove the while (true)...

4 Answers

Steven Parker
Steven Parker
231,236 Points

I can explain why the original program did not pass.

The challenge expects that when given good input, the program will print a number of outputs and then end. When given bad input, the program will print an error message and then end. But either way, it should end.

That loop caused the program to repeat and prevented it from ending.

Steven Parker
Steven Parker
231,236 Points

Congratulations on resolving your own issue. :+1:

If it makes you feel any better, that's a very common mistake for some reason.

louie solis
louie solis
1,126 Points

I just started learning C# about a week ago, actually I just started learning programming. I didn't even understand why removing the while (true) fixed the problem.. LUL

louie solis
louie solis
1,126 Points

Thank you Steven. Now I understand much better.