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

Jonathan Wade Jr
Jonathan Wade Jr
1,051 Points

Final Quiz, Input Validation

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

namespace Treehouse.CodeChallenges { class Program { static void Main() { int counter = 0; int result; Console.Write("Enter the number of times to print \"Yay!\": ");

        try
        {
            result = Convert.ToInt32(Console.ReadLine());
        }


        catch(FormatException)
        {
            Console.WriteLine(" “You must enter a whole number.");
        }
                if (result > counter)
                {
                    counter++;
                    Console.WriteLine("Yay!");
                }


        Console.ReadLine();


        }         

}

}

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            int counter = 0;
            int result;
            Console.Write("Enter the number of times to print \"Yay!\": ");
            result = Convert.ToInt32(Console.ReadLine());

            while (result > counter)
            {
                counter++;
                Console.WriteLine("Yay!");
            }
            Console.ReadLine();
        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,108 Points

:point_right: Looks like you have a stray Console.ReadLine(); on a line by itself near the end, and after you do your printing. You'll need to remove that. That's for both of the two examples you show. That first one also needs a return inside the catch block so it doesn't try to print when there is a bad value entered.

And in case you need them, here are some hints for the second task of the challenge.

Jonathan Wade Jr
Jonathan Wade Jr
1,051 Points

thanks, sorry for the advice, sorry for the delay