Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Final

Daniel Ram
PLUS
Daniel Ram
Courses Plus Student 14,792 Points

C# Basics Course: Final - This doesn't seem to work for Step 2 in the Validation, but it works @ www.repl.it

This seems to work at www.repl.it but it fails in the code challenge.

https://repl.it/B9Jw/0

using System;

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

            if(int.TryParse(input, out amount)){
              Console.WriteLine(amount);
            }else{ 
                while(true){
                    Console.WriteLine("You must enter a whole number");
                    input = Console.ReadLine();
                    if(int.TryParse(input, out amount)){
                        break;
                    }
                }

            }

            for(var i = 0; i<amount; i++){
              Console.Write("Yay!");
            }
        }
    }
}

Am I doing something wrong?

It seems great for me!

1 Answer

Steven Parker
Steven Parker
216,057 Points

It looks like you have a couple of different issues that might cause you to fail the challenge.

For one thing, if you input a numeric value the first time, it is printed out before the words. This might throw off the automatic validation mechanism.

Also, while your use of int.TryParse is perfectly adequate for the desired behavior, the challenge included the comment "Hint: Catch the FormatException". It could be that the validation is looking specifically for a try...catch construct around a call to int.Parse in your code. Take a look at the element of this module titled "try" Code and "catch" Exceptions for examples.

You might need to fix either and maybe both of these to pass the challenge validation.