How to run a compiled file

Re: How to run a compiled file

Post by fashizzlepop on Mon Jan 10, 2011 2:48 am
([msg=51948]see Re: How to run a compiled file[/msg])

alexphang88 wrote:Hmm . . . is it a compiler problem? The codes in the example in the book has no added codes in them, yet they seem to be able to be displayed in the console (or whatever the name was, the black window thing), the example uses Borland.

They probably mention what they are doing (most likely running from cmd) in the Forward.
The glass is neither half-full nor half-empty; it's merely twice as big as it needs to be.
User avatar
fashizzlepop
Developer
Developer
 
Posts: 2304
Joined: Sat May 24, 2008 1:20 pm
Blog: View Blog (0)


Re: How to run a compiled file

Post by nathandelane on Mon Jan 10, 2011 10:49 am
([msg=51960]see Re: How to run a compiled file[/msg])

alexphang88 wrote:Hmm . . . is it a compiler problem? The codes in the example in the book has no added codes in them, yet they seem to be able to be displayed in the console (or whatever the name was, the black window thing), the example uses Borland.

In the past Borland has been known to automatically halt execution at the end of a text-mode or console-based program. It was many years ago that I use Borland's IDE however, and since then I mostly use the console (or terminal) for a lot of my development.

The system(const char *) method is part of cstdlib (or stdlib.h in ANSI C). The problem is not whether system is cross-platform compatible, rather the problem is in the argument that system takes. System essentially opens up a thread process to run a command against the command handler on your system. It makes a call to the operating system. Thus while:

Code: Select all
system("pause");

works on Windows and DOS-based systems, it won't work on *nix-based systems (UNIX, Linux, BSD, Solaris, etc.). The pause you are passing in this case is a call to the BATCH PAUSE command supplied in Microsoft OS's. This is why cin.get() is a preferred method, because it is guaranteed to be cross platform, and therefore more correct. cin.get is in C++'s iostream. If you are not using C++ then use getchar() in C's stdio.h.

For example:

Code: Select all
// C++
#include <iostream>

int main(int argc, char* argv[])
{
   ...
   std::cin.get();
   
   return 0;
}

or

Code: Select all
// C
#include <stdio.h>

int main(int argc, char* argv[])
{
   ...
   getchar();
   
   return 0;
}

alexphang88 wrote:And from the looks of it, there are other system("something") functions?

So yes there are technically other system("something") commands, but they are operating system dependent. So in short if you can avoid them, then avoid them. Find a C or C++ solution that does the same thing. Avoid breaking out into the operating system for anything.

-- Mon Jan 10, 2011 9:03 am --

I just downloaded Dev-C++ (5.0 beta) to see what it does by default. I created a new Console Application project, and the default main.cpp it gave me was this:

Code: Select all
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}

While not very good C++ grammar, as I explained above, this code snippet will do what you expect it to on Windows, that is keep the console window open until a key is pressed. All code then goes above the system("PAUSE"); statement. But also again, this is not a good practice if you are going to turn this in as homework or even project or production code. In that case you would want to take out the system("PAUSE"); all together, and if a requirement is for the console window to remain open after execution, then use the code snippets above.
Me, Nathandelane, Highly influential to Hackerdom, Premature Optimization=http://c2.com/cgi/wiki?PrematureOptimization
User avatar
nathandelane
Poster
Poster
 
Posts: 204
Joined: Thu Jun 26, 2008 11:26 am
Location: Utah
Blog: View Blog (0)


Previous

Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests