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 trialjohn peter
Courses Plus Student 1,215 PointsWhere have i gone wrong?
Ive added the try catch but cant seem to know where i made a mistake?
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
Console.Write("Enter the number of times to print \"Yay!\": ");
var y = Console.ReadLine();
try
{
var t = double.Parse(y);
int z = 0;
bool yes = true;
while(yes)
{
Console.Write("Yay!");
z=z+1;
if(t==z)
{
yes = false;
}
}
Console.Write("Youve enetered " +t+" times");
}
catch(FormatException)
{
Console.Write("You must enter awhoel number.");
}
}
}
}
1 Answer
michaelcodes
5,604 PointsHi there! It would be a lot to break down the changes that I have made, so I will post the final code I came up with, with plenty of added comments, read through them and you will see the changes I made.
using System;
namespace Treehouse.CodeChallenges
{
class Program
{
static void Main()
{
//I changes the variable names to be a little easier to read.
int count = 0; //start up our counter variable for the while loop
int varInput = 0; //start up our converted input (input in integer form)
Console.Write("Enter the number of times to print \"Yay!\": ");
var input = Console.ReadLine();
try
{
varInput = Int32.Parse(input); //For this we do not want a float, but rather
//an integer, if its not an integer we catch the error below
} catch(FormatException)
{
//Console.Write("You must enter awhoel number.");
Console.Write("You must enter a whole number."); //We have to make sure this is spelled
//exactly how they ask for it... This is the way the code challenge
//requires it... best to copy and paste (without the quotes).
}
while(count < varInput) //while our count variable (0) is less than the input requested number of times
{
Console.Write("Yay!");
count++; //increment our counter by 1 (same as "count = count + 1" or "count += 1"
}
}
}
}
In this system there is no need for boolean variables. The code challenges are very picky so its best to copy and paste the text they are asking for, and stick strictly to what theyre asking for. I know its fun to code what you want but the code challenges are strict. Hope this helps, take care and happing coding!