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

Jason Martin
PLUS
Jason Martin
Courses Plus Student 9,316 Points

Can get program to work, but not as intended...

What would be my next step or should I not use a for loop?

Program.cs
using System;

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

            for(var i = int.Parse(entry); i <= 10; i++)
            {
              Console.WriteLine("Yay!");
            }

        }
    }
}

2 Answers

Steven Parker
Steven Parker
231,108 Points

You do need a loop, but your exit condition is probably not what you intended:

            for(var i = int.Parse(entry); i <= 10; i++)

You start your count with the number desired, and stop after you increment past 10. So if you ask for 1, you get 10, and if you ask for 5, you get 6.

You might want to count down and check for zero, or use another variable from the desired count and count up until they match. Either way, you won't need to compare anything to 10.

You might also want to do that Parse on a separate line, not that it matters for this task, but it will make the later tasks a bit easier.

If you get stuck on task 2, search the forum, there are several existing posts with hints for it.