0

I'm trying to compare and multiply 2 random number variables with the int value entered in textboxes. If it is the correct increment the correct answers it does not increase the number although it increment works alone but it does not work with the textbox.

private void button1_Click(object sender, EventArgs e)
{
    int x = Randomnumber.Next(12);
    int z = Randomnumber.Next(12);

    //int cv = +correct;
    textBox2.Text = x.ToString();
    textBox3.Text = z.ToString();

    int s = x * z;
    if (s == int.Parse(textBox4.Text))
    {
        correct++;
        numbercorrect.Text = correct.ToString();
    }
}
8
  • i don't get what the question is Commented Nov 6, 2012 at 1:08
  • i can't identify any apparent problems at the moment. what is it supposed to do? Commented Nov 6, 2012 at 1:12
  • 1
    I dont really understand what the problem is. You should tell us what is happening with your current code, what you're expecting, and how they differ. Commented Nov 6, 2012 at 1:13
  • i try to make mathmatical game to answer the multipiling of random numbers and compare the answer in the text box with the correct answer and if it is true increment the value of correct answer textbox by 1 for every correct answer Commented Nov 6, 2012 at 1:16
  • My guess is that int.Parse is breaking because the user hasn't typed an int. Use int.TryParse. Commented Nov 6, 2012 at 1:22

4 Answers 4

2

EDIT This is assuming that you are trying to have the user enter their guess before the button is pressed. Figured I would put this disclaimer here since there is confusion exactly what you are trying to do.

Looking at your current code sample, you are trying parse textBox4.Text, however, you are not setting textBox4.Text anywhere in your code sample. If textBox4.Text is string.Empty, int.Parse will throw an exception.

You should also look into doing Int.TryParse as it will tell you if it worked without throwing an exception.

EDIT: Since this is a guessing game, you should be validating the user's entry in textBox4 before continuing.

private void button1_Click(object sender, EventArgs e)
{
    int answer;
    if(!int.TryParse(textBox4.Text, out answer))
    {
        MessageBox.Show("Please Enter A Valid Integer.");
        return;
    }
    int x = Randomnumber.Next(12);
    int z = Randomnumber.Next(12);

    //int cv = +correct;
    textBox2.Text = x.ToString();
    textBox3.Text = z.ToString();

    int s = x * z;
    if (s == answer)
    {
        correct++;
        numbercorrect.Text = correct.ToString();
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Nice catch on the notice that TB4 isnt getting set.
@anorton Correct. I assumed the same thing. I merely mentioned it because he didn't show an example of it with a valid answer and that he can't blindly trust the value is a valid int.
0

You're comparing the textbox value to the product of two random values. Unless you know what those two random numbers are before you push the button, the if will fail.

Comments

0

This subroutine will be run as soon as Button1 is pressed. This will display two random numbers for the user to multiply. (Displayed in TB2 and TB3.)

Now, as soon as these numbers are displayed (and before the user has a chance to enter any answer) the program checks the value in TB4. This is empty, and throws an error when the parse is attempted.

Try breaking this into 2 subroutines with 2 buttons: one button to display a new problem, and one button to check the answer.

EDIT: Code added. (Note: I wrote this freehand--don't know if it would compile or not... just get the general idea. Note the button names.)

//This routine sets up the problem for the user.
private void btnGenerateProblem_Click(object sender, EventArgs e) { 
  //Get 2 random factors
  int x = Randomnumber.Next(12);
  int z = Randomnumber.Next(12);

  //Display the two factors for the user
  textBox2.Text = x.ToString(); 
  textBox3.Text = z.ToString(); 
}

//This routine checks the user's answer, and updates the "correct count"
private void btnCheckAnswer_Click(object sender, EventArgs e) { 
  //Get the random numbers out of the text boxes to check the answer
  int x = int.Parse(textBox2.Text);
  int z = int.Parse(textBox3.Text);

  //Compute the true product
  int s = x * z; 

  //Does the true product match the user entered product?
  if (s == int.Parse(textBox4.Text))  {
    correct++; 
    numbercorrect.Text = correct.ToString(); 
  }
}

Add verification code at the beginning of btnCheckAnswer_Click.

5 Comments

I believe the answer is supposed to come first before the button is pressed. It is a guessing game. He just needs to validate that the user's entry is valid before running.
It is supposed to be a guessing game, yes. But how is the user supposed to guess between the display of the random numbers and the checking code at the end of the snippet? The random numbers aren't displayed until this code snippet is run. The user must enter the correct answer between lines 10 and 11 in the snippet above. ;)
Hmmm. I had assumed that the user guesses the value before the numbers are generated (entered their guess in textBox4) and then hit the button to see if they guessed correctly which would then display the values in the text boxes and increment the counter if correct. Perhaps I am just not understanding exactly what he is trying for.
thank you every one your comments was useful for me and helped me to get the correct code i will post it now
If a post was helpful, you can give it an up vote--this rewards those who replied. You can also "accept" an answer, which provides more reputation to that one answer. :)
0
    private void button1_Click(object sender, EventArgs e)
    {


        int result = Convert.ToInt32(textBox4.Text); int x, z;

        if (Convert.ToInt32(textBox2.Text) * Convert.ToInt32(textBox3.Text) == result)
        {
            correct++;
            numbercorrect.Text = correct.ToString();
            Randomnumber.Next(12);
            textBox2.Text = Randomnumber.Next(12).ToString();
            textBox3.Text = Randomnumber.Next(12).ToString();

        }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.