Trying to figure out a simple loop....

This is a discussion on Trying to figure out a simple loop.... within the C++ Programming forums, part of the General Programming Boards category; I am trying to set up an ap that will loop through a messagebox until the user click cancel. As ...

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    131

    Trying to figure out a simple loop....

    I am trying to set up an ap that will loop through a messagebox until the user click cancel. As of now I have
    Code:
    #include <windows.h>
    
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    	LPSTR lpCmdLine, int nShowCmd)
    {
    
    	int check = MessageBox(NULL, "Click an Option!",
    		    "MessageBox Example!!", MB_ICONQUESTION |MB_YESNOCANCEL);
    
    	while (check != IDCANCEL)
    	{
    		if (check == IDYES)
    		{
    			MessageBox(NULL, "You Hit YES!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDNO)
    		{
    			MessageBox(NULL, "You Hit NO!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDCANCEL)
    		{
    			MessageBox(NULL, "You Hit CANCEL - Goodbye!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    	}
    
        return 0;
    }
    You might notice that once the user clicks anything the ap goes into a continues loop that you can't exit. So basically in short this is what I am trying to accomplish....

    Mesasgebox pops up...
    User clicks one of three options (YES, NO, Cancel)

    If user clicks Yes then another message box pops up saying you have hit yes. User then hits ok and the loop starts again

    If user clicks NO then another message box pops up saying you have hit no. User then hits ok and the loop starts again

    If user clicks Cancel then another message box pops sayin you have hit cancel. User the hits ok and the loop exits and ap closes.

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    In your while loop you don' assign anything to check. So once the loop is entered it will never leave anymore.
    maybe this is what you want
    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    	LPSTR lpCmdLine, int nShowCmd)
    {
    
    	int check = MessageBox(NULL, "Click an Option!",
    		    "MessageBox Example!!", MB_ICONQUESTION |MB_YESNOCANCEL);
    
    	while (check != IDCANCEL)
    	{
    		if (check == IDYES)
    		{
    			MessageBox(NULL, "You Hit YES!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDNO)
    		{
    			MessageBox(NULL, "You Hit NO!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDCANCEL)
    		{
    			MessageBox(NULL, "You Hit CANCEL - Goodbye!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
                   check = MessageBox(NULL, "Click an Option!",
    		    "MessageBox Example!!", MB_ICONQUESTION |MB_YESNOCANCEL);
    	}
    
        return 0;
    }
    Kurt

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by ZuK
    In your while loop you don' assign anything to check. So once the loop is entered it will never leave anymore.
    maybe this is what you want
    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    	LPSTR lpCmdLine, int nShowCmd)
    {
    
    	int check = MessageBox(NULL, "Click an Option!",
    		    "MessageBox Example!!", MB_ICONQUESTION |MB_YESNOCANCEL);
    
    	while (check != IDCANCEL)
    	{
    		if (check == IDYES)
    		{
    			MessageBox(NULL, "You Hit YES!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDNO)
    		{
    			MessageBox(NULL, "You Hit NO!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDCANCEL)
    		{
    			MessageBox(NULL, "You Hit CANCEL - Goodbye!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
                   check = MessageBox(NULL, "Click an Option!",
    		    "MessageBox Example!!", MB_ICONQUESTION |MB_YESNOCANCEL);
    	}
    
        return 0;
    }
    Kurt
    Yeah I had just figured this out before you posted and it works so thanks......but now I need to figure out how to display a MessageBox after I hit cancel....the while loop is not letting the nested if statment run one lst time when I hit cancel...

    Maybe a do/while loop?...I am going to go try that...

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    does the user get a chance to back out of the cancel and not close the app?

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by Syneris
    does the user get a chance to back out of the cancel and not close the app?
    Nope........

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    then you could do one last MessageBox for the cancel outside the loop since you know it will be cancel from the loop?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    I think you want to use a do while:

    Code:
    do {
    
        ...
    
    } while (check != IDCANCEL);

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    The only difference in a while and a do/while is that do/while will go through the loop atleast once, as opposed to a while which will not run at all if the criteria for the loop is not met when it goes to start the loop. There is no difference between exiting a do/while and a while. I haven't learned any win32 programming, but it looks like if IDCANCEL is return from the first MessageBox the loop does not run.

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    203
    try

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    				   LPSTR lpCmdLine, int nShowCmd)
    {
    
    	int check;
    	do
    	{
    		check = MessageBox(NULL, "Click an Option!",
    			"MessageBox Example!!", MB_ICONQUESTION |MB_YESNOCANCEL);
    		if (check == IDYES)
    		{
    			MessageBox(NULL, "You Hit YES!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDNO)
    		{
    			MessageBox(NULL, "You Hit NO!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDCANCEL)
    		{
    			MessageBox(NULL, "You Hit CANCEL - Goodbye!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    
    	}
    	while(check != IDCANCEL);
    
    	return 0;
    }
    Sorry, in my previous post on adding a MessageBox after the loop I did not see that check was being initialized to the result of the first MessageBox.
    Last edited by Syneris; 01-05-2006 at 12:18 PM. Reason: code spacing

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    131
    Quote Originally Posted by Syneris
    try

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    				   LPSTR lpCmdLine, int nShowCmd)
    {
    
    	int check;
    	do
    	{
    		check = MessageBox(NULL, "Click an Option!",
    			"MessageBox Example!!", MB_ICONQUESTION |MB_YESNOCANCEL);
    		if (check == IDYES)
    		{
    			MessageBox(NULL, "You Hit YES!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDNO)
    		{
    			MessageBox(NULL, "You Hit NO!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    		else if (check == IDCANCEL)
    		{
    			MessageBox(NULL, "You Hit CANCEL - Goodbye!",
    				"MessageBox Example!!", MB_ICONASTERISK);
    		}
    
    	}
    	while(check != IDCANCEL);
    
    	return 0;
    }
    Sorry, in my previous post on adding a MessageBox after the loop I did not see that check was being initialized to the result of the first MessageBox.

    No problem...this is perfect....I am going to go and try and implement this in my ap.....thanks again everyone....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Visual Studio Express / Windows SDK?
    By cyberfish in forum C++ Programming
    Replies: 23
    Last Post: 01-22-2009, 01:13 AM
  2. Replies: 8
    Last Post: 12-01-2008, 09:09 AM
  3. C++ simple problem that I can't figure out
    By dvessey in forum C++ Programming
    Replies: 9
    Last Post: 05-25-2008, 10:56 AM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. cant figure out where to put the loop....
    By seal in forum C Programming
    Replies: 2
    Last Post: 08-30-2005, 10:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21