title: counter for attempts in c#
body: <p>I am trying to get the counter named "Guesses" to keep a tally of attempts at guessing a random number and output the total attempts at guessing the number. I have tried leaving the counter declaration at 0 and 1 and the number of attempts to guess is always 0 or 1. Help would be appreciated and I will re-post entire working code once it's figured out. Here is my code.</p>
  
  <pre><code>    int Answer; // declares the Answer variable outside button event
      public frmGuess()
      {  // generates random number outside button event so does not change on button click
          InitializeComponent();
          Random rand = new Random(); 
          Answer = rand.Next(100) + 1; // makes it range 1 to 100
      }
      private void btnGuess_Click(object sender, EventArgs e)
      {
          int UserGuess;
          int Guesses = 0;     // start counter
          if (string.IsNullOrEmpty(txtGuess.Text)) // input validation check to make sure not blank and is a whole number integer
          {
              MessageBox.Show("Please enter a whole number between 1 and 100");
              return;
          }
          else
          {
              UserGuess = int.Parse(txtGuess.Text);  // variable assign and code run
              Guesses ++;
              if (UserGuess &gt; Answer)
              {
                  txtGuess.Text = "";
                  lblAnswer.Text = "Too high, try again.";
              }
              else if (UserGuess &lt; Answer)
              {
                  txtGuess.Text = "";
                  lblAnswer.Text = "Too low, try again.";
              }
              else
              {
                  lblAnswer.Text = "Congratulations the answer was " + Answer + "!\nYou guessed the number in " + Guesses + " tries.\nTo play again click the clear button."; //victory statement
              }//end if
          } //end if
      } 
      private void btnClear_Click(object sender, EventArgs e)  // clears Answer label and Guess textbox
      {
          txtGuess.Text = "";
          lblAnswer.Text = "";
      }
  
      private void btnExit_Click(object sender, EventArgs e) // closes window
      {
          this.Close();
      }
  }
  </code></pre>
  
  <p>}`</p>