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

Add input validation to your program by printing “You must enter a whole number.” if the user enters a decimal or someth

?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            bool KeepGoing = true;
            int counter = 0;
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string entry = Console.ReadLine();
 while(KeepGoing)
 {
   try
   {  
            int minutes = int.Parse(entry);
            Console.WriteLine("Yay!");
              counter = counter + 1;
              if(counter != minutes)
                {
                  continue;
                }
              else
                {
                  KeepGoing = false;
                }
   }
   catch(FormatException)
   {
           Console.Write("You must enter a whole number.");
           entry = Console.ReadLine();
     continue;
   }

 }

        }
    }
}
Mayron van Leenden
Mayron van Leenden
9,979 Points

Hi try this solution:

class Program
{
      static void Main()
         {

        var counter = 0;

        Console.Write("Enter the number of times to print \"Yay!\": ");
        var input = Console.ReadLine();

        try
        {
            var times = int.Parse(input);

            while (counter < times)
                {
                    Console.Write("Yay!");
                    counter += 1;
                }

         }

        catch (FormatException)
        {
            Console.WriteLine("you must enter a whole number");
        }

        Console.ReadLine();
    }
}

}

2 Answers

Steven Parker
Steven Parker
230,230 Points

People often over-think this one and get too creative, and their answer is rejected for a variety of reasons (some which don't seem to make any sense).

Here are the secret "rules" you must follow for success on this one:

  • Your program must ask for and accept input ONE TIME ONLY
  • If the input validates, your program will perform exactly as in Task 1
  • If the input does not validate, it will print the error message and exit
  • Validation must be done by exception catching. There are other perfectly good methods that can be used for validation, but they will not pass this challenge

:point_right: It looks like your code has a loop that breaks rule 3 (and might run forever if the input is bad).

John Lukacs
John Lukacs
26,806 Points
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string numofTimes= Console.ReadLine();
            try{
            int num =int.Parse(numofTimes);
                 for(var i=0; i<num; i++){
         Console.WriteLine("\"Yay!\"");
            }
                catch(FormatException){
                Console.WriteLine("You must enter a whole number.");
                    continue;



            }
        }
    }
}

why is it still not passing