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

James Welch
James Welch
10,363 Points

C# Final task just says "Bummer! Try again!" With no error to see where I'm going wrong, please help.

Changed the code, almost there now. Getting this error message: System.ArgumentNullException: Value cannot be null.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            int c = 0;

            while (true) {
              Console.Write("Enter the number of times to print \"Yay!\": ");
              string times = Console.ReadLine();
              try {
                c = int.Parse(times);
                break;
              } catch (FormatException) {
                Console.WriteLine("We only accept whole numbers");
                continue;
              }
            }

            for (int i = 0; i < c; i++) {
              Console.WriteLine("Yay!");
            }

        }
    }
}

EDIT: Fixed!

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {

            int c = 0;

            Console.Write("Enter the number of times to print \"Yay!\": ");
            string times = Console.ReadLine();

            try {
              c = int.Parse(times);
            } catch (FormatException) {
              Console.WriteLine("You must enter a whole number.");
              return;
            }

            for (int i = 0; i < c; i++) {
              Console.WriteLine("Yay!");
            }

        }
    }
}

Thanks.

1 Answer

Steven Parker
Steven Parker
231,269 Points

Good job. :+1:

Did you figure it out, or find one of my previous posts about this challenge?

James Welch
James Welch
10,363 Points

I figured it out, similar to your description, thought the user needed to keep entering values if the input was invalid.

Thanks! Happy coding :-)