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 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
231,269 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.