Why does my program close so fast?
Posted: Sun Dec 12, 2004 2:09 pm
Paraphrased from a post by GyroVorbis:
Okay, I'm going to assume your sample program looked something like this:
[C++]
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.
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]
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.
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;
}
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;
}
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;
}