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

Anusha Singh
PLUS
Anusha Singh
Courses Plus Student 22,106 Points

I just have an issue here.

Here's the question-"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." Please find out what I have done wrong here.

Thanks

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            string number = Console.ReadLine();
            try {
            int number1 = int.Parse(number);
            int numberone = 0;

            bool cycle = true;
            while (cycle == true) {
                Console.WriteLine("Yay!");
                numberone = numberone + 1; 
                if (numberone == number1) {
                     break;
                }
             }
            } catch(FormatException) {

                    Console.WriteLine("You must enter a whole number");

            }

        }
    }
}

Hello!

Have you tried using both , and . to define decimals?

Usually you can type a decimal as 3.50, but it's also correct to type it as 3,50. I mean you can type them using both a comma and a point.

Sorry, I read your question wrong at first.

The boolean variable is not necessary. We could simplify that into

while (true) {
    Console.WriteLine("Yay!");
    numberone = numberone + 1; 
    if (numberone == number1) {
        break;
    }
}

But we can also remove the if statement inside and put the condition inside the while statement. Instead of checking if they are equal and break the iteration, we can iterate as long as the two values are different:

while (numberone != number1) {
    Console.WriteLine("Yay!");
    numberone = numberone + 1; 
}

Changing this made it work for me.