Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

Sherri McDonald
Sherri McDonald
535 Points

Why is mono so annoying?

I compiled the code for this final quiz in C# in Visual Studios and everything worked as the quiz asked of me. The second I put it in mono I get errors because the variable is null.... DUH the user needs to input the variable... I'm a completionist and it bothers me to not have a project done.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while (true)
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");
                var entry = Console.ReadLine();
                try
                {
                    var a = int.Parse(entry);
                    for (int b = 0; b <= a; b++)
                        if (b < 0)
                        {
                            Console.WriteLine("That is not a valid entry, try again.");
                        }
                        else if (b > 0)
                        {
                            Console.WriteLine("Yay!");
                        }
                }
                catch (FormatException)
                { 
                    Console.WriteLine("That is not valid input, try again.");
                    continue;
                }
            }
        }
    }
}

1 Answer

James Churchill
STAFF
James Churchill
Treehouse Teacher

Sherri,

The code challenge is expecting the program to terminate after it processes the user's input and outputs the expected messages. I'd suggest that you eliminate the while loop from your solution.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            var entry = Console.ReadLine();
            try
            {
                var a = int.Parse(entry);
                for (int b = 0; b <= a; b++)
                    if (b < 0)
                    {
                        Console.WriteLine("That is not a valid entry, try again.");
                    }
                    else if (b > 0)
                    {
                        Console.WriteLine("Yay!");
                    }
            }
            catch (FormatException)
            { 
                Console.WriteLine("That is not valid input, try again.");
            }
        }
    }
}

Doing that will allow you to get past the first task of the code challenge.

Thanks ~James