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

Luka Cabarkapa
Luka Cabarkapa
1,022 Points

Why is not working?

Only thing I get is error try again but nothing else, I dont know where is the problem here?

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {   
           int i=0;
            Console.Write("Enter the number of times to print \"Yay!\": ");
             var input =Console.ReadLine();
            try{
                var num=double.Parse(input);
                if(num<=0)
                {
                    Console.WriteLine("You must enter a postive num.");
                }
                else
                {
                while(i!=num)
                {
                Console.WriteLine("Yay!");
                    i++;
                }
              }
            }
            catch(FormatException)
            {
            Console.WriteLine("That is not a valid input.");
            }

        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,096 Points

You're using the wrong kind of conversion.

You want to catch when "the user enters a decimal or something that isn’t a number", but your converting the input using double.Parse. That won't cause exceptions for decimals, it will just convert them. You probably want to use int.Parse instead.

Also, the challenges are very picky about output strings, and it wants to see β€œYou must enter a whole number.” on a bad input. But your exception message is "That is not a valid input."