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

Morten Hauge
Morten Hauge
9,240 Points

The name `timesToLoop' does not exist in the current context

As far as I can tell, I am assigning the variable timesToLoop outside of the while loop?

The way I imagine the program should run if the input is e.g. 5:

  1. The program prints: Enter the number of times to print "Yay", to the console.
  2. The program allows the user to enter a number and stores it as an integer in the variable timesToLoop
  3. The program initializes the variable counter by setting it to 0.
  4. Counter is currently set to zero. 5 is greater than or equal to 0, therefore the loop begins.
  5. The program writes "Yay!" on a new line once and increases counter by 1.
  6. Repeat the process until counter is equal to 5
  7. Counter is now 5 and 5 is greater than or equal to 5, therefore the loop exits and runs the break statement.
  8. The program exits.

Instead the result I get is: "The name `timesToLoop' does not exist in the current context"

I'm hoping someone can help me out here.

Program.cs
using System;

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

            int counter = 0;

            while(counter <= timesToLoop)
            {
                Console.WriteLine("Yay!");
                counter += 1;
            }
            break;
        }
    }
}

1 Answer

andren
andren
28,558 Points

There are three errors in your code two of them being code errors and one of them being a "logic" error.

First problem: You forgot to preface timesToLoop with int, which as far as the compiler is concerned means that you are trying to assign a value to a variable that does not exist, rather than initializing it like you are obviously meaning to do.

Second problem: The "break;" statement in your code which is used to abort loops is placed outside the loop and is also unnecessary, break; is used to abort the current loop before the looping conditions have been fulfilled, which is not what you want in your loop.

Third problem, your loop will actually output "Yay!" six times, not five. Due to the fact that the counter starts at 0 not at 1, when you loop through code with a 0 based counter you have to check that the counter is lesser than the desired loop count, not lesser than or equal to the counter.

Morten Hauge
Morten Hauge
9,240 Points

Thank you so much!

Such obvious mistakes when you point them out that way, I've got a lot to learn.