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

Louis Colbert
Louis Colbert
709 Points

wont compile

try { var input = Console.ReadLine();

            if(input <= 0)
            {
                Console.WriteLine("You must enter a positive number.");
                break;
            }   

within this code It will not compile because it says that it is not valid in that you cannot perform a <= on string. so when I convert to double or atleast try to it will not allow this. can someone tell me why this is not possible by just changing it to a 0.0 and then running it as such or similarly (double)input <= 0

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {

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

            try
            {
                var input = Console.ReadLine();

                if(input <= 0)
                {
                    Console.WriteLine("You must enter a positive number.");
                    break;
                }   
                else
                {
                     int count = int.Parse(input);

                     int i = 0;
                     while(i < count)
                        {
                            i += 1;   
                            Console.WriteLine("Yay!");



                        }


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


            }
        }
    }
}

1 Answer

Antonio De Rose
Antonio De Rose
20,885 Points
\\good one, almost there 3 bugs

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {

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

            try
            {
                var input = Console.ReadLine();

                if(input <= 0)// 1) this will not work, cause you are checking a string on a  greater than 0, convert into int before //checking
                {
                    Console.WriteLine("You must enter a positive number.");
                    break;// 2) you do not need this
                }   
                else
                {
                     int count = int.Parse(input); // 3) this will have to go before checking the positive

                     int i = 0;
                     while(i < count)
                        {
                            i += 1;   
                            Console.WriteLine("Yay!");



                        }


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


            }
        }
    }
}