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 trialLiam Andersson
1,396 PointsWhat am I doing wrong?
Yeah, as the title says.. what am I doing wrong? I am getting tired of it. I haven't even been taught how to loop words like this..
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
var entry = int.Parse(Console.ReadLine());
try
{
for(var i =0; i < entry; i++);
{
Console.Write("Yay!");
Console.WriteLine("Yay was printed " + entry + " times!");
}
}
catch(FormatException)
{
Console.WriteLine("Error!");
}
}
}
}
2 Answers
Steven Parker
231,248 PointsYou're actually doing pretty well here. You just have a few issues to fix:
- there's a stray semicolon at the end of the "for" line that is preventing the following block from being looped
- for the "try" to detect conversion issues, the int.Parse must be done inside the "try" block
- the message about how many times is not part of the instructions and confuses the verifier
- the challenge expects the error message to be "You must enter a whole number." instead of "Error!"
michaelcodes
5,604 PointsHi there! for this you can use a simple while loop and do:
while(count > 0) {
Console.WriteLine("Yay!");
count--;
}
For the try catch you wanna use something like this, Remember to declare count outside of the try-catch!
int count;
try{
count = int.Parse(Console.ReadLine());
} catch(FormatException) {
Console.WriteLine("You must enter a whole number.");
}
Anyone more questions let me know! Hope this helps, and Happy coding :)
Steven Parker
231,248 PointsFYI: there's no advantage to using a "while" loop vs. a "for" loop if both are properly constructed.
michaelcodes
5,604 PointsI only said that because he referenced not being taught "for" loops yet
michaelcodes
5,604 PointsAnd I know these code challenges can be very picky sometimes, was not sure if it was specifically looking for a while loop :)
Liam Andersson
1,396 PointsLiam Andersson
1,396 PointsAh Okay, i will have a look at it tomorrow then. Haha Yeah I just wrote ettor because Iām just at the First step and added try and Catch because I checked the community answers to compare and they had try and Catch in theirs so I just added it!
You are extremely quick to answer questions! Keep it up!
Steven Parker
231,248 PointsSteven Parker
231,248 PointsI just happened to be online! And I just figured you were on step 2, you'll need to do those things then.
But to get the most out of the challenges, you should really try to write them yourself. You might look around for examples but don't just paste them in, you're likely to be getting someone else's mistakes like this time!
And just in general, do only what the challenge asks for in each task. Even good code that does extra stuff can throw off the verifier.
Liam Andersson
1,396 PointsLiam Andersson
1,396 PointsAh Ye I will try.. the problem was this time I didnt have any Idea What to do. I knew that I could use while But i didnt know why. Well Thanks for your help dude