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

C# Basics, Final Challenge Task 3 throws Exception

I cannot get passed this last task. Hopefully someone can see why the code is rejected and tell me how i can make this work.

Program.cs
using System;

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

                while ( numberOfTimes < 0 )
                {
                    Console.WriteLine("You must enter a positive number.");   
                    Console.Write("Enter the number of times to print \"Yay!\": ");
                    numberOfTimes = int.Parse(Console.ReadLine());
                }

                for (int i = 0; i < numberOfTimes; i++)
                {
                    Console.WriteLine("Yay!");
                }
            }
            catch (FormatException) 
            {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,248 Points

A program that follows the instructions will ask for input one time and then either print "Yay!"s or an error message. But either way it will end.

There should not be a loop to get input more than once.

Thanks for your answer.

I fixed the problem by declaring a string variable to which i assigned the result of Console.ReadLine(). As seen in: https://teamtreehouse.com/community/my-c-basics-final-quiz-13-code-works-in-visual-studio-but-wont-pass-the-quiz-whats-wrong

Regarding your comment that there shouldn't be a loop to get input more than once, you're absolutely right. I removed it.

Steven Parker
Steven Parker
231,248 Points

Converting the input to a value on the same line shouldn't cause any problems. It's not really necessary to have a separate string value.