Visual C++ Windows form user input error.

I am fairly new to Visual c++ programming, below is my code to a project I am working on. I am having issues with the code I wrote below. Question 1 works well and the user can input the answer and the second question comes up. However when I get to Question 2 when the user inputs the correct answer which is "c" it will say the user has lost. However when they input "a" nothing happens (which was the same answer to the first question).

Is there anything wrong with the variable or the way I structured it? Thanks in advance.



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
34
35
36
37
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
			 {
				 
				 label1->Text = "Question 1: Which of the following arithmetic operations are allowed in an expression?";
				 label2->Text = "A.";
				 label3->Text = "B.";
				 label4->Text = "C.";
				 label5->Text = "D.";
				 String ^ test = textBox1->Text;
				 if (System::String::IsNullOrEmpty(test))
				 label7->Text = "You are currently on £0!";
				 else if (test == "a" || test == "A")
					{
				     
					
					 textBox1->Text = "";
					 label7->Text = "Well done, you are currently on £500!";
					 label1->Text = "Question 2: The company that popularized computers was?";
					 label2->Text = "A.";
					 label3->Text = "B.";
					 label4->Text = "C.";
					 label5->Text = "D.";
				     if (test == "c" || test == "C")
					 
					 {
						 label7->Text = "£1000 has been secured! Keep playing to increase this amount";
					 }
					 
					 
				 }
				 else
				 {
					 label7->Text = "You lose.";	 
				 }


You need to make test = textbox1->Text again around line 22; you'll also need to allow the user to have time to enter data again, from what I can see of this, it looks like everything is done as soon as the button is clicked. When the user clicks the button again it will just check for "a" everytime?
First thing, this looks like C++/CLI:
http://en.wikipedia.org/wiki/C%2B%2B/CLI
are you aware of this?

I wouldn't class this as 'visual C++' as such.
Last edited on
closed account (z05DSL3A)
Is there anything wrong with the variable or the way I structured it?
Yes. This code is being called in response to the button click. Each time the button is clicked this code is called and you start at the top.
This code is being called in response to the button click. Each time the button is clicked this code is called and you start at the top.


and why is this wrong?
closed account (z05DSL3A)
and why is this wrong?
Read the code, follow the path
Yes. This code is being called in response to the button click. Each time the button is clicked this code is called and you start at the top.


I may be wrong here but, the button is only clicked once to start the game. The user presses enter on the keyboard to input the answer.

However that being said the enter feature is the same as clicking the button
1
2
3
4
private: System::Void textBox1_KeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e) {
				 if(e->KeyCode == Keys::Enter)
					 button1->PerformClick();
			 } 


Is this my issue?
Last edited on
closed account (z05DSL3A)
Ibzz, are you writing this from scratch or modifying some already written code?

Unless you have written a handler for the user pressing the enter key, then it will not be that. If there is code that handles the enter key being pressed then that is where you need to be looking.
I did it from scratch, but I'm fairly new to programming a GUI for a c++ program. This is for a college project set in preparation for university. People have been saying use QT for a GUI but on the college computers we only have Visual Studio.
closed account (z05DSL3A)
Opps, I need to refresh more....

However that being said the enter feature is the same as clicking the button...Is this my issue?
Yes, it will reset the question and check if the answer is 'a'.
Can someone point me in the right direction for a solution.
A simple solution would be to create an integer value for the question then use a switch statement to ask the question. Maybe something like this:
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
34
35
36
37
38
39
40
unsigned int question = 1;

void buttonClick()
{
	switch(question)
	{
	case 1:
		{
			printf("Question 1: Which of the following arithmetic operations are allowed in an expression?\nA.\nB.\nC.\nD.\n");
			char answer;
			scanf("%c", &answer);
		
			if(answer == 'c' || answer == 'C')
			{
				printf("Correct!\n");
				++question;
			}
			else
			{
				printf("Incorrect, try again!\n");
			}
		}
	case 2:
		{
			printf("Question 2: The company that popularized computers was?\nA.\nB.\nC.\nD.\n");
			char answer;
			scanf("%c", &answer);
		
			if(answer == 'a' || answer == 'A')
			{
				printf("Correct!\n");
				++question;
			}
			else
			{
				printf("Incorrect, try again!\n");
			}
		}
	}
}


Of course this is using a different function and console input/output but I hope you get the idea.
Topic archived. No new replies allowed.