Sunday, August 2, 2009

I'm having trouble running my C++ program after i compile and link it?

I just start programing in the C language. I Use Microsoft developer studio standard (if there something better, add that, though i can't choose you as best answer unless you answer my actual question). I'm also learning from "C for dummies".





well, enough of that, I made my first program. all it does is say "goodbye crule world" (in contradicts the "hello world" prgram.) So I compiled and linked my .C file into a .exe file. when i open the source file and go to "run GOODBYE.EXE" it works fine. but when i got to my documents and open the .EXE file there, it breifly opens and then closes. and i didn't push the any key (which my book says is enter :) )





so, help me out here, why doesn't it stay?

I'm having trouble running my C++ program after i compile and link it?
Ok, first you need to get your terminology right. This is computer programming and computer science: precision is important. C++ and C are different. I’m assuming you’re are writing a C program. Not a C++ program. Second, there’s no such thing as Developer Studio. I’m assuming you are using Visual Studio? You also should mention the version. It’s important. VS 2005? VS 2003? VC++ 6? Don’t skimp on details.





Next, don’t use C for dummies. Think about it for a moment. If you find C difficult, you need to pick a different programming language. C is naturally complex. It’s a low level language, so you don’t have much logical abstraction. And it has quirks. It is difficult. The book I recommend is K %26amp; R’s The C Programming Language. Only that. It is the de facto book on C, and you should get that. If you find C too difficult to start with, pick a different language, please.





Now, onto the actual program. The program you created is what is known as a console program. If you go to start-%26gt;run-%26gt;”cmd”, you get a console or command line. You should learn to run programs from there. As a beginner, it’s acceptable to use a kludge: #include %26lt;stdio.h%26gt; and at the end of the main, add getchar();
Reply:The program runs. Here's the deal windows runs fast enough that it will open a cmd windows display "goodbuy crule world" read the return 0; in your code and exit all in about the time it takes to blink! ( I've had the same problem before ). Try adding this to the end of your code.





#include %26lt;iostream%26gt;


using namespace std;


cout %26lt;%26lt; "enter any number to exit /t" %26lt;%26lt; endl;


int completely_useless_num;


cin %26gt;%26gt; completely_useless_num;


return 0;





// happy coding
Reply:When you run a console program from the windows explorer, it opens a console, runs your program, and then the console closes as soon as your program finishes. Whereas, when you run it in visual studio, the compiler doesn't close the console window right away, and waits for your input first.





There are two ways to get around this.





One way is to run your program from the command line instead of the from the explorer. If you know how to use the dos command line, this is your easiest option.





The second way is to make your program pause before terminating. To do so, add the following line at the end of main():





cin.get();
Reply:lol You know that C++ and C are two different languages right? I could help you in C++ but C might have a different way of doing it.





before the last return statement put





cin.get();


cin.get();





It'll pause the screen, waiting for input from the user (clicking any button).





I put 2 down b/c 1 doesn't always work. This is the simplest way I've found to do it. And I'm sorry if that doesn't help in C, I know how frustrating programming can be.
Reply:Windows automatically closes any console program launched from the file browser as soon as it is finished running. So once your program prints its message, there's no more code, it exits, and Windows closes it. This only takes a millisecond or so to process, much too fast for a human being to read.





Just add this line:





getchar();





Put it in your program right under your printf() statement. This will cause the program to wait for one character of input. It doesn't do anything with the input since the function isn't assigned to any variable. But it will wait, as long as it takes, to get at least one character from the user. Until it gets that, it won't exit.


No comments:

Post a Comment