• Advertisement
Sign in to follow this  

First C++ Program

This topic is 4400 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
Or
#include <iostream>

int main()
{
std::cout << "Game Over!" << std::endl;
std::cin.get();
return 0;
}

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
This is definitely a FAQ.

You are writing a console program, and there is nothing wrong with the code. Just run the program from the console and get in the habit of running console programs from the console.

Share this post


Link to post
Share on other sites
Quote:

I don't think you read his post completely.



Ack... You're right. Sorry about that.

Share this post


Link to post
Share on other sites
Quote:
Original post by Photonman
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...


So I could type it like this?

{
std;cout << "Game Over!" << std::endl;
cin.get();
return 0;
}



Share this post


Link to post
Share on other sites
you can also try this. Sleep(2000) suspends the execution of the program for 2000 milisecs. :)


#include <iostream>
#include <windows.h>
using namespace std; //this allows you to not have to do this: std::cout, etc.

int main ()
{
cout << "Game Over, Dude!" << endl;
Sleep(2000);

return 0;
}

Share this post


Link to post
Share on other sites
Quote:
Original post by SKS
Quote:
Original post by Photonman
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...


So I could type it like this?

{
std;cout << "Game Over!" << std::endl;
cin.get();
return 0;
}


No, you would write it simply as cout, when you do "using namespace std" it pretty much just makes the typing easier on you.

Share this post


Link to post
Share on other sites
Quote:
Original post by SKS
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?


Are u looking for _getch() ? (note: this site may be changed in the future)

Share this post


Link to post
Share on other sites
Not exactly.... More like this:

// Game Over
// A first C++ program

#include <iostream>

using namespace std;//This eliminates the need for the std:: before everything

int main()
{
cout<<"Game Over!"<<endl;//Notice no std:: before cout and endl
cin.get();//Keeps the window open until enter is pressed.
return 0;
}

Share this post


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

  • Advertisement