469,964 Members | 1,540 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,964 developers. It's quick & easy.

While Loop Trouble, Visual C++

I believe my problem lies inside the while loop. When I click the play button on the gui app, it goes inside the while loop, reads the file and calls the necessary function to do what it needs to do. I have it set in a way whenever I click the PLAY button, the PLAY button is suppose to hide and the PAUSE button is suppose to appear. This does not happen until the complete while iteration has finished. My goal is to Play and Pause at any time I want, basically Play and Pause toggle as soon as I click one or the other, one of them is available at a time.
Expand|Select|Wrap|Line Numbers
  1.  
  2. private: System::Void keystrokes(System::Object *  sender, System::EventArgs *  e)//, char *array)
  3.          {//actual key presses depending on the characters read
  4.  
  5.             char array[255];
  6.  
  7.             ifstream in ("c:\\write.rtf"); //input
  8.  
  9.             //in.open("c:\\write.rtf");
  10.  
  11.             if(!in)
  12.             {
  13.                 richTextBox1->AppendText("\nCannot open input file.\n");
  14.             }
  15.             else
  16.             {
  17.                 while(in)// && keystrokeFlag == true)
  18.                 {
  19.  
  20.                     //keystrokeFlag = false;
  21.                     in.getline(array, 6); //delim defaults to '\n'
  22.                 //    wait(sender, e, 1);
  23.                     pushKeystrokes(sender, e, array);
  24.                     checkStatus();
  25.  
  26.                     if(keystrokeFlag == false)
  27.                     {
  28.                         break;
  29.                         //keystrokeFlag = false;
  30.                     }
  31.                     else
  32.                         continue;
  33.  
  34.                     //need some sort of if button clicked, break out of while loop (or change the while loop condition)
  35.                 //    wait(sender, e, 1);
  36.  
  37.                     if(in.eof())
  38.                     {
  39.                         in.clear(); //clear the flags (in.seekg() will not work properly without this function call)
  40.                         in.seekg(0, ios::beg); //infinite loop work on this tomorrow
  41.                     }                            
  42.                 }
  43.             }
  44.             in.close(); //close document
  45.          }
  46.  
  47. private: System::Void checkStatus()
  48.          { 
  49.             if(play->Visible == true)
  50.             {
  51.                 richTextBox1->AppendText("\n\nKEYSTROKE SET TO FALSE\n\n\n");
  52.                 keystrokeFlag = false;
  53.             }
  54.             if(play->Visible == false)
  55.             {
  56.                 richTextBox1->AppendText("\n\nKEYSTROKE SET TO TRUE\n\n\n");
  57.                 keystrokeFlag = true;
  58.             }
  59.          }
  60.  
  61. private: System::Void play_Click(System::Object *  sender, System::EventArgs *  e)
  62.          {
  63.             pause->Visible = true;
  64.             play->Visible = false; 
  65.  
  66.             //keystrokeFlag = true;
  67.             keystrokes(sender, e);
  68.          }
  69.  
  70. private: System::Void pause_Click(System::Object *  sender, System::EventArgs *  e)
  71.          {
  72.              play->Visible = true;
  73.              pause->Visible = false;
  74.              //keystrokeFlag = false;
  75.              keystrokes(sender, e);
  76.          }
  77.  
  78.  
