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

Code not working for the quiz

Any ideas why?, I know that the ArguementNullExcpetion isn't required but I just wanted to eliminate all possibilities

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            bool state = true;

            while (state)
            {
                Console.Write("Enter the number of times to print \"Yay!\": ");

                // Get user input
                string userInput = Console.ReadLine();
                int currentNumber = 0;

                try
                {
                    var counter = int.Parse(userInput);

                    if(counter <= 0)
                    {
                        Console.WriteLine("You must enter a whole number.");
                        continue;
                    }


                    while (currentNumber < counter)
                    {
                        Console.WriteLine("Yay!");
                        currentNumber += 1;

                        if (currentNumber == counter)
                        {
                            state = false;
                            break;
                        }
                    }

                }
                catch (FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }
                catch (ArgumentNullException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
229,695 Points

You have an outer loop that prevents the program from ending when invalid input is entered. A program built according to the instructions will prompt and accept input one time, and based on the input it will either print "Yay!"s or an error mesage. Either way, it should end.