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

Chia Yong Lian
Chia Yong Lian
908 Points

I can run my code on Visual Studio or workspaces perfectly but there is error also when i check my work.

Here is my code.

using System;

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

Thanks in advance

Program.cs
using System;

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

2 Answers

Jonathan Muniz
PLUS
Jonathan Muniz
Courses Plus Student 3,923 Points

You moved the line Console.Write("Enter the number of times to print \"Yay!\": "); when the instructions said to leave it as is. Move that statement back outside the while loop where it was originally placed and you should be good.

Tomas Rinblad
seal-mask
.a{fill-rule:evenodd;}techdegree
Tomas Rinblad
Full Stack JavaScript Techdegree Student 12,427 Points

What is the error you get?

One thing that you could do instead of the following code:

while (i < count)
{
Console.WriteLine("Yay!");
 i+=1;
 }

is doing a for loop instead (If you have learned that).