Why does this not work?

using System;

namespace MakingProgress

{

  class Program

  {

    static void Main(string[] args)

     {

      // declare steps variable

      int steps = 0;

      number = Console.ReadLine();

      Console.WriteLine(steps);

      while (steps != number) {

      // Two steps forward

      steps += 2;

      // One step back

      steps --;

      // Print result to the console

      Console.WriteLine(steps);

      }

    }

  }

}

And instead of working, this gives me an error message:
Program.cs(11,7): error CS0103: The name ‘number’ does not exist in the current context [/home/ccuser/workspace/csharp-working-with-numbers-operator-shortcuts-csharp/e4-workspace.csproj]
Program.cs(13,23): error CS0103: The name ‘number’ does not exist in the current context [/home/ccuser/workspace/csharp-working-with-numbers-operator-shortcuts-csharp/e4-workspace.csproj]

The build failed. Fix the build errors and run again.

I am not a C# programmer but from a tiny bit of Java knowledge (and a quick google), I think number should be assigned as either an int or a double (or one of various other types). Maybe try that?

Mainly replying without any real expertise because you’re fairly unlikely to get a good reply with such a vague title; try to use titles for posts asking for help that are connected to your problem, you’ll get a better response generally.

2 Likes

This is it. You’ve tried to assign a value to number but you haven’t declared it. You either need to put var (compiler will figure out the type from the context) or string before number as ReadLine() will return a string. This will declare and initialise number.

You will need to then try and convert the string to an int. There are a number of methods to do this, Int.Parse, Int.TryParse and Convert.ToInt32 they all work slightly differently, but they all take a string and try turn it into a 32 bit signed integer.

1 Like
using System;

namespace MakingProgress

{

    class Program

    {

        static void Main(string[] args)

        {

            // declare steps variable

            int steps = 0;
            // need to declare number as a variable
            int number;

            //User input will be a string so you need to convet this to a number
            number = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(steps);

            while (steps != number)
            {

                // Two steps forward

                steps += 2;

                // One step back

                steps--;

                // Print result to the console

                Console.WriteLine(steps);

            }

        }

    }

}

I’ve corrected your code above, you had the following problems.
You didnt declare number as a variable nor state its type.
When a user types something to a console it is considered a string, you cannot do equations with a string.
Therefore you must convert it to a number first. You can do this with the Convert.ToInt32 method
The above code I’ve corrected will work. But you might want to consider writing some additional code to handle the errors when a user types letters instead of numbers to the console.

Hope this helps
I’m a newb be kind, if this has helped click the like button
P.S Deleted messages where because I couldn’t figure out how to paste code example lol

1 Like