1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

C++

Discussion in 'Software Development' started by divorce3, Jan 14, 2006.

Thread Status:
Not open for further replies.
Advertisement
  1. divorce3

    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
     
  2. Dan O

    Dan O

    Joined:
    Feb 13, 1999
    Messages:
    8,974
  3. InterKnight

    InterKnight

    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.
     
  4. smooth

    smooth

    Joined:
    Sep 26, 2005
    Messages:
    4,029
    First Name:
    Garrett
    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?
     
  5. InterKnight

    InterKnight

    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.
     
  6. smooth

    smooth

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

    flipninjax

    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.
     
  8. Shadow2531

    Shadow2531

    Joined:
    Apr 30, 2001
    Messages:
    2,636
    @all

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

    flipninjax

    Joined:
    Aug 31, 2005
    Messages:
    44
    Thanks, Shadow. That worked great! I can stop banging my head against the wall now.(y)
     
  10. Shadow2531

    Shadow2531

    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();
    }
    
     
  11. PBeck

    PBeck

    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;
    }
     
  12. Sponsor

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 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/434192

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice