C++ execution in prompt (DoS)

Status
Not open for further replies.
hello, I'm learning C++, and recently I have been making some testing programs(very simple things just to test what I've learned, about 15 strings per program)

but every experienced programmer knows that when we are learnig, we should pratice making programs that are made to work in the prompt, but when I compile the program and execute it, the prompt window shows up and instantly it dissapear, I need to see the program to confirm if I've done everything right (I think it's because the system not the code).

I'm using Windows XP, and Dev-C++ as my editor. I'll post the most simple code that cause what I said above.

#include <iostream>
using namespace std;

int main()
{
int length, width, area;
length = 7;
width = 5;
area = length * width;
cout << "A area é ";
cout << area;
return 0;
}

please if there is no way to fix that problem in the system, can you show me a way to avoid this in the code?note that this happens only with programs made to work with prompt.
 
Last edited:
It's not really a "problem"; it's just the way that the system reads the program. It reads "return 0;" after outputting to the command prompt and instantly exits. Practically any simple program of that type will do this. The good news is that there are lots of workarounds for this issue. Here are the two that I would personally recommend:

1. Does your editor have a "Start without Debugging" option? If so, use it to run the program. If you do this, then the program will end by saying "Press any key to continue...", allowing you to see what the program has done before closing it.

2. If that's not an option, you can throw in some extra code to make the program pause before exiting. The simplest way to do this is to put "system("pause");" directly before "return 0;". This will also make the program wait for you to press a key before closing.

Hope that helps!
 
Last edited:
First of all, ditch Dev-Cpp. "That Shit Is Wack!"; to quote someone I knew once upon a time. Please do not use the system() function. I am unsure if it still does, but once upon a time that was for performing system calls, and you don't want to switch between kernel and user mode for simply printing a quick message and leaving. What function asks for any one key? The answer: getchar(). So print your "Press any key to continue . . ." message and call getchar() with no arguments or anything special, simply let it run. It will have the same effect.

You will need to #include <stdio.h> to do this, though.

By the way, that isn't a DOS program, it is a Win32 console program. Learn the difference under penalty of being mocked
 
Re: getchar(): He can achieve the same effect and avoid being a weird mix of C and C++ by using cin >> dummy;.
 
Except that waits for a press of enter, not any character?

Also, Cine, we know you hate Dev-C++, but seriously, stop it. That's getting annoying.
 
Status
Not open for further replies.

Who is viewing this thread (Total: 1, Members: 0, Guests: 1)

Back
Top