Sign in to follow this  
IsseyMiyaki

[Help] C++, My work is jumping for a second and gone.

Recommended Posts

Hello, my first post here, i saw thad there is nice community here and hope you guys will help me a little to become "Pro C++ Game's Dev" hehe. I have some C++ Issues, when im writing a code's accordingly to the "Beginning C++ Game Programming" Book, i have my windows jumping for like 10 mil.seconds and gone. Author words: "When you run the Game Over program, you might only see a console window appear and disappear just as quickly. That's because C++ is so fast that it opens a console window, displays Game" he wrote some codes thad will make you to press enter to exit the app. | std::cout << "Press the enter key to exit"; std::cin.ignore(std::cin.rdbuf()->in_avail() + 1); it was working on all the apps till i get to code's with the "cin" command in it, For Example: // Game Stats // Demonstrates declaring and initializing variables #include <iostream> using namespace std; int main() { int score; double distance; char playAgain; bool shieldsUp; short lives, aliensKilled; score = 0; distance = 1200.76; playAgain = 'y'; shieldsUp = true; lives = 3; aliensKilled = 10; double engineTemp = 6572.89; cout << "\nscore: " << score << endl; cout << "distance: " << distance << endl; cout << "playAgain: " << playAgain << endl; //skipping shieldsUp since you don't generally print Boolean values cout << "lives: " << lives << endl; cout << "aliensKilled: "<< aliensKilled << endl; cout << "engineTemp: " << engineTemp << endl; int fuel; cout << "\nHow much fuel? "; cin >> fuel; cout << "fuel: " << fuel << endl; typedef unsigned short int ushort; ushort bonus = 10; cout << "\nbonus: " << bonus << endl; cout << "\nPress the Enter Button"; cin.ignore(cin.rdbuf()->in_avail() + 1); return 0; } When i type the Fuel it just exiting the Dos. I'am using "Dev-C++" Compiler. Some help please? BTW i didn't saw CODE tag like on other forums, so i just pasted it normally =]

Share this post


Link to post
Share on other sites
replace the lines:

cout << "\nPress the Enter Button";
cin.ignore(cin.rdbuf()->in_avail() + 1);


with:


system("pause");


and that should work.

Share this post


Link to post
Share on other sites
Thanks alot,
its working,
do i need to put this at end of all my apps?
because i really dont want it on my other one's,
there is some other way?
anyway i think ill fing it on the book,
thanks alot.

edit:
and how can i make the "Press and key to continue..." message to start on new line.??

Share this post


Link to post
Share on other sites
Another way to do it - if you want to specify yourself the message to be displayed - is to display the message you want the user to see, then make the console app wait for user input.

eg.

cout << "You must press Enter to continue!";
cin.get();


although, that will only accept the enter key to proceed, any other keys the user presses will be displayed in the console window.

Share this post


Link to post
Share on other sites
Quote:
Original post by IsseyMiyaki
there is some other way?

The proper way is to run console applications from the console.

Share this post


Link to post
Share on other sites
Quote:
Original post by IsseyMiyaki
Thanks alot,
its working,
do i need to put this at end of all my apps?
because i really dont want it on my other one's,
there is some other way?


Actually you are not going to use it for long. You need it now because your programs are very simple and run only once. When you have things that repeat (loops) you won't need it.

Quote:

edit:
and how can i make the "Press and key to continue..." message to start on new line.??


cout << "\n"; // before system("PAUSE") ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by IsseyMiyaki
edit:
and how can i make the "Press and key to continue..." message to start on new line.??


The same way you make anything on a new line; by outputting an std::endl or "\n" to std::cout.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this