HelpMe
HelpMe

Reputation: 61

How to count the number of "correct" and "incorrect" responses

I'm new to coding and am creating a windows form application in C#. I was having difficulties figuring out how to track the number of correct and incorrect responses. When a button is clicked, if the response is correct a label says correct. I want a different label to count the amount of times it says correct and incorrect. Here's what I was thinking but didn't work.

int add = 0;
add = int.Parse(correct.Text);

if (label1.Text == "Correct")
{
    correct.Text = add++;
}
else
    incorrect.text = add++;

correct and incorrect are names of my label.

Here's my button click event, there are many alike.

private void G_Click(object sender, EventArgs e)
        {
            if (go.Visible == true)
            {
                go.Visible = false;
                Random rnd = new Random();
                int y = rnd.Next(1, 7);

                if (y == 1)
                {
                    eo.Visible = true;
                }
                if (y == 2)
                {
                    ao.Visible = true;
                }
                if (y == 4)
                {
                    dd.Visible = true;
                }
                if (y == 5)
                {
                    go.Visible = true;
                }
                if (y == 6)
                {
                    eeo.Visible = true;
                }

                timer1.Start();

                label1.Text = "Correct";
            }
            else
             {
                label1.Text = "Incorrect";
             }

            private int correctCount = 0;
            private int incorrectCount = 0;


             if (label1.Text = "Correct";)
                {
                     correctCount++;
                     correct.Text = correctCount.ToString();
                }
    else
                {
                      incorrectCount++;
                      incorrect.Text = incorrectCount.ToString();
                }

Upvotes: 0

Views: 2368

Answers (2)

Masoud Mohammadi
Masoud Mohammadi

Reputation: 1739

class fields = variables that are declared outside of a method and directly inside a class
local variables = variables that are declared inside of a method

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace deleteMe
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int correctCount = 0; // field

        private int incorrectCount = 0; // field

        private void G_Click(object sender, EventArgs e)
        {
            if (go.Visible == true)
            {
                go.Visible = false;
                Random rnd = new Random();
                int y = rnd.Next(1, 7); // local variable

                if (y == 1)
                {
                    eo.Visible = true;
                }
                if (y == 2)
                {
                    ao.Visible = true;
                }
                if (y == 4)
                {
                    dd.Visible = true;
                }
                if (y == 5)
                {
                    go.Visible = true;
                }
                if (y == 6)
                {
                    eeo.Visible = true;
                }

                timer1.Start();

                incorrect.Text = "Correct";
            }
            else
            {
                incorrect.Text = "Incorrect";
            }


            if (label1.Text = "Correct")
            {
                correctCount++;
                correct.Text = correctCount.ToString();
            }
            else
            {
                incorrectCount++;
                incorrect.Text = incorrectCount.ToString();
            }
        }
    }
}

== equality. (for comparing 2 values)
= assign a value. (for initialize a variable or field)

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500055

The immediate problem is that the type of the Text property is string, not int - you have to set it to text, not a number. Fortunately, you can just call ToString on an int to get a string representation. However, I'd suggest there are other things to change too.

I would strongly suggest that you keep separate counters as variables. While you could keep parsing your labels, it's simpler for them to be purely output. So you might have:

// Fields
private int correctCount = 0;
private int incorrectCount = 0;
...
// Wherever your code is
if (correctAnswer)
{
    correctCount++;
    correct.Text = correctCount.ToString();
}
else
{
    incorrectCount++;
    incorrect.Text = incorrectCount.ToString();
}

Note the correctAnswer condition here - we don't know what's setting the text of label1, but I'd suggest that that's the right place to also change the correct/incorrect counters, from the same condition. Again, treat label1 just as output, rather than as a feedback system.

Upvotes: 5

Related Questions