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

A. D.
A. D.
4,192 Points

Can someone can show me how to tackle this challenge: Add a check for user entering negative number input?

I've tried different while loops, and tried adjusting the scope in various ways. There's a good chance that my base solution is the problem. At these point I'd be grateful if someone took my artistic brain by the hand and just showed me how to do this. I'm truly stuck. Thank you!

Program.cs
using System;

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


            try
                {
                    var yayTimes = int.Parse(entry);

                   /* if (yayTimes < 0)
                        {
                            Conaole.WriteLine("You must enter a positive number.");
                            continue;
                        }*/

                    while(yayTimes > 0)
                    {
                        Console.WriteLine("Yay!");
                        yayTimes--;
                        continue;
                    }
            }
        catch(FormatException)
        {
            Console.WriteLine("You must enter a whole number.");
        }
            Console.ReadLine();
        }
    }
}

3 Answers

/*I'm not 100% on how to use the answer forum but this is an example of a correct answer. I don't think you need the continue in your while loop. Also you do have a spelling error in your commented out section. Also in the instance that yayTimes was not less than zero, you would need an else surrounding the while loop to tell the code where to go next. Finally the final "Console.ReadLine" is not necessary. On a bright note, your use of the decrement operator on yayTimes as opposed to instantiating a counter and comparing against it was cool. Anyways this code should work and I hope I explained things well. Once again, sorry for the formatting. I am not familiar with the forums. */

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");

            try
            {
                string val = Console.ReadLine();
                int count = 0; 
                int num = Int32.Parse(val);
                    if (num < 0)
                        {
                            Console.WriteLine("You must enter a positive number.");   
                        } else
                            while (count < num)
                        {
                            Console.WriteLine("Yay!");
                            count++;
                        }
            }
            catch (FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");  
                }

        }
    }
}
A. D.
A. D.
4,192 Points

Oh, thank you so much, Matt! I haven't had the time to try it out yet, but I'm so eager to move on and this makes sense. If I recall correctly, I added the continue because when I was trying the code out in Visual Studio, the console window would disappear unless I added the continue. I wanted to see if the console did indeed print out "Yay!" the number of times I'd specified. I had tried one version of the code that always gave me one more than expected. Exasperating. Hah.

A. D.
A. D.
4,192 Points

I had tried the else wrapping, but maybe the misspelling was throwing me off. Thanks again!