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

Zoltán Makó
Zoltán Makó
1,503 Points

I Got Bummer! Try again! Code was tested in Visual Studio multiple times, whats wrong?

Hey guys, so im currently at Task 2 and i had to put input validation into my code to catch Exception. I made the input validation, but it seems like something wrong because the challenge wont accept it just throwing Bummer! Try again! at me. So i tested in Visual Studio and it worked perfectly. What im missing?

i looked info for the forum before posted here and i found this:

Here are the secret "rules" you must follow for success on this one:

Your program must ask for and accept input ONE TIME ONLY If the input validates, your program will perform exactly as in Task 1 If the input does not validate, it will print the error message and exit Validation must be done by exception catching. There are other perfectly good methods that can be used for validation, but they will not pass this challenge

i checked my program as it says:

  1. One input at a time: i have one input at a time. 2.If the input validates, your program will perform exactly as in Task 1 : i just changed the codes a bit 3.If the input does not validate, it will print the error message and exit : there is my message and my break; 4.I catched the validation fith FormatException

Please help me :) Thanks!

using System;

namespace Treehouse.CodeChallenges
{
  class Program
    {
        static void Main()
        {
            bool cycle = true;
            int loopTimes = 0;

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

            while (cycle)
            {
                try
                {
                    loopTimes = loopTimes + 1;
                    int converTedtimes = int.Parse(numberOftimes);
                    Console.WriteLine("Yay!");
                    if (loopTimes == converTedtimes)
                    {
                        cycle = false;
                    }
                }

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

2 Answers

Kjetil Lorentzen
Kjetil Lorentzen
13,360 Points

Hi!

I reckon this has to do with the code challenges expecting quite explicit response to code challenges.

I have added a code snippet that should work for this challenge. My motto when solving these challenges is to keep it simple, as adding more code tends to throw the code validator off.

Notice that I have put all my code into the "Try" block, except the first Console message.

using System;

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

            while (counter < numPrint)
            {
                Console.WriteLine("Yay!");
                counter ++;
            }
            }
            catch(FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
            }
        }
    }
}

Check your if statement. I think you should check if looptimes <= to convertedTimes