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();


      while (steps != number) {

      // Two steps forward

      steps += 2;

      // One step back

      steps --;

      // Print result to the console






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.

1 Like

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.