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

Daren Davis
Daren Davis
6,929 Points

C# basic Final. Works on mono, but can't find bug. (spelling error?)

Problem is in try block. Problem was to add more input validation and if input was negative, display "You must enter positive numbers." Thanks in advance for your help.

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            var input = 0;
            try
            { 
              input =  int.Parse(Console.ReadLine());
              if(input < 0)
              {
                while(input < 0)
                {
                    Console.WriteLine("You must enter a positive number.");
                    Console.Write("Enter the number of times to print \"Yay!\": ");
                    input =  int.Parse(Console.ReadLine());
                }
              }
            }
            catch(FormatException)
            {
                Console.WriteLine("You must enter a whole number.");
            }
            catch
            {
              Console.WriteLine("You must enter a positive number.");   
            }    
            var counter = 0;
            while (counter != input)
            {
                Console.WriteLine("\"Yay!\"");
                counter += 1;
            }



        }
    }
}

3 Answers

Steven Parker
Steven Parker
231,269 Points

:point_right: Your program "works", but deviates from the instructions.

This is a great example of how something that works (will compile an run) might not be what was asked for (satisfies the requirements). This has been a topic of many previous questions, but I'll repost the info here:

People often over-think this one and get too creative, and their answer is rejected for a variety of reasons (some which don't seem to make any sense).

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 specified 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

It looks like you have a loop and a second ReadLine that both deviate from the first requirement. You also seem to have an extra catch block.

Daren Davis
Daren Davis
6,929 Points

thx.. got it. I've been at the computer too long. thanks for your help.

There's something curious when you run your code. If you first type a negative int and move into the while loop where it asks you to enter a positive number again, then if you enter a string instead of an int it just prints "Yay!" forever. If anyone can tell me why that is I'd be very interested as I thought it would crash the program.