C++

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

divorce3

Thread Starter
Joined
Nov 6, 2003
Messages
75
Hi y'all

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

Best wishes
 
Joined
Oct 18, 2004
Messages
355
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.

Code:
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

Garrett
Joined
Sep 26, 2005
Messages
4,029
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:

Code:
//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?
 
Joined
Oct 18, 2004
Messages
355
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:
Code:
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

Garrett
Joined
Sep 26, 2005
Messages
4,029
Grr, I know I must have something wrong here, but this is what I have:

Code:
//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.
 
Joined
Aug 31, 2005
Messages
44
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.
 
Joined
Apr 30, 2001
Messages
2,636
Yeh, you can do it a few different ways like fflush() + cin.get or fflush + getchar() or cin.ignore() and getchar(), or other comibinations.

Code:
#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();
}
 
Joined
Jan 17, 2006
Messages
1
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;
}
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top