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 triallouie solis
1,126 Points[Solved] Task 1 is no longer passing C#
I'm not sure what is going on. I have build this code from Visual Studio 2017 and its working just fine even without the Console.ReadKey its not quitting out. Negative numbers input will give me a message that I am expecting, to use a positive number. Typing string of letters will ask me to input whole number. And finally typing whole positive number will print the number of Yay! I instructed the program to print. Please help. Thanks
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
while (true)
{
string yay = "Yay!";
int number = 0;
Console.WriteLine("Enter the number of times to print \"Yay!\": ");
string userPrint = Console.ReadLine();
try
{
number = int.Parse(userPrint);
if (number <= -1)
{
Console.WriteLine("You must enter a positive number.");
}
while (number > 0)
{
Console.WriteLine(yay);
number--;
}
}
catch (FormatException)
{
Console.WriteLine("You must enter a whole number.");
}
}
}
}
}
4 Answers
Steven Parker
231,269 PointsI can explain why the original program did not pass.
The challenge expects that when given good input, the program will print a number of outputs and then end. When given bad input, the program will print an error message and then end. But either way, it should end.
That loop caused the program to repeat and prevented it from ending.
Steven Parker
231,269 PointsCongratulations on resolving your own issue.
If it makes you feel any better, that's a very common mistake for some reason.
louie solis
1,126 PointsI just started learning C# about a week ago, actually I just started learning programming. I didn't even understand why removing the while (true) fixed the problem.. LUL
louie solis
1,126 PointsThank you Steven. Now I understand much better.
louie solis
1,126 Pointslouie solis
1,126 Pointsnever mind i have this working i just remove the while (true)...