Hello,
Please disregard any small syntax errors I have made in this question, I’m trying to simplify a program that is many pages long.
I have a program that has 2 parts, main.cpp and a form GUI.h. Both have functions that pass variables between themselves and I don't want to combine the two due to other aspects of their functionality. The form is loaded from running the main program using the Application::Run command.
The idea of the form is that it is constantly open (viewable) and updates the values that it shows based on either: a) algorithm run in main.cpp; or b) input from the user (via buttons on the form itself).
The program takes turns to accept decisions from the computer and user. I want to find a way to get the program execution to pause and wait for the user to make their decision (via one of 2 buttons) then continue when the decision is made (one of the buttons is pressed). I have added a simplified version of the code below, which adds or subtracts 1 from a running total:
GUI.h
namespace NGUI
{
// <namespace declarations>
public ref class GUI : public System::Windows::Forms::Form
{
int running_count; bool computer_turn; bool user_turn; bool still_user_turn;
public:
GUI(void)
{
InitializeComponent(); // usual form code detailing form construction
running_count = 10;
computer_turn = true;
user_turn = false;
for (int y = 0, y < 5, y ++) // repeat procedure a few times
{
if (computer_turn == true)
{
// <execute one of the functions (plus or minus 1 from running total) based on an main.cpp based algorithm>
}
else if (user_turn == true)
{
while (still_user_turn == true)
{
// <wait until button is pressed and action related function functions (plus or minus 1 from running total), then continue program>
}
}
if (computer_turn == true) {computer_turn = false; user_turn = true; }
if (user_turn == true) {user_turn = false; computer_turn = true;}
// <update form label with new values >
}
}
public:
int function_plus_1(int x)
{
x = x + 1;
return x;
}
public:
int function_minus_1(int x)
{
x = x - 1;
return x;
}
// <code>
// <Windows Form Designer generated code>
}
// Button click events:
private: System::Void button1_Click(System::Object^sender, System::EventArgs^e)
{
running_count = function_plus_1(running_count);
still_user_turn = false;
}
private: System::Void button2_Click(System::Object^sender, System::EventArgs^e) {
{
running_count = function_minus_1(running_count);
still_user_turn = false;
}
};
}
main.cpp:
// <code>
int _tmain(int argc, _TCHAR* argv[])
{
// <code>
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Application::Run(gcnew GUI()); // load up form
return 0;
}
I assume you use some combination of while / break statements to pause and continue the program when the button is pressed but I cannot seem to get it to work. I am currently trying to use a while statement, as shown above, with the still_user_turn boolean variable but the buttons seem inoperable when the form loads.
Any help would be greatly appreciated.