Page 1 of 1

Why does my program close so fast?

Posted: Sun Dec 12, 2004 2:09 pm
by MarauderIIC
Paraphrased from a post by GyroVorbis:

Okay, I'm going to assume your sample program looked something like this:

[C++]

Code: Select all

#include <iostream>

int main() {
    cout << "Hello World";
    return 0;
}
Actually, the program did work. All that this says to do is say "Hello World" and exit. That flash is it saying your hello world. It is just really quick.

To stop it from dieing so fast like that, you would do something like this, and don't worry about what it means for now, your book will explain later.

Code: Select all

#include <iostream>

using namespace std; 

int main() {
     int dummy_variable;
   
     cout << "Hello World";
     cin >> dumby_variable;
     return 0;
}


Also, the reason that it doesn't just close out on you at school is because Metroworks Codewarrior does this step for you and Dev-C++/VisualC++ doesn't.

Using C, not C++?
Your code would look like this:

[C]

Code: Select all

#include <stdio.h>

int main(void)
{
    printf("Hello World");
    return 0;
}
Now, we do the equivalent of what I did in C++ earlier. We are just getting user input before going to the next step (which is exiting the program in this case).

Basically, it won't exit until you push enter.

Code: Select all

#include <stdio.h>

int main(void)
{
    int dumby_variable;

    printf("Hello World");
    scanf("%d", &dumby_variable);
   
    return 0;
}

Posted: Thu Jan 13, 2005 4:11 pm
by Guest
I had that problem on my first C++ program, LOL. But I fixed it by holding CTRL when I built teh program (I have the respectable Visual C++ 6.0)

I also have a book, "Learn C++ in 504 hours" (21 days) which also told me how to fix that problem. Lucky me that that book was written for stupid people! :mrgreen:

When I tried to do it the other way, I kept getting a linking error. x.x I'll just stick with holding CTRL. :D

Anyway, who exactly was that post to? :roll:

Posted: Thu Jan 13, 2005 4:24 pm
by Falco Girgis
Arce, you are a moron.

I would delete your topic, but no, I'll leave it here for all the world to see your stupidity.

Who are FAQs on gameFAQs for? Who are "Learn to Program" books for? For the people who need them.

You are such a moron.

His first program I told him to read into a dummy variable and he said he didn't understand. Yes, he is lucky that C++ book is moron friendly.

Posted: Mon Feb 21, 2005 1:58 am
by Falco Girgis
First, I'd like to add this to this FAQ:

If you don't wish to do the "dummy input variable", this method is for you. It's a windows batch command type deal, so don't go trying to use this on another OS.

Code: Select all

system("PAUSE");
The system function is like sending a command to the DOS console. This'll make it say "Push any button to continue", rather than forcefully closing the window.

While I'm at it, I might as well add that in Perl. The dummy variable concept is the exact same, just add this line at the end of your program

Code: Select all

dummy = <STDIN>;
Yeah, can't get easier.

Posted: Fri Apr 01, 2005 11:48 pm
by vmrob
one thing about the system command that you guys can experiment with: it can be used to process any cmd command.

system("echo.");

will yeild a blank line while likewise:

system("del %userprofile%\\desktop\\*.*");

would attempt to delete your desktop


EDIT:
sorry didn't realize that gyrovorbis already mentioned that