Problem with basic input

I'm using devc++ and by default it closes when I press any key. I'm trying to add input to my program and I can put in a number, but when I hit return the program closes. Is there a way to stop it closing when any key is pressed?
Reinstall.
Sorry, I realise that wasn't very clear at all. I don't mean devc++ closes. I mean when I run my code. There is a message that says "Press any key to continue..." and i'd like to remove it.
He means, uninstall. DevC++ is no longer maintained you should probably switch to something that will work. Something like VisualC++, CodeBlocks, or Eclipse is a much better idea.

as far as that Press any key to continue, that is something almost all IDE's have it stops the terminal/console/command window/... prompty closing before you can read any output.

some IDE's have option to view the console within the IDE so then there's no popup window but then this also takes up workspace area.

IMO I think codeblocks is a good idea as it is available on most mainstream OS's.
Check for a nightly build.

On the other hand being that you want to remove this "press any key to continue" you could use MS VC++ as this is disabled by default.
http://cplusplus.com/forum/beginner/1988/#msg7263
http://cplusplus.com/forum/beginner/1988/#msg7682

If we combine them:
1
2
3
4
5
6
7
8
9
10
11
12
13
class keepRunning {
    public:
        ~keepRunning() {
            /* Prompt the user to press enter */
            std::cout << "Press ENTER to continue." << std::endl;
            /* Wait until enter press and/or maximum amount of data is read by std::cin */
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
        }
}

int main() {
    keepRunning kr;
}

which will prompt the user to press enter regardless of how the program exits.
I didn't read the original post properly, basically if your wanting to keep the window open creating a function like the above chrisname mentioned will solve your problems, however if your wanting to remove the "press any key to continue" then your terminal will automatically shutdown when the program finishes.
I think even though DevC++ isn't maintained anymore it should still work in this regard. I think perhaps there might be something wrong with your code if it is doing this... eg. your taking input but doing nothing with it.

EDIT: 300th post woohoo!.

you may want to provide some sample code just to make sure we understand what is happening.
Last edited on
I originally used MS VC++, but I got an error with the IDE whenever I tried to run any code at all. There isn't much code at all, so I'll post it all:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main ()
{
    int name, age;
    cout << "What is your name and age?" << endl;
    cin >> name, age;
    cout << endl << "Your name is " << name << " and you are " << age;
    return 0;
}
I compiled and ran the code using VS c++ compiler, it didnt have the symptom you are describing, it didnt have the outcome you intended, but there were no system messages either
Last edited on
Topic archived. No new replies allowed.