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 trialNathan Carstairs
7,906 PointsUnable to finish C# Basics Final Code Challenge
This is the Code Challenge: https://teamtreehouse.com/library/c-basics/perfect/final My code is as follows:
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
while(true)
{
int timesRan = 0;
Console.Write("Enter the number of times to print \"Yay!\": ");
string entry = Console.ReadLine();
try
{
int times = int.Parse(entry);
while(timesRan < times)
{
Console.WriteLine("\"Yay!\"");
timesRan += 1;
}
break;
}
catch (FormatException)
{
Console.WriteLine("You must enter a whole number.");
continue;
}
}
}
}
}
It successfully got me through step 1 and while it should pass step 2 as well, it does not. I know it should because it worked when I tried it in a Treehouse Workspace. The result in the Workspace was a FormatException, as expected, but the Code Challenge itself returned an ArgumentNullException seen below.
Bummer! 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 () [0x00012] in <f437b8f53cd1432eb8d9002d5e45d560>:0 at MonoTester.Run () [0x00208] in /workdir/MonoTester.cs:140
Any help that can be offered is appreciated.
1 Answer
Steven Parker
231,271 Points Be careful about testing a challenge in an external REPL or IDE.
If you have misunderstood the challenge, it's also very likely that you will misinterpret the results.
In this case, you have added an extra loop that prevents the program from ending if bad values are entered..
A program that follows the challenge instructions will only ask for input one time, and either print "Yay!"s or an error message. But either way it should end.
Nathan Carstairs
7,906 PointsNathan Carstairs
7,906 PointsThank you Steven. I took out the extra loop and it worked so I will be sure to watch out for differences in REPL / IDE from now on.