Jump to content
  • Advertisement
Sign in to follow this  
SKS

First C++ Program

This topic is 4670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
system(PAUSE); //I got errors when i tryed to compile it with this

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

thanx

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!