How to quit a program???????

This is a discussion on How to quit a program??????? within the C++ Programming forums, part of the General Programming Boards category; Hello people, I was just wondering if anybody could tell me how to shut down a program when it ends ...

  1. #1
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128

    How to quit a program???????

    Hello people, I was just wondering if anybody could tell me how to shut down a program when it ends or when the user signals that they want to finish, I get how to let the user signal the end of a program but I don't have the commands to quit the program, any help would be greatly appreciated thanks.











    the mind is like a parachute it only functions properly when correctly packed and Falling with high velocity from a great height and the correct strings are pulled.

  2. #2
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    use exit(0); from stdlib.h

  3. #3
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128

    Elalboration required please

    Hi, I'm really quite new to this so I could do with a bit of elaboration on the last point please, the program I wanted to try it on I have already got stdlib.h here it is:

    Code:
    /*Program to work out the price of any quantity
    of products at any price, plus any rate of vat*/
    #include <iostream.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    int main(void)
    
    {
    
    	//Declare the variables
    	float cost, quant, price, vat, full, temp;
    	int length, count;
    	char yesno, vats[10];
    
    		  while (yesno != 'n' && yesno != 'N')
    		  {
    				//Get info from user
    				clrscr();
    
    				cout<<"Please enter the price of the product: ";
    				cin>>cost;
    				cout<<"\n\n";
    				cout<<"Please enter the quantity of products: ";
    				cin>>quant;
    				cout<<"\n\n";
    				cout<<"Please enter your rate of vat: ";
    				cin.ignore();
    				cin.getline(vats, 10);
    
    				length=strlen(vats);
    
    				if (length == 0)
    				{
    					 temp = 17.5;
    				}
    
    				else
    				{
    					 temp=atof(vats);
    				}
    
    
    				cout<<"\n\n\n";
    
    				//do the calculation
    				temp= temp / 100;
    				price= cost * quant;
    				vat= cost * quant * temp;
    				full= price + vat;
    
    
    				//give the answer
    				cout<<"The price of "<<quant<<" product(s) at "<<cost<<" each = £"<<price<<"\n\n"
    				"The vat = £"<<vat<<"\n\n"
    				"Price plus vat = £"<<full<<"\n\n";
    
    				cout<<"Would you like to use the program again? ";
    				cin>>yesno;
    				cout<<"\n";
    
    		  }
    		  clrscr();
    		  cout<<"\n\n\n\n\n\n\n\n\n\nThankyou Bye!";
    
    		  for (int i=0; i<=5000; i++)
    		  {
    				for (int j=0; j<=2500; j++)
    				{
    				}
    		  }
    		  exit(0);
    
    return 0;
    }
    That is my attempt at exit(o);, if it is wrong or I should be doing anything differently please let me know.

  4. #4
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    exit(0) forces program abort at the exact point it happens. Nothing will be processed after it. In fact, exact() is a dangerous tool when using dynamic memory because not even class destructors get called, I think (someone correct me if I'm wrong please).

    You should be the best judge on where to place it. According to your code I would say right after the "thankyou bye" cout. It's "Thank you", with a space, by the way

    But take the following advise please. There are very few reasons for you to want to end a program with exit(0). Especially for such a short program. Exit aborts a program and in this case it returns 0 to the caller, saying that everything is alright. Well, in fact, aborting means something is wrong. And should be used only for those cases when you trapped something wrong at runtime and you signal the program it can't proceed and should abort. And even in those cases it should return something other than 0.

    For a program as short as yours, I would say letting 'return 0;' be the one controlling program exit. Since you want to exit only when the user says he doesn't want to continue, your should design your code with that in mind. I don't know what those FOR loops are doing after the bye cout. Maybe you omited the code to keep it shorter. But consider moving them somewhere else. That way when the user presses 'N' or 'n', the DO loop ends and it processes the clear screen and the bye cout. And what will happen next?... the return 0 statement Exactly what you want to happen
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  5. #5
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    your program ends when it gets to the end of main or at a return. If you loop within your program, it runs the loop until conditions exist to get it out of the loop... which it then ends when you get to the end of your program or to a return.

    I really don't see a reason to use exit()... (especially where you used it at... right before the return)

    Just return when you want to exit. Then you do not need to include extra libs.


    Code:
    #include <iostream>
    using namespace std;  // or std::cout... whatever... irrelevant
    
    int main()
    {
    
      cout << "This text goes to the std output buffer.";
    
      return 0;  // a return here ends the program here
    
      cout << "This is never run";
    
    }
    Last edited by Betazep; 05-29-2002 at 05:14 PM.
    Blue

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    if you are using int main or float main or double main or whatever
    u MUST return a value
    dont forget that it's a function!

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by pode
    if you are using int main or float main or double main or whatever u MUST return a value dont forget that it's a function!
    "int main" is the only valid one. "void main" gets discussed a lot as a non-standard option (but it isn't one in my opinion). But I've never heard of "float main" and "double main". In DOS and Unix terms (and Windows I presume), the OS only provides an int for the program to return its value in, anything bigger will probably just get truncated or mucked up.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i'm just saying that its possible!

  9. #9
    Registered User Mario's Avatar
    Join Date
    May 2002
    Posts
    317
    Yes, but exit(0) does return a value. 0.

    In both cases (with return 0 or exit(0)) he was returning a value
    Regards,
    Mario Figueiredo
    Using Borland C++ Builder 5

    Read the Tao of Programming
    This advise was brought to you by the Comitee for a Service Packless World

  10. #10
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by pode
    i'm just saying that its possible!
    No it's not, that's the point. DOS and Unix allow only the sizeof an int (two bytes I believe) for a return code. One is used for the return code itself, and the other is for additional information (signal information etc).

    The maximum return code you can send back to the OS it 255. Here's an example run under Cygwin. It shows a return of 300, which the OS sees as 44.
    Code:
    $ sh
    $ exit 300
    $ echo $?
    44
    If you're program sends back a float, the info after the decimal point is removed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #11
    ResurgentBarbecue UnclePunker's Avatar
    Join Date
    May 2002
    Posts
    128

    I think I've been misunderstood

    Hello again, I've been to sleep and I am back at work now using the internet instead of working , I understand how to end the program, that is not a problem, I am only new to C++ and I understand that when I return whatever then the program is finished, the while loop keeps it going until the user signals the end by pressing 'n' or 'N', I assumed exit(0); was a way to shut the program down afterwards so I put a nested for loop with no code in it just to create a short delay before the program shuts down, if anybody could let me know a bit of code to shut the program down that would be great.

    By the way thank you for the spelling check.

  12. #12
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    did i say it was the correct way, NO
    what i meant was that u can write a program like that
    ofcourse u havent seen a program like that cause its wrong
    but u can write it like that

  13. #13
    _B-L-U-E_ Betazep's Avatar
    Join Date
    Aug 2001
    Posts
    1,412
    exit and return are the same thing (essentially). So if you understand how return ends the program.... what is the problem?

    You put return in.... the program ends. That is it...


    classic example

    Code:
    ...
    
    ofstream outs;
    
    outs.open("data.dat");
    
    if (outs.fail())
        cerr << "Failure opening data.dat";
        return 1;  // or exit(1)... program ends here if condition exists
    
    outs << "blah blah blah";
    
    outs.close();
    
    return 0;  // or exit(0)... program ends here if it makes it this far
    
    ...
    And as far as using runaway loops as a delay... don't do it. Use one of the delay functions like Sleep() for MSVC++ and others, or sleep() for borland, or delay for dos.... etc.

    Using loops to delay a program is bad practice.
    Blue

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Press "Enter" key to quit the program
    By Vitamin_C in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2005, 08:25 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. Error when trying to quit program...
    By marCplusplus in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2002, 08:03 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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