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 - Challenge Task 2 of 2 - Always get the BUMMER feedback!!!

I am working on my challenge tasks but I cannot finish it as i always get the BUMMER back!!! If i type the code into the workspace it does not get any issues and works as expected. So I am not sure where I am wrong. Please let me know your thoughts ... Thanks in advance

Challenge Task 2 of 2

Add more input validation to the program by printing "You must enter a positive number." if the user enters a negative number.

Add an if/else conditional statement just after parsing the user's provided input to an integer Write a conditional expression to ensure that the remainder of the code in the try block is only executed if the user provides a non-negative number If the user enters a negative number, output to the console the message "You must enter a positive number."

Code -

int count = 0;

        while (true)
        {
        Console.Write("Enter the number of times to print \"Yay!\": ");
        string input = Console.ReadLine();

            try
                {
                    count = int.Parse(input);
                        if (count <= 0)
                        {
                        Console.WriteLine("You must enter a positive number.");
                        }
                        else
                        {
                            break;
                        }
                }
                catch
                {
                    Console.WriteLine("You must enter a whole number.");
                }
            }


        int i = 0;
        while(i < count)
        {
            i += 1;   
            Console.WriteLine("Yay!");
        }

2 Answers

Steven Parker
Steven Parker
231,236 Points

The only loop you'll need in this challenge is the one originally provided to write out the messages. In particular, you will not want a loop that would ask the question again.

Once the question has been asked and answered, the program should either print an error message or the correct number of outputs. In either case it should end.

Thanks Steven,

I already freaked out hehe

By the way ... I did the challenge task 1 with the while loop to ask the question again till the user prompts a number and I got a well done :O That's why I never thought about that issue.

Steven Parker
Steven Parker
231,236 Points

Sometimes the challenge won't detect deviations from the instructions, but for best results, always do only what the instructions ask for.