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

Mytch Hagan
Mytch Hagan
1,876 Points

Final code challenge 3 of 3 - "I entered -1 but nothing is printing out" ?

I tried multiple ways, but it still is not working properly

Program.cs
using System;

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

            Console.Write("Enter the number of times to print \"Yay!\": ");
            var entry = Console.ReadLine();
            try
            {
                var yayLimit = int.Parse(entry);
                if (yayRunningTotal <= -1)
                {
                    Console.WriteLine("You must enter a positive number.");
                }
                while (yayRunningTotal < yayLimit)
                {
                    yayRunningTotal += 1;
                    Console.WriteLine("Yay!");
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,269 Points

It looks like you're validating the wrong variable.

When you convert the input to a numeric value, you store it in the variable yayLimit. But when you perform a test for validity you check yayRunningTotal instead (which is still 0 at this point).

Also, check the instructions again but you might want your validity test to exclude 0 as well as negative values.

Antonio De Rose
Antonio De Rose
20,885 Points

please check the comments

using System;

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

            Console.Write("Enter the number of times to print \"Yay!\": ");
            var entry = Console.ReadLine();
            try
            {
                var yayLimit = int.Parse(entry);
                if (yayRunningTotal <= -1)//why are you checking for yayRunningTotal, and the other why is it checking for less than -1
                {
                    Console.WriteLine("You must enter a positive number.");
                }
                while (yayRunningTotal < yayLimit)
                {
                    yayRunningTotal += 1;
                    Console.WriteLine("Yay!");
                }
            }
            catch (FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}