user770022
user770022

Reputation: 2959

Increment the number by 1

I would like to see label6 display the number of correct times the user chooses the number. And label7 display the number of times the user choose incorrectly. Its not incrementing by one.

Error 1 Operator '++' cannot be applied to operand of type 'string' Error 2 Operator '++' cannot be applied to operand of type 'string'

private void button1_Click(object sender, EventArgs e)
    {
        string correct="0";
        string incorrect="0";
        RandomNumber(0,99);
        button2.Enabled = true ;
        button1.Enabled = false;
        label3.Visible = true;
        if (textBox1.Text == label1.Text)
            label3.Text=("Winner");
               label6.Text = correct +1;
               if (textBox1.Text != label1.Text)
                   label7.Text = incorrect= +1;
            label3.Text=(string.Format("Sorry - You Lose, The number is {0}",   label1.Text));

    }


Edit (From OP's answer to his own question):

I have tried the ways your suggesting but the number doesnt increase by one everytime I guess wrong.

private void button1_Click(object sender, EventArgs e)


    {
        int correct=0;
        int incorrect=0;
        RandomNumber(0,99);
        button2.Enabled = true ;
        button1.Enabled = false;
        label3.Visible = true;
        if (textBox1.Text == label1.Text)
        {
            label3.Text = ("Winner");
            label6.Text = (++correct).ToString(); 
        }

        else if (textBox1.Text != label1.Text)
        {
            label7.Text = (incorrect+1).ToString(); 

            label3.Text = (string.Format("Sorry - You Lose, The number is {0}", label1.Text));
        }


    }

Upvotes: 3

Views: 6957

Answers (5)

Gabe
Gabe

Reputation: 50483

It doesn't look like you're persisting the correct and incorrect

Create Properties:

public int Correct { get; set; }
public int Incorrect { get; set;}

Then:

private void button1_Click(object sender, EventArgs e)
{
   RandomNumber(0,99);
   button2.Enabled = true ;
   button1.Enabled = false;
   label3.Visible = true;

   if (textBox1.Text == label1.Text)
   {
     label3.Text=("Winner");
     label6.Text = (++this.Correct).ToString();
   }
   else
   {  
      label3.Text=(string.Format("Sorry - You Lose, The number is {0}", label1.Text));
      label7.Text = (++this.Incorrect).ToString();
   } 
}

Upvotes: 10

Alan Geleynse
Alan Geleynse

Reputation: 25139

You are storing your correct and incorrect variables as string.

Use int instead like this:

int correct = 0;
int incorrect = 0;

and change your code to:

correct++;
label6.Text = correct.ToString();

and:

incorrect++;
label7.Text = incorrect.ToString();

Upvotes: 8

Jeff Mercado
Jeff Mercado

Reputation: 134811

Adding to strings, correct and incorrect will just append the string representation of what's added. You have to convert it to an integer type, then increment, then convert back to string. However it would be easier to just keep these variables as integer instance variables. That way incrementing is trivial and you are actually keeping the correct count and not resetting every time the button gets clicked. (There are actually a number of problems with the code)

// instance variables
private int correct = 0;
private int incorrect = 0;

private void button1_Click(object sender, EventArgs e)
{
    RandomNumber(0,99);
    button2.Enabled = true ;
    button1.Enabled = false;
    label3.Visible = true;
    if (textBox1.Text == label1.Text)
    {
        label3.Text=("Winner");
        label6.Text = (++correct).ToString(); // convert int to string
    }
    // indentation does not indicate the block
    else //if (textBox1.Text != label1.Text)
    {
        label3.Text=(string.Format("Sorry - You Lose, The number is {0}",   label1.Text));
        label7.Text = (++incorrect).ToString();
    }
}

Upvotes: 4

Mark Avenius
Mark Avenius

Reputation: 13947

label6.Text = correct +1;

only sets label6's value to correct + 1; it does not change the value of correct. If you use:

int correct;
label6.Text = (++correct).ToString();

you should see what you want.

Upvotes: 0

zamN
zamN

Reputation: 236

Or you can use:

correct = correct + 1; ( I think this is what you were trying to achieve) incorrect = incorrect + 1;

OR

correct += 1; incorect += 1;

Upvotes: 1

Related Questions