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

Anders Sundbø
Anders Sundbø
1,531 Points

[Solved]Works in Workspace but the test will not Accept the solution

Im at part two of the final test of the C# basic course. When I test the solution in the Workspace it Works perfectly but somehow the assignment wont Accept the solution. Can anyone see why?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            var counter = 0;

            while(true)
            {
                Console.WriteLine("Enter the number of times to print \"Yay!\": ");

                var entry = Console.ReadLine();

                try
                {
                    var number = Int32.Parse(entry);

                    while(true)
                    {
                        Console.WriteLine("Yay!");
                        counter++;
                        if(number == counter)
                        {
                            break;
                        }
                    }
                    break;
                }
                catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    //continue;
                }
                catch(ArgumentNullException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    //continue;
                }
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,236 Points

:point_right: Your program "works", but deviates from the instructions.

This is a great example of how something that works (will compile an run) might not be what was asked for (satisfies the requirements). This has been a topic of many previous questions, but I'll repost the info here:

People often over-think this one and get too creative, and their answer is rejected for a variety of reasons (some which don't seem to make any sense).

Here are the secret "rules" you must follow for success on this one:

  • Your program must ask for and accept input ONE TIME ONLY
  • If the input validates, your program will perform exactly as in Task 1
  • If the input does not validate, it will print the error message and exit
  • Validation must be done by exception catching. There are other perfectly good methods that can be used for validation, but they will not pass this challenge

It looks like you have an extra loop that is violating the first and third "rule".

Anders Sundbø
Anders Sundbø
1,531 Points

Thanks Steven! Appreciate it :-)

Anders Sundbø
Anders Sundbø
1,531 Points

Hi again and sorry to bother you on this fine sunday. I have removed the loop and the program is not (as I can see) violating any of the rules. using System;

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

        try
        {
            var number = Int32.Parse(entry);
            while(true)
            {
                Console.WriteLine("Yay!");
                counter++;
                if(number == counter)
                {
                    break;
                }
            }
        }
        catch(FormatException)
        {
            Console.WriteLine("You must enter a whole number.");
        }
        catch(ArgumentNullException)
        {
            Console.WriteLine("You must enter a whole number.");
        }
    }
}

}