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

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