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
173,876 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
173,876 Points

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