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

Getting the "bummer! Try again!" error with no specification to what im doing wrong nor any console errors

No idea what i'm doing wrong, from what i can tell, the code does what it's supposed to do.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            var total = 0;

            while(true)
            {
                try
                {
                    Console.Write("Enter the number of times to print \"Yay!\": ");
                    int input = Convert.ToInt32(Console.ReadLine());

                    total = input + total;

                    while(total > 0)
                    {
                        Console.Write("Yay!");
                        total = total - 1;
                    }
                }
                catch(FormatException)
                {
                    Console.WriteLine("that is not a valid input, please try again");
                    continue;
                }
            }


        }
    }
}

2 Answers

Lukas Hölzer
Lukas Hölzer
37,349 Points

I changed your code a bit and it worked fine. I just deleted the first while loop and the continue. Remember to print out “You must enter a whole number.” in your first catch clause.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            var total = 0;
                try
                {
                    Console.Write("Enter the number of times to print \"Yay!\": ");
                    int input = Convert.ToInt32(Console.ReadLine());

                    total = input + total;

                    while(total > 0)
                    {
                        Console.Write("Yay!");
                        total = total - 1;
                    }
                }
                catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");

                }

        }
    }
}

but won't this make it so the code stops after the catch writeline? from what i can tell if some one types anything that can trigger an formatexception it'll say "You must enter a whole number." and then stop right? don't i have to make the code reset through a loop?

also, im getting the exact same thing on the challenge after that one

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            var total = 0;
                while(true)
                {
                    try
                    {
                        Console.Write("Enter the number of times to print \"Yay!\": ");
                        int input = Convert.ToInt32(Console.ReadLine());

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

                            while(total > 0)
                            {
                                Console.Write("Yay!");
                                total = total - 1;
                            }
                        }
                    }
                catch(FormatException)
                {
                    Console.WriteLine("You must enter a whole number.");
                }
                }

        }
    }
}