0

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;
    }
}

`

Servy
  • 202,030
  • 26
  • 332
  • 449
L. Huni
  • 3
  • 1
  • 2
    You can't use `main` in your program. And from the looks of it, your program is already finished when that message appears. Also, if I make `n` 111 or higher, your program gives me a good opportunity to exploit the buffer overrun. – chris Dec 03 '13 at 17:24
  • 2
    Don't call `main()` from `main()`. – Dave Rager Dec 03 '13 at 17:26
  • The program does close automatically. It sounds like your IDE runs it in a console, and doesn't close that console until you press a key. Possibly, there's an option in the IDE to close the console immediately. Alternatively, run the program directly rather than through the IDE. – Mike Seymour Dec 03 '13 at 17:27
  • If running from within Visual Studio you will get the "Press any key" prompt. Try running the executable directly. – Dave Rager Dec 03 '13 at 17:28
  • @DaveRager About your first comment... how could I doit in an other way? I want to make it to run that code again... I don't know any other way to do this. I would apreciate some tips. :) – L. Huni Dec 03 '13 at 17:38
  • @L.Huni, I'm not saying it's the ideal way, but other functions allow recursion. – chris Dec 03 '13 at 17:44
  • @chris, I'm just a learner, I don't have too much experiance in programming but trying to give my best. If you have any suggestions to write a program in a more effective way just write them down. And actually the whole program is made of one function (the main function) which, if i'm right, can't be missing from any code. – L. Huni Dec 03 '13 at 17:58

1 Answers1

1

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.

Dave Rager
  • 8,002
  • 3
  • 33
  • 52
  • Thank you wery much! And yes I'm intrested but i think that i need at least one more year of study until I reach the level when I can try somthing new. – L. Huni Dec 03 '13 at 18:05