Why does my program close so fast?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Why does my program close so fast?

Post 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;
}
Last edited by MarauderIIC on Wed Jun 13, 2007 8:18 pm, edited 1 time in total.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Guest

Post 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:
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post 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.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Post 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.
User avatar
vmrob
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 97
Joined: Fri Sep 03, 2004 5:13 pm

Post 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
Post Reply