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 Pointsmust enter positive number
My code is still not working. Who can help me? Thanks in advance.
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
try
{
userSpecifiedValue = Convert.ToInt32(Console.ReadLine());
}
catch (FormatException fe)
{
if(userSpecifiedValue<=0) {
Console.WriteLine("You must enter a positive number.");
} else {
Console.WriteLine("You must enter a whole 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,269 PointsThe test for a positive number should be done after the catch block.
That's what I suggested in your previous question.
But you're not doing yourself any favors by starting with another student's code that was bad to begin with and I gave him hints to fix. To get the most learning value from this exercise, you should start the challenge over, and follow the instructions, and write the code yourself. You might manage to make this code pass, but you're just applying "hack-n-slash" techniques to bad code instead of learning how to write good code to start with.