Code::Blocks Forums

User forums => Help => Topic started by: Zeus on August 30, 2005, 11:19:25 pm

Title: the program closes too fast...
Post by: Zeus on August 30, 2005, 11:19:25 pm
ok so i wrote a prog...where it asks u something and you type it in...in the console runner the program works fine and lets me type in my thing and then shows the result before it asks me to close it...when i try to run the .exe file from the project folder..I type in my number then I see the response flash very quickly and the program just closes before i can even read it..I tried putting "return 0;" at the end of my prog but still nothing..the only thing I have gotten to work is "system("PAUSE") " but I dont want to use that...someone help please
Title: Re: the program closes too fast...
Post by: AkiraDev on August 30, 2005, 11:26:43 pm
Windows explorer automatically closes command prompt programs as soon as they return an error code to the system (that's what your return 0; line is meant for) - this is normal behaviour. If you want your command prompt window to remain open, you should open the command prompt first (Accessories menu), and then run the program in there, a la MS-DOS.
Title: Re: the program closes too fast...
Post by: Zeus on August 30, 2005, 11:29:44 pm
is there another way...i dont feel like running the prog through the command prompt every time...
Title: Re: the program closes too fast...
Post by: rickg22 on August 31, 2005, 12:20:31 am
I think there was an option for shortcuts to keep the window opened after program exit... this way you can make a shortcut (in the desktop) to your program, and then just double click to run it. Ta-da :)
Title: Re: the program closes too fast...
Post by: Zeus on August 31, 2005, 01:48:22 am
What if i wanted to give this prog to a friend...if he tryed to open it he would type something and it flash and would look dumb...im sure he wouldnt want to go through all that "ok open a command prompt and type this in..." instead of just double clicking the .exe....
Title: Re: the program closes too fast...
Post by: takeshimiya on August 31, 2005, 01:58:37 am
In those cases is expected to use system("pause")

Or put a getchar() or something.
Title: Re: the program closes too fast...
Post by: Zeus on August 31, 2005, 03:19:39 am
getchar() didnt do anything...and ive heard that system("PAUSE") is bad programming practice...any other solutions
Title: Re: the program closes too fast...
Post by: rickg22 on August 31, 2005, 03:35:47 am
If you want to redistribute your program you can make a batch file... which would run the program in question and then do the pause.

Also, you might try EXPERIMENTING with getchar(), I think there was a case when the user didn't press any input... i think it returned 0...

again, it might be that the input buffer wasn't empty... argh. I don't really remember. You might like to try some c++ programming (specialized) forums...

Hope that helps :)
Title: Re: the program closes too fast...
Post by: sethjackson on September 01, 2005, 04:13:29 pm
Here is a simple program in C++ that waits until the user presses enter to quit.

Code
#include <cstdlib>
#include <iostream>

int main()
{
    std::cout << "Hello World!";
    std::cout << std::endl;
    std::cout << "Press Enter to exit.";
    std::cout << std::endl;

    std::cin.get();

    return 0;
}

This is probably not the most efficient way but it will work on all OS.