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

Final Code Challenge

Hello !

Actually I don't know whether my code is good or not. In the exercise there is no range of numbers for the loop but how can you loop with no comparison with an other number ?

I used 10 to start but got an error telling "I entered 5, but saw Yay! printed 6 times". I used the same code in Visual Studio and I have 5 "YAY" (cf. image link)... I'm lost.

And I tried to look on Google and MSDN how to track the number of time an entry is printed with While in C# but can't find it. Someone got an idea ?

Image link : http://1drv.ms/1RMsCZN

Thank you !

Program.cs
using System;

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

            var userEntry = Console.ReadLine();

            var repeat = int.Parse(userEntry);

            while (repeat < 10)
            {
                Console.WriteLine("YAY");
                repeat++;
            }

            Console.WriteLine("YAY was printed " + repeat + " times");

        }
    }
}

4 Answers

Rune Andreas Nielsen
Rune Andreas Nielsen
5,354 Points

I found a solution.

The problem with your code was that, you wrote this out:

Console.WriteLine("YAY was printed " + repeat + " times");

It has yay in it, so the solution checker that TreeHouse use, takes the yay you write out, as a part of the counter, if it makes sence. So it says that u wrote YAY 6 times even tho the loop only wrote out YAY 5 times.

In short, it counted "YAY", that you wrote on last line.

using System;

namespace Treehouse.CodeChallenges
{
    class Program
    {
        static void Main()
        {

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

            var userEntry = Console.ReadLine();

            var repeat = int.Parse(userEntry);
            var counter = 0;

            while (counter < repeat)
            {
                Console.WriteLine("YAY");
                counter++;
            }
        }
    }
}
Rune Andreas Nielsen
Rune Andreas Nielsen
5,354 Points

Hi Mika, your problem is that you enter what the current value of the repeat is. This means that if you enter 5, it will run 5 times, but 10-5 = 5.

In other case if you say 10, then 10-10 = 0, so the loop will never run, because your condition in the while loop checks if it is less than 10.

The way i will deal with it is putting in a new variable named counter, set it to 0, and use that as a condition statement in the while loop, and that way check if it is lower than the user input.

        static void Main(string[] args)
        {
            Console.Write("Enter the number of times to print \"Yay!\": ");

            var userEntry = Console.ReadLine();

            var repeat = int.Parse(userEntry);
            var counter = 0;

            while (counter < repeat)
            {
                Console.WriteLine("YAY");
                counter++;
            }

            Console.WriteLine("YAY was printed " + repeat + " times");

        }

This work great ! Thank you :) I It gives me the same error in the challenge, this is weird. Maybe they should fix something. :(

Rune Andreas Nielsen
Rune Andreas Nielsen
5,354 Points

Sounds strange, will try to do it in 10mins, to check if i can finish it. Will reply if i find a solution.

Awesome, I passed with this one. :) Thank you!!