Sep 8 '07 #1
3 2311
Agrona
5
I may be misunderstanding something, but it appears that you never call the play_Click() and pause_Click() functions. Is it just a simple oversight? (You may do this in pushKeyStrokes() function, but that isn't listed here.)

The changes to your code are old lines 23, 46-58 to new lines 23, 46-60

Expand|Select|Wrap|Line Numbers
  1.  
  2. private: System::Void keystrokes(System::Object *  sender, System::EventArgs *  e)//, char *array)
  3.          {//actual key presses depending on the characters read
  4.  
  5.             char array[255];
  6.  
  7.             ifstream in ("c:\\write.rtf"); //input
  8.  
  9.             //in.open("c:\\write.rtf");
  10.  
  11.             if(!in)
  12.             {
  13.                 richTextBox1->AppendText("\nCannot open input file.\n");
  14.             }
  15.             else
  16.             {
  17.                 while(in)// && keystrokeFlag == true)
  18.                 {
  19.  
  20.                     //keystrokeFlag = false;
  21.                     in.getline(array, 6); //delim defaults to '\n'
  22.                 //    wait(sender, e, 1);
  23.                     pushKeystrokes(sender, e, array);
  24.                     checkStatus(sender, e);
  25.  
  26.                     if(keystrokeFlag == false)
  27.                     {
  28.                         break;
  29.                         //keystrokeFlag = false;
  30.                     }
  31.                     else
  32.                         continue;
  33.  
  34.                     //need some sort of if button clicked, break out of while loop (or change the while loop condition)
  35.                 //    wait(sender, e, 1);
  36.  
  37.                     if(in.eof())
  38.                     {
  39.                         in.clear(); //clear the flags (in.seekg() will not work properly without this function call)
  40.                         in.seekg(0, ios::beg); //infinite loop work on this tomorrow
  41.                     }                            
  42.                 }
  43.             }
  44.             in.close(); //close document
  45.          }
  46.  
  47. private: System::Void checkStatus(System::Object * sender, System::EventArgs * e)
  48.          { 
  49.             keystrokeFlag = !(play->Visible);
  50.  
  51.             if(play->Visible == true)
  52.             {
  53.                 richTextBox1->AppendText("\n\nKEYSTROKE SET TO FALSE\n\n\n");
  54.                 play_Click(sender, e)
  55.             }
  56.             else
  57.             {
  58.                 richTextBox1->AppendText("\n\nKEYSTROKE SET TO TRUE\n\n\n");
  59.                 pause_Click(sender, e)
  60.             }
  61.          }
  62.  
  63. private: System::Void play_Click(System::Object *  sender, System::EventArgs *  e)
  64.          {
  65.             pause->Visible = true;
  66.             play->Visible = false; 
  67.  
  68.             //keystrokeFlag = true;
  69.             keystrokes(sender, e);
  70.          }
  71.  
  72. private: System::Void pause_Click(System::Object *  sender, System::EventArgs *  e)
  73.          {
  74.              play->Visible = true;
  75.              pause->Visible = false;
  76.              //keystrokeFlag = false;
  77.              keystrokes(sender, e);
  78.          }
  79.  
  80.  
Sep 8 '07 #2
Thanks for the response Agrona, I will try the checkstatus(sender, e), that slipped my mind. The pushKeystrokes function only pushes keystrokes, it's only a function that pushes the buttons from the text file results. That works fine, that's why I didn't post it. play_Click and pause_Click functions are actually buttons on the gui app. When you click the PLAY button, it calls the play_Click(System::Object * sender, System::EventArgs * e) function and when you click the PAUSE button, that calls the pause_Click(System::Object * sender, System::EventArgs * e) function. I'm sorry I wasn't clear on that. I have it designed where it starts from that then does the rest. The wierd thing is, why it finishes the while loop then it displays the pause button. You can see in my code, I make pause->Visible and I hide play as soon as the PLAY button is clicked. Then it calls the function, it just seems like the play button is lagging out until the while loop is done. If anything is pushed while it's in the while loop, the program will crash. I'm gonna try your suggestion on Monday when I get back to work. I'll post a comment on Monday again. I do appreciate the quick response. Thanks again.
Sep 8 '07 #3
I tried the suggestions and I'm still having the same problem. I think I might need a pause or some sort of clear memory function so that when it goes in the while loop it won't freeze up everything else. I need an alternative to

system("pause")

that's for the dos version of C++. I need a GUI Form that will work with .NET Framework.

Other than that I'm open to any suggestions, I still can't understand why it won't display the PAUSE before it calls the function to go into the while loop.
Sep 10 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Andreas Paasch | last post: by
14 posts views Thread by jsaul | last post: by
17 posts views Thread by Pushkar Pradhan | last post: by
2 posts views Thread by apec | last post: by
36 posts views Thread by Scott | last post: by
6 posts views Thread by TrevS17 | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.