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

Add input to your program by printing "You must enter whole number." if user enters a decimal or something that isn't a

using System;

namespace Treehouse.CodeChallenges { class Program { static void Main() { Console.Write("Enter the number of times to print \"Yay!\": ");

        string entry = Console.ReadLine();
        int minutes = int.Parse(entry);
        bool nadaljujem = true;
        int stevec = 0;

        while(nadaljujem)
          {
          try
          {
          Console.WriteLien(minutes + "This is not valid input");
          }
          catch(FormatException)
{
Console.WriteLine("That is not valid input.");
  continue;
}  
          Console.WriteLine("Yay!");
          stevec = stevec + 1;
          if(stevec != total)
            {
              continue;
            }
          else
            {
              nadaljujem = false;
            }
          }

    }
}

}

Program.cs
using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");

            string entry = Console.ReadLine();
            int minutes = int.Parse(entry);
            bool nadaljujem = true;
            int stevec = 0;

            while(nadaljujem)
              {
              try
              {
              Console.WriteLien(minutes + "This is not valid input");
              }
              catch(FormatException)
    {
    Console.WriteLine("That is not valid input.");
      continue;
    }  
              Console.WriteLine("Yay!");
              stevec = stevec + 1;
              if(stevec != total)
                {
                  continue;
                }
              else
                {
                  nadaljujem = false;
                }
              }

        }
    }
}
Mazen Halawi
Mazen Halawi
7,806 Points

Hello Andrej, your code looks kinda messy and difficult to read. You have to add a try-catch block before you enter the loop. (see solution below)

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");
            // Start of Challenge Task 2
            try {
              int numberOfTimes = int.Parse(Console.ReadLine());

              // Start of Challenge Task 3
              if (numberOfTimes >= 0) {
                for (int i=0; i<numberOfTimes; i++) {
                  Console.WriteLine("\"Yay!\"");
                } else {
                  Console.Write("You must enter a positive number.");
                }
              else {}
            } catch (FormatException) {
              Console.Write("You must enter a whole number.");
            }
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

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

In addition to the common issues, you also seem to have these:

  • your try block does not enclose int.Parse, which is where the errors are likely to occur
  • you have a method misspelled (Console.WriteLein)