First C++ Program

Started by
15 comments, last by Photonman 18 years, 3 months ago
I'm on the first C++ program and already got a problem. I am using Bloodshed Dev C++ I typed in the program as the book said to do // Game Over // A first C++ program #include <iostream> int main() { std::cout << "Game Over!" << std::endl; return 0; } Went to compile and run and it did open, but only for a second then it closed. Anyone know why? Could it be a probloem with my computer?
Advertisement
#include <cstdlib>#include <iostream>using namespace std; //this allows you to not have to do this: std::cout, etc.int main () {   cout << "Game Over, Dude!" << endl;   system("PAUSE"); // this is from the cstdlib     return 0;}


edit: fixed mistake pointed out by Zao.

[Edited by - Alpha_ProgDes on December 30, 2005 12:07:56 PM]

Beginner in Game Development?  Read here. And read here.

 

Or
#include <iostream>

int main()
{
std::cout << "Game Over!" << std::endl;
std::cin.get();
return 0;
}
Blog(Hungarian)MDX - C# - C++
Quote:Original post by Alpha_ProgDes
system(PAUSE);


That should be
system("pause");
since the function has the signature
int system(const char * _Command)


To the original poster, the reason that your application terminates immediately is that it reaches the return statement in main almost instantly, thus telling the OS that the application is finished and that it can close the window it ran in.

If you want to keep the window up to see what it did, you can either use Alpha's method and call the system command 'pause' or you can read some input from standard in which will make the application wait until some input is done.

To make it is hell. To fail is divine.

system(PAUSE); //I got errors when i tryed to compile it with this

std::cin.get(); //this worked

thanx
Quote:Original post by Zao
Quote:Original post by Alpha_ProgDes
system(PAUSE);


That should be
system("pause");
since the function has the signature
int system(const char * _Command)


To the original poster, the reason that your application terminates immediately is that it reaches the return statement in main almost instantly, thus telling the OS that the application is finished and that it can close the window it ran in.

If you want to keep the window up to see what it did, you can either use Alpha's method and call the system command 'pause' or you can read some input from standard in which will make the application wait until some input is done.


ok, so thats why pause gave me the error, thanx
Perhaps you have done so already, but do listen to what gymisi and Alpha_ProgDes have to say about the namespace-thingy. cout, cin, and every (well, almost) other function and object belonging to the C++-standard are placed in the std-namespace, and you should therefore specify the location of those things. It is in fact a requirement in most modern compilers.
Since no one told you *why* it was just 'crashing', I might as well.

There actually was no crash or error or anything. The problem was that your program ended and, well, quit becuase it was finished. Just like a function that you call, it won't just pause until the window is exited.

The two fixes listed above merely tell the computer to wait until a key press before closing the program. Another way to acheive this would be to open up the command console and running your program from there. That way, the program will finish, but its output(the text) will stay on screen since the console is still open.
Quote:Original post by Ezbez
Since no one told you *why* it was just 'crashing', I might as well.

There actually was no crash or error or anything. The problem was that your program ended and, well, quit becuase it was finished. Just like a function that you call, it won't just pause until the window is exited.

The two fixes listed above merely tell the computer to wait until a key press before closing the program. Another way to acheive this would be to open up the command console and running your program from there. That way, the program will finish, but its output(the text) will stay on screen since the console is still open.

Quote:Original post by Zao
To the original poster, the reason that your application terminates immediately is that it reaches the return statement in main almost instantly, thus telling the OS that the application is finished and that it can close the window it ran in.

If you want to keep the window up to see what it did, you can either use Alpha's method and call the system command 'pause' or you can read some input from standard in which will make the application wait until some input is done.

I don't think you read his post completely.

Beginner in Game Development?  Read here. And read here.

 

As a side note, you should prolly use cin.get() instead of system("PAUSE"), since system("PAUSE") isn't available on every platform and it actually suspends your program and goes to the OS to find the function.... Not all that important, it's just style.

And put using namespace std; at the beginning...that will save you from typing std:: every third line...
______________________________Stranger things have happened...The Following Sentence is True. The Above Sentence is False.

This topic is closed to new replies.

Advertisement