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

Radu - Adrian Buha
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 Points

Code works fine in Workspace but it's not good enough to pass the 3rd code challenge at the end of C# Basics.

Hi. So, I am at the end of C# Basis and there's a code challenge. I have successfully completed the first two challenges but at the third, my code doesn't seems to be good enough for me to pass. I have already tried my code in the Workspace and it works just fine.

Can anyone tell what am I doing wrong?

Thanks in advance!

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            while(true){
                Console.Write("Enter the number of times to print \"Yay!\": ");
                try{
                    var entry = Console.ReadLine();
                    var numberOfTimes = int.Parse(entry);
                    if(numberOfTimes <= 0){
                        Console.WriteLine("You must enter a positive number.");
                        continue;
                    }else{
                        var countTheTimes = 0;
                        while (countTheTimes < numberOfTimes){
                            Console.WriteLine("Yay!");
                            countTheTimes += 1;
                        }
                        break;
                    }
                    }catch(FormatException){
                        Console.WriteLine("You must enter a whole number.");
                    }

            }

        }
    }
}

1 Answer

Radu - Adrian Buha
PLUS
Radu - Adrian Buha
Courses Plus Student 5,535 Points

Found the issue! The thing is, I over-complicated things. All I had to do is to remove the first while loop (and the adjacent continue and break statements) and everything was ok.