# C++



## divorce3 (Nov 7, 2003)

Hi y'all

In WINXP using Dev C++ compiler how do you get the RUN window to stay in place.

Best wishes


----------



## Dan O (Feb 13, 1999)

I moved your thread as your problem is not a Windows OS problem.

I also did a search for you and I found this:
http://www.codeguru.com/cpp/w-d/doc_view/windowpositionssizes/article.php/c6123/

Good luck.


----------



## InterKnight (Oct 19, 2004)

I typically use this code to achieve the pause because otherwise the window (as you stated) will run and typically disappear.

Just place this before your "return 0;" statement.


```
cout << "Press any key to continue...";
getchar();
```
Also, I am not sure if Dev-C++ is still in development (maybe someone else here in the forum may know?). Another good free alternative (still quite new to my knowledge, but they have me hooked) is Code::Blocks. I've been using it for the past few months with hardly any problems whatsoever. It's worth a look. I usually always am testing various IDEs while programming...to build a personal preference.

Take care.


----------



## smooth (Sep 26, 2005)

Sorry, don't mean to hijack the thread, but I tried the suggestion given, and the window I am using still closes. I created a very simple addition program, and this is what I have:


```
//Calculator Program
//Created 1-15-06

#include <iostream>

using namespace std;

int main()
{
    int integer1; //First integer to be input by user
    int integer2; //Second integer to be input by user
    int sum; //Sum of two integers
    
    sum = integer1 + integer2; //sets value of sum
    
    cout << "Please enter your first integer: "; //Asks user to input first integer
    cin >> integer1; //Prompts for input
    
    cout << "Please enter your second integer: "; //Asks user to input second integer
    cin >> integer2; //Prompts for input
    
    cout << "The sum of the integers is " << sum << endl; //Outputs sum of the integers
    
    cout << "Press any key to continue..."; //Outputs Press any key statement
    getchar(); //Prompts for input to continue
    
    return 0; //Indicates program is successful
}
```
Using Dev-C++, and when I hit enter after entering the second integer, it closes out really quickly.

Any reason why?


----------



## InterKnight (Oct 19, 2004)

Sorry guys, that was a big error on my part. Typed it out faster than I rrealized what I typed. Just to make sure I have everything right now, I went and looked it up.

The way to accomplish this is to use:

```
int main()
{
     ...//Your program here
     
     cout << "Press any key to continue...";
     cin.get();  //Gets the next character input by the user.
     return 0;
}
```
I am sorry about that.

Hope this solves your problem.

Take care.


----------



## smooth (Sep 26, 2005)

Grr, I know I must have something wrong here, but this is what I have:


```
//Calculator Program
//Created 1-15-06

#include <iostream>

using namespace std;

int main()
{
    int integer1; //First integer to be input by user
    int integer2; //Second integer to be input by user
    int sum; //Sum of two integers
    
    sum = integer1 + integer2; //Sets value of sum
    
    cout << "Please enter your first integer: "; //Asks user to input first integer
    cin >> integer1; //Prompts for input
    
    cout << "Please enter your second integer: "; //Asks user to input second integer
    cin >> integer2; //Prompts for input
    
    cout << "The sum of the integers is " << sum << endl; //Outputs sum of the integers
    
    cout << "Press any key to continue..."; //Outputs Press any key statement
    cin.get(); //Prompts for input to continue
    
    return 0; //Indicates program is successful
}
```
The window closes immediately before I see the answer, or can press a key.


----------



## flipninjax (Aug 31, 2005)

I'm having the same problem with Dev C++. When I create a program that doesn't require user input the window stays open until I hit enter or whatever. If there is any input required, I can see the output flash real quick but the screen closes hella fast after that with or without the code that is supposed to keep it open. 

I got the program from a book that's supposed to teach C++ for beginners, but I can't find a fix to this problem anywhere in the book.


----------



## Shadow2531 (Apr 30, 2001)

@all

put an fflush(stdin); line before the cin.get();


----------



## flipninjax (Aug 31, 2005)

Thanks, Shadow. That worked great! I can stop banging my head against the wall now.:up:


----------



## Shadow2531 (Apr 30, 2001)

Yeh, you can do it a few different ways like fflush() + cin.get or fflush + getchar() or cin.ignore() and getchar(), or other comibinations.


```
#include <iostream>
#include <limits> // For numeric_limites
#include <cstdio> // for fflush() and getchar()

using namespace std;

void pause() {
    cin.clear();
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
    cout << "\n" << "Press ENTER to exit . . ." << flush; // flushing just in case
    cin.get();
}

void pause2() {
    fflush(stdin);
    printf("\nPress ENTER to exit . . .");
    fflush(stdout); // flush stdout just in case
    getchar();
}

int main() {
    char c;
    cin >> c;
    pause();
}
```


----------



## PBeck (Jan 18, 2006)

another beginner way to do it which I remember seeing in "C++ For Dummies" goes like this:



{
....

// wait until user is ready before terminating program
// to allow the user to see the program results
system("PAUSE")
return 0;
}


----------

