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 trialAdis Palic
548 PointsCode is not working in challenge but works fine in workspace
Hello I wrote same code in workspace and it worked as expected, but it's not working in code challenge, why?
code challenge 2 is about adding input validation. and using try and catch to see if right input is there. for this one I need to add whole number to make it work.
Anyone that can see the problem? and why?
Error: Program.cs(39,246): error CS1525: Unexpected symbol `end-of-file' Compilation failed: 1 error(s), 0 warnings
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
while (true)
{
Console.Write("Enter the number of times to print \"Yay!\": ");
var entry = Console.ReadLine();
int y = 0;
if (entry.ToLower() == "quit")
{
break;
}
try
{
int x = int.Parse(entry);
while (y < x)
{
Console.WriteLine("Yay!");
y++;
}
}
catch(FormatException)
{
Console.WriteLine("You must enter a whole number.");
continue;
}
Console.WriteLine("Goodbye!");
}
}
}
2 Answers
Steven Parker
231,236 PointsYour program "works", but deviates from the instructions.
This is a great example of how something that works (will compile an run) might not be what was asked for (satisfies the requirements). This has been a topic of many previous questions, but I'll repost the info here:
People often over-think this one and get too creative, and their answer is rejected for a variety of reasons (some which don't seem to make any sense).
Here are the secret "rules" you must follow for success on this one:
- Your program must ask for and accept input ONE TIME ONLY
- If the input validates, your program will perform exactly as in Task 1
- If the input does not validate, it will print the error message and exit
- Validation must be done by exception catching. There are other perfectly good methods that can be used for validation, but they will not pass this challenge
In this case, it looks like you have an outer loop and a "goodbye" message that aren't part of the requirements.
Adis Palic
548 Pointslol now i got your answer on only accepting one input :)
didn't need while(true) :D
fixed and working code
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
var entry = Console.ReadLine();
int y = 0;
try
{
int x = int.Parse(entry);
while (y < x)
{
Console.WriteLine("Yay!");
y++;
}
}
catch(FormatException)
{
Console.WriteLine("You must enter a whole number.");
}
}
}
}
Steven Parker
231,236 PointsNot that I care about the points (particularly since the Leaderboards have not worked for over a year), but you're supposed to select the "best answer" from the feedback that helped you most, not from your own repost.
Adis Palic
548 PointsAdis Palic
548 Pointsnow i striped code for everything that i dont need, and surprise surprise it gots another error, this time "ArgumentNullException"
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.4.2/external/referencesource/mscorlib/system/number.cs:1074 at System.Number.ParseInt32 (System.String s, NumberStyles style, System.Globalization.NumberFormatInfo info) [0x00014] in /builddir/build/BUILD/mono-4.4.2/external/referencesource/mscorlib/system/number.cs:745 at System.Int32.Parse (System.String s) [0x00000] in /builddir/build/BUILD/mono-4.4.2/external/referencesource/mscorlib/system/int32.cs:120 at Treehouse.CodeChallenges.Program.Main () <0x41261f10 + 0x0005f> in :0 at MonoTester.Run () [0x00095] in MonoTester.cs:86 at MonoTester.Main (System.String[] args) [0x00013] in MonoTester.cs:28
Code looks like this now: