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

Dominik Huber
Dominik Huber
4,631 Points

Why is the check for the negative number not working in my example? It says that it returns nothing.

Hi guys,

I'm at the last project for the C# beginner course and I can't make it to pass. I'm at the last section 3/3 and the problem is that I have to check for negative numbers.

In my mind it makes sense how I wrote it but the compiler says that he entered -1 and gots no message displayed.

Best you look for yourself in my .cs I attached.

The exact error I get is:

"Bummer! I entered -1, but nothing was printed out."

What is the problem?

Program.cs
using System;

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


            try 
            {
                int loopcount = 0;
                int count = int.Parse(Console.ReadLine());

                while (loopcount < count) 
                {

                    if (count < 0) 
                    {
                        Console.WriteLine("You must enter a positive number.");
                        count = int.Parse(Console.ReadLine());
                        continue;                 
                    }

                    Console.WriteLine("Yay!");
                    loopcount += 1;

                }
            }

            catch (FormatException)
            {
                Console.WriteLine("You must enter a whole number.");   
            }




        }
    }
}

4 Answers

When the user types in a negative number, the condition of your while loop will not evaluate to true. Hence your loop never starts and your check for a negative number never gets executed.

Dominik Huber
Dominik Huber
4,631 Points

Hi I added a new post to my thread. Maybe you can have another quick look. Really appreciate it mate thx :)

Since the user can input almost anything (or nothing), there's no guarantee that Console.ReadLine() returns a string that int.parse() can successfully parse. It's better to use int.TryParse() to deal with that uncertainty.

int.TryParse(Console.ReadLine(), out count);
Dominik Huber
Dominik Huber
4,631 Points

Thank you that's it. It's now completed :)

Dominik Huber
Dominik Huber
4,631 Points

Thank you that's it. Will rework that code now.

Dominik Huber
Dominik Huber
4,631 Points

Sorry but I have to ask again.

I tried and tried but I can't get it to work and I don't know why. The Error message I got is this: " Bummer! System.ArgumentNullException: Value cannot be null. Parameter name: String. See output for stack trace."

So anyhow the count = int.Parse(Console.ReadLine()); (the one in the if block) gets null assigned, am I right? Why?

The closest I can get to is that code: (Output from Stacktrace at the bottom) /edit: Funny thing. The exact same code in Visual Studio works as intended. Tested it with a positive number, a negative number and zero. All 3 worked just as intended. What am I missing?

using System;

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



            try 
            {
                int loopcount = 0;
                int count = int.Parse(Console.ReadLine());




                while (true) 
                {

                     if (count < 0) 
                     {
                        Console.WriteLine("You must enter a positive number.");
                         count = int.Parse(Console.ReadLine());
                         continue;

                     }

                    if (loopcount < count){
                        Console.WriteLine("Yay!");
                        loopcount += 1;
                    } else {
                        break;
                    }




                }
            }

            catch (FormatException)
            {
                Console.WriteLine("You must enter a whole number.");   
            }




        }
    }
}
System.ArgumentNullException: Value cannot be null.
Parameter name: String
  at System.Number.StringToNumber (System.String str, System.Globalization.NumberStyles options, System.Number+NumberBuffer& number, System.Globalization.NumberFormatInfo info, System.Boolean parseDecimal) [0x00054] in /builddir/build/BUILD/mono-4.8.1/mcs/class/referencesource/mscorlib/system/number.cs:1074 
  at System.Number.ParseInt32 (System.String s, System.Globalization.NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /builddir/build/BUILD/mono-4.8.1/mcs/class/referencesource/mscorlib/system/number.cs:745 
  at System.Int32.Parse (System.String s) [0x00000] in /builddir/build/BUILD/mono-4.8.1/mcs/class/referencesource/mscorlib/system/int32.cs:120 
  at Treehouse.CodeChallenges.Program.Main () [0x0002d] in <7443b08c0fed4b718a9bdc724518d483>:0 
  at MonoTester.Run () [0x002b3] in MonoTester.cs:169 
  at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:28