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

Chris M
Chris M
592 Points

Works in workspace but throws an error in testing challenge.

For some reason, this code works perfectly in the workspace. But when I use it in the task challenge it keeps giving me an error.

Program.cs
using System;
namespace Treehouse.CodeChallenges {
 class Program {
  static void Main() {
   while (true) {
    // parse the amount of times you want yay printed out

    Start:
     Console.Write("Enter the number of times to print \"Yay!\": ");
    var ToRpeat = Console.ReadLine();

    // make this all one massive while loop





    // Catching entries that aren't numbers here

    // while loop starts here

    // Display Yay and then sutract 1 from var 'total'
    try {
       var total = int.Parse(ToRpeat);
     while (true) {

      if (total == 0) {

       break;

      }
      if (total < 0) {

       Console.WriteLine("You must enter a whole number.");
       goto Start;

      }
      Console.WriteLine("Yay!");

      total = total - 1;


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

10 Answers

anil rahman
anil rahman
7,786 Points

This will do task 2

using System;

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

          try
          {
            num = Convert.ToInt32(Console.ReadLine());
          }
          catch(FormatException fe)
          {
            Console.Write("You must enter a whole number.");

          }

          int i = 0;

          while(i < num) //counter is less than typed number
          {
              i++; //add 1 to the counter each time you loop
              Console.Write("Yay!");
          }

        }
    }
}
Chris M
Chris M
592 Points

Well I finished task 2 and 3.

Thanks for the help, still not sure why it hated my code so much :(

anil rahman
anil rahman
7,786 Points

That is so complicated and long winded. Keep short and simple :)

using System;

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

          int num = Convert.ToInt32(Console.ReadLine());

          for(int i = 0; i < num; i++)
          {
            Console.Write("Yay!");
          }

        }
    }
}
Chris M
Chris M
592 Points

One thing lead to another and it eventually turned into that beast , haha.

In any case, the program also has to catch exceptions for the test, for whatever reason and I haven't got to for loops yet.

anil rahman
anil rahman
7,786 Points

But you seem fine with while loops, so this is a for loop converted to a while :)

using System;

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

          int num = Convert.ToInt32(Console.ReadLine());
          int i = 0;

          while(i < num) //counter is less than typed number
          {
            i++; //add 1 to the counter each time you loop
            Console.Write("Yay!");
          }

        }
    }
}
Chris M
Chris M
592 Points

Your right! Man, forgot how much I loved for loops.. Haven't touched programming in a while so I'm beyond rusty. i'll try this again, thanks.

anil rahman
anil rahman
7,786 Points

I don't see anything asking for exception checks so that would be extra stuff you can add yourself after :)

Chris M
Chris M
592 Points

Looks like I'm still getting the same error.

using System;
namespace Treehouse.CodeChallenges 
{
 class Program
 {
  static void Main()
  {
   while (true)
   {
    Start: Console.Write("Enter the number of times to print \"Yay!\": ");
    var ToRpeat = Console.ReadLine();
    try {
     var total = int.Parse(ToRpeat);
     for (int i = 0; i < total; i++) 
     {
      if (total == 0)
      {
       break;
      }
      Console.WriteLine("Yay!");
     }
       if (total < 0)
      {
       Console.WriteLine("You must enter a whole number.");
       goto Start;
      }
    } catch (FormatException)
    {
     Console.WriteLine("You must enter a whole number.");
     continue;
    }
    break;
   }
  }
 }
}

I keep getting this error:

System.ArgumentNullException: Value cannot be null. Parameter name: String at System.Number.StringToNumber (System.String str, NumberStyles options, System.NumberBuffer& number, System.Globalization.NumberFormatInfo info, Boolean parseDecimal) [0x00054] in /builddir/build/BUILD/mono-4.2.3/external/referencesource/mscorlib/system/number.cs:1080 at System.Number.ParseInt32 (System.String s, NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /builddir/build/BUILD/mono-4.2.3/external/referencesource/mscorlib/system/number.cs:751 at System.Int32.Parse (System.String s) [0x00000] in /builddir/build/BUILD/mono-4.2.3/external/referencesource/mscorlib/system/int32.cs:140 at Treehouse.CodeChallenges.Program.Main () <0x41b94830 + 0x00058> in :0 at MonoTester.Run () [0x00197] in MonoTester.cs:125 at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:28

Not sure what the deal is. Works fine in the workspace but as soon as I enter it into the test, stuff happens.

anil rahman
anil rahman
7,786 Points

Why are you doing exception catching and number checking. The test doesn't ask you to do any of that. Also sometimes if you do extra things than what they ask for you can get the bummer error even if your code is correct. :)

Chris M
Chris M
592 Points

Well the directions say "Add input validation to your program by printing “You must enter a whole number.” if the user enters a decimal or something that isn’t a number. Hint: Catch the FormatException."

So I figured I had to. I'm on task 2 of 3.

anil rahman
anil rahman
7,786 Points

That's why i gave you the code to pass the test :)

anil rahman
anil rahman
7,786 Points

Oops my bad i didn't see that -_-

Chris M
Chris M
592 Points

All good! Just wish I knew what is going on. I want to just skip it but if it's a bug then I feel like people should know.

anil rahman
anil rahman
7,786 Points

This will do task 2

using System;

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

          try
          {
            num = Convert.ToInt32(Console.ReadLine());
          }
          catch(FormatException fe)
          {
            Console.Write("You must enter a whole number.");

          }

          int i = 0;

          while(i < num) //counter is less than typed number
          {
              i++; //add 1 to the counter each time you loop
              Console.Write("Yay!");
          }

        }
    }
}
anil rahman
anil rahman
7,786 Points

This will do task 2

using System;

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

          try
          {
            num = Convert.ToInt32(Console.ReadLine());
          }
          catch(FormatException fe)
          {
            Console.Write("You must enter a whole number.");

          }

          int i = 0;

          while(i < num) //counter is less than typed number
          {
              i++; //add 1 to the counter each time you loop
              Console.Write("Yay!");
          }

        }
    }
}

This also will help..

Cheers

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {   
            while(true)
            {   
                try
                {
                    Console.Write("Enter the number of times to print \"Yay!\": ");
                    int input = Convert.ToInt32(Console.ReadLine()); 

                    for(int i = 0; i < input; i++)
                    {
                       Console.Write("Yay!");
                    }
                   break;
                }     
                catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                    continue;
                }
           }
      }
    }
}
Simon Coates
Simon Coates
28,694 Points

seems to get through with:

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            int loopsNumber;            
                try {                
                    loopsNumber = int.Parse(Console.ReadLine());
                    if(loopsNumber >= 0){
                        while(loopsNumber > 0){
                            Console.WriteLine("Yay");
                            loopsNumber--;
                        }
                    } else {
                        Console.WriteLine("You must enter a positive number.");
                    }
              }  catch(FormatException){
                    Console.WriteLine("You must enter a whole number.");
               }
        }
    }
}

I think what is happening is that it's running it, but since your code doesn't quite behave as it should, you're seeing an unexpected error in the test code. Without knowing about how the tests are performed, the error seems inexplicable. (I also started out looping until a valid input was entered, and i guess the test code didn't know to keep providing values)

nb: i appreciate that y'all have probably moved on or had psychotic breaks, so this is mostly for future reference.