how to stop run of a program after a message display [modified]
-
Thanks. I use exit(-1); after the message box. it works. if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); exit(-1); }
mrby123 wrote:
I use exit(-1)...
To exit a Windows GUI application? :confused: Not a good choice.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
-
mrby123 wrote:
Please select a larger sampling interval.
That message does not make sense if your app is exiting anyway. :(
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
Well, maybe you should just "return" from the current flow of execution and let the user enter a new interval instead of just closing the application!! something like :
while (sample not good )
{
enter sampling.
compute value.
validate value.
}M.
Watched code never compiles.
-
When you program suddenly stop, you may want to inform the use what happen. Otherwise, the use feels not good.
I agree, but then you should choose a more accurate message; maybe "next time you run this program, please enter ...". Although I still think it is rather rude to stop a program just because the interactive user entered something you did not like; you should allow for another chance. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
I agree, but then you should choose a more accurate message; maybe "next time you run this program, please enter ...". Although I still think it is rather rude to stop a program just because the interactive user entered something you did not like; you should allow for another chance. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
I agree with you, it is better to give user a chance to correct. I wish to find an interactive message box to allow user to inter a correct value for the parameter. Do anyone know how to program an interactive dialogue box? Thanks again
Assuming you're using MFC...
If (IDYES == MessageBox(_T("Are you sure you want to quit"), _T("Confirm Exit"), MB_YESNO))
{
PostQuitMessage(0);
}«_Superman_» I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++)
Polymorphism in C -
I agree with you, it is better to give user a chance to correct. I wish to find an interactive message box to allow user to inter a correct value for the parameter. Do anyone know how to program an interactive dialogue box? Thanks again
you may want to implement this pseudo-code:
string s="Please enter a number in the range [a,b]"
for(;;) {
int n=NumericInputBox.Show(s);
if (n==-1) Application.Exit();
if (n>=a && n<=b) break;
s="Sorry, "+n+" is not valid as it is not in the range [a,b]; please try again now (or enter -1 to quit)";
}:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
you may want to implement this pseudo-code:
string s="Please enter a number in the range [a,b]"
for(;;) {
int n=NumericInputBox.Show(s);
if (n==-1) Application.Exit();
if (n>=a && n<=b) break;
s="Sorry, "+n+" is not valid as it is not in the range [a,b]; please try again now (or enter -1 to quit)";
}:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read formatted code with indentation, so please use PRE tags for code snippets.
I'm not participating in frackin' Q&A, so if you want my opinion, ask away in a real forum (or on my profile page).
-
I want to stop the program run after the message box display int npoints=(tmax+1000)/sam_interval; if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); } With the above statement, after I close the message box, the program resumes the run. In fact, I want to stop the program after closing the message box. Please give me help Thanks
modified on Wednesday, May 19, 2010 1:16 PM
Hi, As you're using an MFC app look up Dialog Data Exchange (DDX) and Dialog Data Validation (DDV). Between the pair of them you can specify allowed ranges for field in a dialog box and give the user a message if they enter something that's out of the range. Out of the box it's a bit clunky but works okay with a bit of tender loving care. So if you've got an edit control with the sampling interval in milliseconds (IDC_SAMPLE_INTERVAL) and you want the user to be able to enter something between 10 and 1000ms, you overide DoDataExchange in your dialogue box class as something like:
void CSampleDlg::DoDataExchange( CDataExchange *pDX )
{
CDialog::DoDataExchange( pDX );DDX\_Text( pDX, IDC\_SAMPLE\_INTERVAL, sample\_interval\_ ); DDV\_MinMaxInt( pDX, sample\_interval\_, 10, 1000 );
}
where sample_interval_ is the data member containing your er, sample interval. If the entered interval isn't between 10 and 1000 MFC displays an message box and lets the user choose to abandon the edit or try again. Cheers, Ash PS: Using exit() in a C++ program can be a bad idea one main reason - destructors aren't run for any objects. If those objects are managing a system global resource then that resource can leak. It can also mean that files aren't flushed and closed properly and may not have the contents you expect. On the other hand exit() can be a quick way to bail out of a misbehaving program with a long shutdown time if that shutdown time is due to a large number of or slow destructors. This is generally regarded as being pretty vile by most C++ experts and only used as a desperate measure.
-
I want to stop the program run after the message box display int npoints=(tmax+1000)/sam_interval; if (npoints>32768) {MessageBox("The sampling time interval is too small. Please select a larger sampling interval."); } With the above statement, after I close the message box, the program resumes the run. In fact, I want to stop the program after closing the message box. Please give me help Thanks
modified on Wednesday, May 19, 2010 1:16 PM