Nov 22, 2009

Pause A Command Prompt After Execution?

free web hosting
Open Discussion & Free Web Hosting > Computers & Tech > Programming > Programming General > C, C++ & Visual C++

Pause A Command Prompt After Execution?

Feelay
sorry, title should be "Pause a console program after execution".

Hey!

ok.. when I created this thread, I needed help, but now I solved the problem, and here is the solution.

my problem was, that I couldn't make my application pause. It just executed, and then closed. so here is my old code.


CODE
#include "stdafx.h"
#include "iostream"
#include "stdio.h"
using namespace std;

int main()
{
    cout << "Hello World!" << endl;
    char name;
    cout << "Write your name, please: ";
    cin >> name;
    cout << "Hi " << name << "! Welcome, And Thank You For Trying My First Software :D"<< endl;
    cout << "Press ENTER to continue..." << endl;
    cin.get();
return 0;
}


And here is what I did.

after the
CODE
cin >> name;


I added this:
CODE
cin.ignore();


Now this will allow me to add the:

CODE
cin.get();

before the "return 0;", and this will pause the application till you press the ENTER button.




//Feelay

 

 

 


Comment/Reply (w/o sign-up)

wutske
Jep jep, I've always used this method to pause my C-applications smile.gif . It's much better than system.pause() because cin.get() works for linux too (system.pause() is a windows-only app).

Comment/Reply (w/o sign-up)

Quatrux
Yes, I did this too to pause a console application, sometimes I did system("PAUSE"); too but it works if there is a command PAUSE, it is by default on Windows, I think it's not by default on Linux, but as I know anyone can create such a command on Linux too.. The bigger issue is with CLEAR and CLS due to they both are different on Linux and Windows, so you have to check someway what system your using and only when use to clear the terminal or command prompt by CLS or/and CLEAR ;]

Comment/Reply (w/o sign-up)

magiccode9
Thank, that's help me a bit as I learning how to write c++.
While I do the same thing with getch() and check it's value if it is enter key then quit.

as this

CODE
if (getch() == 13)
{
    ...
}

Comment/Reply (w/o sign-up)

Feelay
sorry, but what do you mean by if (getch() == 13)
13 = the number for the enter key? if yes, can you please tell me where I can get the number for all keys, and if no, can you then please tell me what that is tongue.gif

Comment/Reply (w/o sign-up)

magiccode9
Hi,

It is just the ASCII computer code and you can get it from here http://www.asciitable.com.
And yes, 13 is the key code for enter key. smile.gif


Regards,
Eric,

Comment/Reply (w/o sign-up)


Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

This textarea will convert to Rich-Text automatically (IE, Firefox, Chrome)

Similar Topics

Keywords : Pause Command Prompt Execution


    Looking for pause, command, prompt, execution,

See Also,

*SIMILAR VIDEOS*
Searching Video's for pause, command, prompt, execution,
advertisement



Pause A Command Prompt After Execution?

Affordable Web Hosting, Low cost Web Hosting - ComputingHost.com