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

Amanda Free
Amanda Free
11,375 Points

Why is this code running 6 times when it should run 5 times?

Here's my code:

Program.cs
using System;

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

           while(runningTotal < entry) {
                Console.Write("Yay!");
                runningTotal += 1;
                }

            Console.Write("You printed \"Yay!\" " + entry + " times!");
        }
    }
}

1 Answer

Steven Parker
Steven Parker
231,269 Points

:point_right: You may be printing more "Yay!"s than you think.

The challenge is counting the output from this line as an additional "Yay!":

            Console.Write("You printed \"Yay!\" " + entry + " times!");

That line isn't part of the challenge anyway, so you can just remove it. In general, be careful not to do more than the challenge asks, that can cause the challenge to fail even it the code works fine in a REPL.