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

Ashim Aryal
Ashim Aryal
3,162 Points

C#

Add input validation to your program by printing “You must enter a whole number.” if the user enters a decimal or something that isn’t a number. Hint: Catch the FormatException.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string x = Console.ReadLine();
            bool keepgoing = true;
            while (keepgoing)
            {

                try
               {
                    int y = int.Parse(x);

                    if(y <= 0)
                    {
                        Console.WriteLine("You must enter a whole number.");
                        continue;
                    }

                    while(y > 0)
                    {
                        Console.WriteLine("Yay");
                        y--;
                    }
                   keepgoing = false;

                }

                catch (FormatException)
                {
                    Console.Write("You must enter a whole number.");
                    continue;
                }  
            }

        }
    }
}

2 Answers

Raffael Dettling
Raffael Dettling
32,999 Points

Got it for you. Your catch methode should test if it´s a hole number so you don´t need the first if statment in 2/3. In 3/3 you need the if statment to check if the input is negativ Hope it helps :)

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            try
            {
                int input = int.Parse(Console.ReadLine());
                if(input > 0)
                {
                    for(int i = 0; i < input; i++)
                        {
                            Console.Write("Yay!");
                        }
                }
                else
                {
                      Console.WriteLine("You must enter a positive number."); 
                }
            }
            catch(FormatException)
            {
                Console.WriteLine("You must enter a whole number.");             
            }            
        }
    }
}
Ashim Aryal
Ashim Aryal
3,162 Points

Thank you so much, :)

Raffael Dettling
Raffael Dettling
32,999 Points

You forgot the ! at YAY :D Took me a moment ^^

Ashim Aryal
Ashim Aryal
3,162 Points

I changed the code, it still dint work though :(

Allan Clark
Allan Clark
10,810 Points

what is the error you are getting?