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

Michael Sawyer
Michael Sawyer
2,086 Points

Please help with Challenge Task 1 of 3 in the C# basics course Final.

Below is my code and I'm not sure if I'm even close to getting this question correct but now I've thrown and exception. I'm not sure if the Int32.Parse is correct or how to get it to work properly. Please help explain what I'm doing wrong so I could possible see it on my end to correct the problem. Thanks!

using System;

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

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

            string entry = Console.ReadLine();

            if (entry == "quit");
            {
                break;
            }

            try
            {
                int number = Int32.Parse(entry);

                if (number >= 0)
                {
                    Console.WriteLine( number + " is not an exceptable value.");
                    continue;
                }

                else if (number >= 1)
                {
                    Console.WriteLine("Yay!");
                }
            }

            catch(FormatException)
            {
                Console.WriteLine("This is not valid input.");
                continue;
            }

        }        
    }
}

}

Program.cs
using System;

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

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

                string entry = Console.ReadLine();

                if (entry == "quit");
                {
                    break;
                }

                try
                {
                    int number = Int32.Parse(entry);

                    if (number >= 0)
                    {
                        Console.WriteLine( number + " is not an exceptable value.");
                        continue;
                    }

                    else if (number >= 1)
                    {
                        Console.WriteLine("Yay!");
                    }
                }

                catch(FormatException)
                {
                    Console.WriteLine("This is not valid input.");
                    continue;
                }

            }        
        }
    }
}

1 Answer

Chris Adamson
Chris Adamson
132,143 Points

This problem is a little simpler. the number should only be read in once, and used in the loop counter. The while(true) loop isn't necessary:

using System;

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

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

                string entry = Console.ReadLine();

                try {
                    int number = Convert.ToInt32(entry);

                    for(int i = 0; i < number; i++) {
                        Console.WriteLine("Yay!");
                    }
                } catch(FormatException) {
                    Console.WriteLine("This is not valid input.");
                }
        }
    }
}