VC++ Winforms reading input from textbox and converting to int

Pages: 12
Hey, basically I'm trying to read a user's input from a textbox and define that input as an integer. The problem I'm having is I get this error when I run the code: http://gyazo.com/ec464cd6ab07d090fc2a7650a1670021

Here is the code I am using:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
private: System::Void btnStart_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->num1->Visible = true;
				 this->num2->Visible = true;
				 this->add->Visible = true;
				 this->equals->Visible = true;
				 this->textBox1->Visible = true;
				 this->btnRestart->Visible = true;
				 this->labScore->Visible = true;
				 this->numScore->Visible = true;
				 this->btnStart->Text = "Next";

				 int firstNum;
				 int secondNum;
				 int answer;
				 int correct;
				 int score;

				 answer = System::Convert::ToInt32(textBox1->Text); // THIS IS THE LINE PRODUCING THE ERROR
				 firstNum = rand()%10+1;
				 secondNum = rand()%10+1;
				 correct = firstNum+secondNum;
					
				 this->textBox1->Clear();
				 this->num1->Text = System::Convert::ToString(firstNum);
				 this->num2->Text = System::Convert::ToString(secondNum);

				 if (answer == correct) {
					 score++;
				 }

				 this->numScore->Text = System::Convert::ToString(score);

		 }
Try this:

 
Int32::TryParse(textBox1->Text,answer);
That worked in removing the error, but now I have another problem, when I answer the question correctly (correct being defined by firstNum+secondNum) the score isn't incremented so the if statement isn't executed.
Updated Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
private: System::Void btnStart_Click(System::Object^  sender, System::EventArgs^  e) {
				 this->num1->Visible = true;
				 this->num2->Visible = true;
				 this->add->Visible = true;
				 this->equals->Visible = true;
				 this->textBox1->Visible = true;
				 this->btnRestart->Visible = true;
				 this->labScore->Visible = true;
				 this->numScore->Visible = true;
				 this->btnStart->Text = "Next";

				 int firstNum;
				 int secondNum;
				 int answer;
				 int correct
				 int score;

				 answer = Int32::TryParse(textBox1->Text,answer); // Editted to remove Error.
				 firstNum = rand()%10+1;
				 secondNum = rand()%10+1;
				 correct = firstNum+secondNum; // Being defined
					
				 this->textBox1->Clear();
				 this->num1->Text = System::Convert::ToString(firstNum);
				 this->num2->Text = System::Convert::ToString(secondNum);

				 if (answer == correct) { // Checking if the user input is the same as correct)
					 score++;
				 }

				 this->numScore->Text = System::Convert::ToString(score);

		 }
Line 15 correct is missing the ;
Fixed it, still no improvement I'm afraid.
What error are you getting?
Also, with the new code I provided, answer = on line 18 shouldn't be needed, try taking that off. Not sure what will happen.
Last edited on
No error, the code just skips the if statement even though I'm meeting it's requirements. Here is the exact same concept in console c++. It works perfectly:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main() {
    int answer;
    int correct;
    int incorrect;
    int num1;
    int num2;

    num1 = rand()%10 + 1;
	num2 = rand()%10 + 1;
    correct = num1+num2;

    cout << "What is " << num1 << "+ " << num2 << endl;
    cin >> answer;

    if (answer == correct) {
            cout << "Correct" << endl;
    }

    return 0;
}



My problem in winforms is that even though answer IS EQUAL to correct. It's just not completing the contents of the if block.
I think it is not converting answer correctly, take "answer =" out of line 18, that is what the "answer" at the end of the line is for.
Tried that also, still didn't fix it. However if I change Line 21 to: Correct = 1; And then input 1 to the textbox it increments the score and does what it's supposed to. So I believe the problem lies in line 21. Any ideas?
Can you PM the project in a ZIP file to me, first I am interested in playing the game, I have trouble with quick math, second, it will make it easier for me to search for problems.
Ahhh, move the contents of line 18 under the contents of line 20:

1
2
3
firstNum = rand()%10+1;
secondNum = rand()%10+1;
answer = Int32::TryParse(textBox1->Text,answer); // Editted to remove Error. 
Done
Did it work?
Afraid not
Any other ideas? I'm stumped here
I found the problem using step-debugging, searching for a solution now.
Got, the solution was not simple, I will repack it and send it back, examine it closely, let me know in a PM if you want to know how something works!
Also, I downgraded it to 2010, I haven't taken the time to install '12 yet, should be a simple upgrade.
Afraid I'm using 2005, :/ any chance you could downgrade it that far?
Pages: 12