Hello, I'm learning to start to program in C++, and I have a problem with the prompt closing before the program ends. I know there's a lot of info about it, and I've googled it and searched here, but all the solutions to system("pause") don't work for me.

I'm using Dev-C++, and this is an example of a program that I tried to compile, but it closed after I input the random number.

#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
		int magic; //Magic number
		int guess; //User's guess
		
		magic = rand(); //Random number function
		
		cout << "Enter your guess: ";
		cin >> guess;
		
		if (guess == magic) { //if guess is right
			cout << "**Right**";
		}
		else {
			cout << "...Sorry, you're wrong.\n";
			cout << "The number was " << magic;
                }

                return 0;
}

I know it's basic, but whatever thing that I try (cin.get(), etc...), it just doesn't seem to work.

Thanks in advance

Dani AI

Generated

was seeing the usual Windows behavior: the program finishes and the console that was opened for it closes immediately. 's idea (block for user input before returning) is a valid fix; showed that running the program from an existing shell also keeps the output visible. Below are clear, practical alternatives and a short note on generating a bounded random number (your sample printed a large unbounded value).

Run the compiled program from a terminal (no IDE tweak needed). Example on Windows:

C:\> cd "C:\path\to\folder\with\exe"
C:\path\to\folder\with\exe> myprogram.exe

This way the console stays open after the program ends so you can read the output.

If you want the program itself to wait before exiting, use a line-based, portable wait (press Enter to continue):

#include <iostream>
#include <string>

// ... program logic ...

std::cout << "Press Enter to exit...";
std::string dummy;
std::getline(std::cin, dummy);

If you specifically need “press any key” without Enter on Windows, use the non-standard console call (conio.h):

#include <conio.h>
std::cout << "Press any key to exit...";
_getch();

See Microsoft’s documentation for _getch for platform details: ().

About the random number: prefer the C++11 random facilities to get a bounded, well-seeded value. Example pattern:

#include <random>

std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dist(1, 100);
int magic = dist(gen);

This produces a 1..100 value and avoids the unseeded, large results you saw. More on the modern random library: (https://en.cppreference.com/w/cpp/numeric/random).

Quick checklist: run from a terminal while testing, or add a portable wait (getline) to pause; use <random> for bounded numbers; avoid Windows-only tricks unless you need them.

Recommended Answers

All 9 Replies

So putting cin>>somevariable; before the return 0; doesn't work?

>>but it closed after I input the random number.

Because you programmed it to do that. As ^^^ said you have to put something before the return statement to make the program stop and wait for you to enter something from the keyboard.

Is this what you mean?

#include <iostream>
#include <stdlib.h>
using namespace std;

int main() {
		int magic; //Magic number
		int guess; //User's guess
                int end;
		
		magic = rand(); //Random number function
		
		cout << "Enter your guess: ";
		cin >> guess;
		
		if (guess == magic) { //if guess is right
			cout << "**Right**";
		}
		else {
			cout << "...Sorry, you're wrong.\n";
			cout << "The number was " << magic << "\n";
                }

                cin >> end;

                return 0;
}

It works although I have to do an input, but isn't there another way to do it?

depends: how do you want your program to end?

I was hoping that there could you could just press any key or enter to end it.

You just have to put the following instructions before return 0; :

cin.ignore();
cin.get();

It doesnt close when i run it, i get..

$ CC test.cpp -o main
$ ./main
Enter your guess: 23
...Sorry, you're wrong.
The number was 16838
$


I think it might be your compiler which it closing the file, otherwise it works fine

It doesnt close when i run it

I think he's using Windows (not Linux or Unix or ...)
And in Windows you can just double click on the executable file and it launches, but in his case it will just appear and dissappear directly, if he started it from a console window (what you're doing) it isn't dissappearing ...

So it isn't compiler related ...

I tried your suggestion:

cin.ignore();
cin.get();

return 0;

It worked, and thanks for your help.

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.