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 trial

C# C# Basics (Retired) Perfect Final

how to do this question?

im a bit confused here how do i solve this question? i need to allow it to tell if the value entered is bool then it should show me an error message saying enter whole number else it will continue to run. help please

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string y = Console.ReadLine();
            int t = int.Parse(y);
            int z = 0;
            bool yes = true;
            while(yes)
              {
                 Console.Write("Yay!");
                 z=z+1;
                 if(t==z)
                    {
                      yes = false;
                    } 
              }
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

The value entered will not be a bool, it will be a number of how many times to print.

But to add the error checking needed by the next task you will need to use a "try...catch" combination.

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."); }

    }
}

}

Steven Parker
Steven Parker
231,269 Points

It's difficult to read unformatted code, but at first glance it looks like you added an extra message to show the number of times entered (doing things not asked for can confused the validation system), and your error message needs to be spelled the same was as it was given in the instructions.

And when posting code, use the instructions for code formatting in the Markdown Cheatsheet pop-up below the "Add an Answer" area. :arrow_heading_down:   Or watch this video on code formatting.