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 trialMaarten De Kruif
7,314 PointsGet the error "Oops! It looks like Task 1 is no longer passing.".
What is wrong with my code?
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
int numberCount = 0;
int userSpecifiedValue = 0; // added initialization here
if(Convert.ToInt32(Console.ReadLine())<=0)
{
Console.WriteLine("You must enter a positive number.");
}
try
{
userSpecifiedValue = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException fe)
{
Console.Write("You must enter a psoitive number.");
//Console.ReadLine(); <- this would confuse the challenge
}
//userSpecifiedValue = numberCount; // do not change the user value!
while (userSpecifiedValue > numberCount) // put the loop back in
{
//userSpecifiedValue ++; <- don't increment the user's value
numberCount++; // ...increment the counter instead!
Console.Write("Yay!");
//Console.ReadLine(); <- this would confuse the challenge
}
}
}
}
1 Answer
Steven Parker
231,248 PointsThe error message may be misleading, but I do see a couple of issues:
- In task 2, if the conversion to integer failed you were instructed to print out "You must enter a whole number.". But your code above prints out "You must enter a psoitive number." instead.
- You call ReadLine in two places, but the program should only take input one time.
Another hint: Since you already convert the input to a number in the try block, you might just put a simple test for negative after the catch.
Boy, those comments look awfully familiar — are you copying code directly from an answer I gave to another student last month? Shame on you!