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

Italo Santos
Italo Santos
1,521 Points

I'm using the same code in the Visual Studio and also in the Treehouse workspace and I don't have the error.

The error System.ArgumentNullException appears when I try to check my work. I don't have the same error in treehouse workspace and the program is doing exactly what the excercise is asking. Can someone review my code please?

Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Yay();
        }
        static void Yay()
        {
            bool error = true;
            int check = 0;

            while (error)
            {
                bool keepGoing = true;

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

                string entry = Console.ReadLine();

                while (keepGoing)
                {
                    try
                    {
                        int times = int.Parse(entry);

                        if (check < times)
                        {
                            Console.WriteLine("Yay!");
                            check += 1;
                        }
                        else
                        {
                            keepGoing = false;
                        }
                        error = false;
                    }
                    catch (FormatException)
                    {
                        Console.WriteLine("You must enter a whole number");
                        keepGoing = false;
                        continue;
                    }
                }
            }
        }
    }
}

2 Answers

This is what I did in the challenge and it passed:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            try{
              Console.Write("Enter the number of times to print \"Yay!\": ");
              var yayCount = Console.ReadLine();
              var count = 0;
              if(Int32.Parse(yayCount) < 0){
                Console.Write("You must enter a positive number.");
              }
              else{

                while(count < Int32.Parse(yayCount)){
                  Console.Write("Yay!");
                  count++;
                }

              }

            }
            catch(FormatException formatException){
              Console.Write("You must enter a whole number.");
            }

        }
    }
}
Italo Santos
Italo Santos
1,521 Points

I did this two FormatException inside the catch and it accepted. Thanks Jeremy

You're welcome :)

You're welcome :)