1

Possible Duplicate:
How to stop C++ console application from exiting immediately?

I am a newbie on C++. Following tutorials, my program selfkilled when it finished executing simple commands (like cout ans stuff). I discovered the get.cin() function that avoided that. However, anytime I use 'cin' commands to insert variables, the program selfkills just after receiving the input and executing the work. Is there a way to avoid that? thanks a lot!

Community
  • 1
  • 1
  • 1
    Instead of exiting, what do you want/expect it to do? – msEmmaMays Sep 01 '12 at 21:29
  • 3
    If you write a program for console, then run it in console. – jrok Sep 01 '12 at 21:29
  • 1
    It doesn't self kill, it probably just reaches the end of its code. Just make it ask for another input at the end if you want the window to remain open. It's the most portable way to do it. –  Sep 01 '12 at 21:29

5 Answers5

1

The reason it quits when your program receives input, even though you are using std::cin.get() is because whenever cin reads input, there's a chance that some junk is left behind; when you call std::cin.get(), you will get that junk.

What you have to do is clear cin of any undesired data, such that std::cin.get() has nothing to read and is required to wait for new input.

...
std::cin.clear();
std::cin.get();
return 0; 
Paul Manta
  • 30,618
  • 31
  • 128
  • 208
1

A program doesn't "kill itself". It just exits when it has finished doing everything it was supposed to do (i.e. when you return from main).

It is up to you to set up your work environment so that you can see the output of a program. For example, if you are in Windows, you could open your own command line (run cmd) and run your program from there; or instruct your IDE to not close the terminal window after the program exits.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

Your program doesn't kill itself after execution, it just ends it.

Simple example:

#include <iostream>
int main( int argc, const char* argv[] )
{
    std::cout << "Hello, World" << std::endl;
    return 0; // End of execution
}

In that example a small window opens then close very fast because the logic of the code says so, However in the next example:

#include <iostream>
int main( int argc, const char* argv[])
{
     std::cout <<"Hello, World!" << std::endl;
     std::cin.get();
     return 0;
}

Your application will still be showing in the screen until you press enter key 'Return key' then it will exit.

In case you are using Windows Operating System, consider the next example:

#include <iostream>
int main( int argc, const char* argv[])
{
     std::cout << "Hello, World!" << std::endl;
     system("PAUSE");
     return 0;
}

Please note that system("PAUSE") is in Windows only and won't run on other operating systems.

One more thing worth mentioning here, there are a lot of methods to use other that these, but I wrote the most common ones.

Aamer
  • 755
  • 2
  • 11
  • 26
0

In some windowing systems, a console window is created when your program executes. When your program finishes, this console window disappears.

I always recommend the "pause" pattern to newbies:

cout << "Paused. Press ENTER to continue.\n");
cin.ignore(10000, '\n'); // Ignore until 100000 characters are entered or a newline is entered.

Sometimes, I make this into a function:

void Pause(void)
{
    cout << "Paused. Press ENTER to continue.\n");
    cin.ignore(10000, '\n'); // Ignore until 100000 characters are entered or a newline is entered.
}

Hope this helps,

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0

std::cin.get() work well and its usage is pretty easy but it expect user to press return. I used to end my program using ESC, so it won't work for me, so I use this

#ifdef _WIN32
std::system( "pause" );
#else
std::system( "read -n1 -r -p \"Press any key to continue...\"" );
#endif

It would print "Press any key to continue..." and continue execution with pressing any key so I can use my lovely ESC

BigBoss
  • 6,904
  • 2
  • 23
  • 38