Thread: program to close its own window

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    3

    program to close its own window

    At the end of my code, I would like to add in a piece of code that simply closes out of whatever window the program is running in. I would like the equivalent of pressing the "X" button in the top right (running windows), only automatically.
    Code:
    	if (bool=='n'||bool=='N')
    	{
    	// something to exit program
    	}

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Usually you just let the program fall off the end of main() or WindMain() to exit.

    Post your code, at least enough to see any loops you're using... in code tags... please.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    here's the code
    Code:
    /*QUADRATIC FORMULA*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    main()
    {
    	float a;
      	float b;
    	float c;
    	float d;
    	float e;
    	float f;
    	float x1;
    	float x2;
    	int choice;
    	float sqr;
    	char bool='Y';
    	
    	while (bool=='y'||bool=='Y')
    	{
    	
    	a=0;
    	b=0;//setting variables to 0
    	c=0;
    	d=0;
    	e=0;
    	f=0;
    	x1=0;
    	x2=0;
    	
    	printf ("A=\n");
    	scanf ("%f",&a);
    	printf ("B=\n");
    	scanf ("%f",&b);
    	printf ("C=\n");
    	scanf ("%f",&c);
    	
    	if (a==0 && b==0 && c==0)
    	choice=1;
    	else if (a==0 && b==0 && c!=0)
    	choice=2;
    	else if (a==0 && b!=0 && c!=0)
    	choice=3;
    	else if (pow(b,2)-4*a*c < 0)
    	choice=4;
    	else if (pow(b,2)-4*a*c == 0)
    	choice=5;
    	else
    	choice=6;
     
        switch (choice)
        {
          case 1:
    	printf ("\n\n\t\t\tInfinite Solution der...");
    	break;
          case 2:
    	printf ("\n\n\t\t\ttTHATS ONLY ONE NUMBER!!");
    	break;
          case 3:
    	x1=-b/a;
    	printf ("\n\n\t\t\tSingle Root\n\t\t\tx= %4.2f", x1);
    	break;
          case 4:
    	d=sqrt(pow(b,2)-4*a*c);
    	e=-b/(2*a);
    	f=d/(2*a);
    	printf ("\n\n\t\t\t\tTwo complex roots\n\n\t\t\t x1 = %4.2f + %4.2fi", e, f);
    	printf ("\n\n\t\t\t x2 = %4.2f - %4.2fi", e, f);
    	break;
          case 5:
    	printf ("\n\n\t\t\tRepeated Root\n\t\t\t");
          case 6:
    	
    	x1=(-b+sqrt(pow(b,2)-4*a*c))/(2*a);
    	x2=(-b-sqrt(pow(b,2)-4*a*c))/(2*a);
    	printf ("x1=%f x2=%f",x1,x2);
    	break;
    	}
    	bool='N';
    	printf ("\nagain? press Y for yes, or N for no\n"); 
    	scanf ("%s", &bool);
    	while (bool!='y'&&bool!='n'&&bool!='Y'&&bool!='N')
    	{
    	printf ("\nY for yes, or N for no\n");
    	scanf ("%s", &bool);
    	}
    	printf ("\nthankyou\n");
    	if (bool=='n'||bool=='N')
    	{
    	// something to exit program
    	}
    	}
    }
    ending main() exits the program, but it doesn't close the window

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    That's going to depend how you're running it....

    If you are opening a console or terminal window then running the program, exiting the program will not close the window.
    It stays open in case you want to do something else. This is by design in the operating system.

    If you are clicking on the program, opening a temporary console or terminal, it should close when main exits.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    3
    I'm clicking "run..." on my compiler. I suppose that this is the first case in your statement

    *edit
    I tried running the .exe file directly, but it didn't close out either
    Last edited by wizard21212; 04-20-2011 at 09:00 PM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try replacing all this....
    Code:
    	bool='N';
    	printf ("\nagain? press Y for yes, or N for no\n"); 
    	scanf ("%s", &bool);
    	while (bool!='y'&&bool!='n'&&bool!='Y'&&bool!='N')
    	{
    	printf ("\nY for yes, or N for no\n");
    	scanf ("%s", &bool);
    	}
    	printf ("\nthankyou\n");
    	if (bool=='n'||bool=='N')
    	{
    	// something to exit program
    with this ...
    Code:
    printf("Like to try again? (Y/N) : ");
    scanf("%c", bool);
    }
    Make sure it's inside your while (bool == ... loop right at the bottom.
    Anything but Y or y should then exit your program.

    It's my fathers old rule... K.I.S.S. ... Keep It Simple Son...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. close window
    By sgh in forum C# Programming
    Replies: 1
    Last Post: 04-27-2009, 08:24 AM
  2. how do i close one window at a time?
    By stde in forum Windows Programming
    Replies: 4
    Last Post: 08-01-2006, 12:43 PM
  3. Help Needed - Window won't close
    By DZeek in forum C++ Programming
    Replies: 12
    Last Post: 03-06-2005, 07:24 PM
  4. Automatic Window Close
    By Nectron in forum C++ Programming
    Replies: 1
    Last Post: 05-30-2003, 01:30 PM
  5. how to command to close a dos window?
    By Unregistered in forum C++ Programming
    Replies: 9
    Last Post: 07-19-2002, 02:04 PM