Making program to close automatically after clicking on NO on a MessageBox (C++)

437 views Asked by At

I'm learning C++ programming at school and by myself as well. Now I'm trying to make some upgrades on the visual experience and on the user interface. My question is that can I close the program after a simple click on a MessageBox's "NO" button. I whant to skip "Press any key to contiue" after the program ran. Here is the code: `

#include<iostream>
#include<windows.h>
#include<stdlib.h>
using namespace std;
int main()
{
MessageBox ( NULL, "7. Adott az a(i), i=1,n szamsor. Hozzuk létre a b szamsort", "Feladat szovege", MB_OK);
float a[111], b[111], n;
cout<<"n= ?\b";

MessageBox ( HWND_DESKTOP, "Kerlek irj be egy szamot, hogy hany ertek van a sorozatban!", "10_tombok/07", MB_OK);

cin>>n;
cout<<"\n";

for(int i=1; i<=n; i++)
{
    cout<<"a"<<i<<"= ?\b";
    cin>>a[i];
}

for(int i=1; i<=n; i++)
{

    if (a[i]>=0)
    {
        b[i]=2*a[i];
    }
    else
    {
        b[i]=-a[i]/2;
    }

}

cout<<"\nA 'b' szamsor: ";

for(int i=1; i<=n; i++)
{
    if (i!=n)
        cout<<b[i]<<", ";
    else
        cout<<b[i]<<"\n";
}
int mbID= MessageBox (NULL, "Szeretned ujra futtatni a programot?", "Program end", MB_ICONQUESTION | MB_YESNO | MB_DEFBUTTON2);
switch (mbID)
{
    case IDYES:

    {
        return main();
        break;
    }
    case IDNO:
    {
        //here i need something
        return 0;
        break;
    }
}

`

1

There are 1 answers

1
Dave Rager On BEST ANSWER

To address the question, if running from within Visual Studio you will get the "Press any key to continue." prompt. Try running the executable directly.

For the question in your comment, use a loop. At the very least something like this:

int main()
{
    int done = 0;
    while(!done)
    {
        // skipping all the other program code above...

        switch (mbID)
        {
        case IDYES:
            break;
        case IDNO:
            done = 1;
            break;
        }
    }

    return 0;
}

It is actually undefined behavior to call main() from within your C++ program.

If you are really interested in Windows UI programming, have a look at some Win32 or MFC tutorials. It is a bit different from building console applications.