Hey all, I posted a question earlier this week regarding dev-c++ and my inability to keep the output window (#include <iostream>) open long enough to see my program running!

I have already tried the following things and would be grateful for any other suggestions!

1:
(Without typing using namespace std;) \\At the top
std::cout << "Press enter to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail + 1);
return 0;

2:
(Typing using namespace std;) \\At the top
cout << "Press enter to exit";
cin.ignore(cin.rdbuf()->in_avail + 1)
return 0;

3:
#include <cstdio> \\At the top
cout << "Press enter to exit";
getchar();
return 0;

Someone please help! I seem to have tried everything! If anyone knows dev-c++, could you give me a sure fire way of keeping the output window open! Thank you!

(And thanks to the guys or gals who have already suggest ways to help!)

#include <cstdio> \\At the top
cout << "Press enter to exit";
getchar();
return 0;

This DOES work.....

This sounds like a common error to me. In DevC++ MAKE SURE that if you are using console output (cout) or generally not using a window for display then make sure in "Project Options" (right click project in class browser pane) you have "Win32 Console" selected. If you have "Win32 GUI" selected the program compiles as a windows app and will terminate at the end, ignoring console commands such as cin!

try this

#include <cstdio>
#include <iostream>
using namespace std;

int main(void)
{
    cout << "Hello! Press enter to quit\n";
    getchar();
    return 0;
}

This code creates a program that displays hello and quits when you press enter...

In Dev-C++ go File > New > Project > Console Application > give the project a name > OK > create a directory, open it and save the .dev file there. A skeleton file comes up, flesh this one out with your code. The skeleton file has system("PAUSE") and the required #include <cstdlib> already there for your program to wait appropriately. What more do you want?

// the Dev C++ skeleton file for Console Apps

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    
    // ...  your code and more ...

    system("PAUSE");
    return EXIT_SUCCESS;
}

system("PAUSE") should be replaced with cin.get() for better portability!!!!

Ok, thanks all!

I'll try these methods and hope for success!

Thanks

I am having the same problem.

It seems that certain function calls cause the window to close. For example, when this function is called, the output window closes immediately and I have no idea why. All function calls before and after(when the following is commented out) work fine. Any ideas?

/* A recursive definition with cache. */
int calcFast(int n){

    start = clock();
    int cache[n + 1];
    for(int i = 0; i <= n; i++){
        cache[i] = -1;
    }
    cache[0] = 0;
    cache[1] = 1;
    return calcFastHelper(n, cache);

}

/* Helper method. Only called by calcFast() */
int calcFastHelper(int n,int cache[]){

    if (cache[n] >= 0)
        return cache[n];
    else{
        cache[n] = calcFastHelper(n-1, cache) + calcFastHelper(n-2, cache);
        return cache[n];
    }

}

Edit: I wanted to add that I am using the version 5.0 beta for Windows XP in case that makes a difference from the v4.0.

Edit 2: Having downloaded and tried it in the old v4.0, here are a few things found.

The program now works but REQUIRES that system("PAUSE") and cstdlib header be included to work without prematurely closing.

If someone knows a way around this with v5.0 beta, that would be great. It's a bit irritating having to do "work arounds" when the code is required to be written a particular way for a class. Thanks.

There will almost certainly be an error in the function which causes the closing. From experience DevC++ loves doing this if you give a variable an invalid value....

As for closing it is fairly easy to stop the program close at the end. When it hits "return 0;" its all over, YOU need to stop it from closing (a common mistake). cin.get;, getchar(); (include stdlib) and system("PAUSE"); (include stdlib) work fine

My point is, when I omit the one line of code that calls the above mentioned function, it works fine, and the output window does not close. However, as soon as I include the above mentioned function it immediately closes, even with system("PAUSE") or getchar(). Again, that is with v5.0, with v4.0 it works fine.

again: it is the function causing the error. invalid values are the MOST common cause for it to QUIT WITHOUT making a MS Windows error box. New versions have probably disguised the error and made the closing the only visible clue, that is where the error is though

I am having the same problem.

It seems that certain function calls cause the window to close. For example, when this function is called, the output window closes immediately and I have no idea why. All function calls before and after(when the following is commented out) work fine. Any ideas?

/* A recursive definition with cache. */
int calcFast(int n){

    start = clock();
    int cache[n + 1];
    for(int i = 0; i <= n; i++){
        cache[i] = -1;
    }
    cache[0] = 0;
    cache[1] = 1;
    return calcFastHelper(n, cache);

}

/* Helper method. Only called by calcFast() */
int calcFastHelper(int n,int cache[]){

    if (cache[n] >= 0)
        return cache[n];
    else{
        cache[n] = calcFastHelper(n-1, cache) + calcFastHelper(n-2, cache);
        return cache[n];
    }

}

Edit: I wanted to add that I am using the version 5.0 beta for Windows XP in case that makes a difference from the v4.0.

Edit 2: Having downloaded and tried it in the old v4.0, here are a few things found.

The program now works but REQUIRES that system("PAUSE") and cstdlib header be included to work without prematurely closing.

If someone knows a way around this with v5.0 beta, that would be great. It's a bit irritating having to do "work arounds" when the code is required to be written a particular way for a class. Thanks.

This calls for some creative printf-debugging. Temporarily insert some printf() followed by getchar() statements into strategic places in your code allowing you a look at the values of n and cache[n]. You might go past or below ( particularly in calcFastHelper(n-1, cache) ) the array which gives you a runtime error! You have to prevent n from going less than 0 !!!

Hey all, I posted a question earlier this week regarding dev-c++ and my inability to keep the output window (#include <iostream>) open long enough to see my program running!

I have already tried the following things and would be grateful for any other suggestions!

1:
(Without typing using namespace std;) \\At the top
std::cout << "Press enter to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail + 1);
return 0;

2:
(Typing using namespace std;) \\At the top
cout << "Press enter to exit";
cin.ignore(cin.rdbuf()->in_avail + 1)
return 0;

3:
#include <cstdio> \\At the top
cout << "Press enter to exit";
getchar();
return 0;

Someone please help! I seem to have tried everything! If anyone knows dev-c++, could you give me a sure fire way of keeping the output window open! Thank you!

(And thanks to the guys or gals who have already suggest ways to help!)

I am new too C++ but try this at the end of code in main before return statement system("pause"); this should work as it has been doing so for me for some time, but i find it does not work with the old style header files meaning the .h files and namespace. also dev appears not like cstidio or cstring instead replace without the C

Hey all, I posted a question earlier this week regarding dev-c++ and my inability to keep the output window (#include <iostream>) open long enough to see my program running!

I have already tried the following things and would be grateful for any other suggestions!

1:
(Without typing using namespace std;) \\At the top
std::cout << "Press enter to exit";
std::cin.ignore(std::cin.rdbuf()->in_avail + 1);
return 0;

2:
(Typing using namespace std;) \\At the top
cout << "Press enter to exit";
cin.ignore(cin.rdbuf()->in_avail + 1)
return 0;

3:
#include <cstdio> \\At the top
cout << "Press enter to exit";
getchar();
return 0;

Someone please help! I seem to have tried everything! If anyone knows dev-c++, could you give me a sure fire way of keeping the output window open! Thank you!

(And thanks to the guys or gals who have already suggest ways to help!)

Did you try
system("pause");
return 0;

did you try

system("pause");
return 0;

Its simple. Use getch() to hold the screen. You must include the conio.h header file when using getch(). You place it at the end of your code just before the return statement in main().
See below for demo:

#include <iostream>
#include <conio.h>
int main()
{
      cout << "hello world";
      getch();
      return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.