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 trialJose Badillo
1,186 PointsI don't know what else to try in order to pass the test for C#. I just cant seem to create a working loop.
I cant seem to be able to create a loop with some decent input validation to pass the C# Basic test this is ridiculus because I literally finished the whole entire class in less the 30 hourse and took some very detailed notes on my findings can someone please help me...?
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
string userInput = Console.ReadLine();
int number = int.Parse(userInput);
bool repeat = true;
while (repeat == true)
{
try
{
int number;
if (number == 0)
{
break;
}
if else
{
Console.WriteLine("Yay!");
number =number -1;
}
if else
{
Console.WriteLine("Yay!");
number =number -1;
}
catch (FormatException)
{
Console.WriteLine("You must enter a whole number.");
continue;
}
}
}
}
}
2 Answers
Steven Parker
231,210 PointsHere's a few hints:
- for the "try" to be effective, the conversion (parse) must be done inside its block
- the conversion/try only need to be done once and should be outside of the loop
- "if else" is not proper syntax, you probably meant "else"
- you can only have (and only need) one "else" block at the end of a conditional sequence
- the program needs to end after it has completed the output
And while not necessary to pass the challenge, a "best practice" loop will test the limiting condition directly and not require a conditional statement inside to determine when to finish.
michaelcodes
5,604 PointsHi there! For this final assignment they actually do NOT want you to continuously loop for input, they only want you to catch the exception. The code will NOT pass if you have a loop, this was causing me to fail the challenge for almost an hour until I finally looked up help and got rid of the loop and the code passed no problem.
Steven Parker gave many good recommendations to follow to fix the code, just wanted to let you know about the loop.
Take care and happy coding!