Jump to content
  • Advertisement
Sign in to follow this  
m3rcyful ag3nt

Problem!

This topic is 4821 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

Hi there everyone. I´m new here at the forum and also newbie at C++ programming, although I have some experience in java. My problem is the following: I recently bought a C++ book and along came a cd with some examples like this one: #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; return 0; } The problem is that when I input the value for fuel, say 100, the console window just vanishes, and it´s not only in this one. For example I created a small program to calculate the players score average of three players, and when I input the value for third player, the window disappears too. I really don´t know where´s the problem... The compiler I´m using is dev-c++. I really would appreciate some help because I´m clueless. Thanks a lot

Share this post


Link to post
Share on other sites
Advertisement
A) Welcome!

B) Just so you know for future reference, you can use

or
 for formatting code to look nice. The first one puts it in a white box with syntax highlighting and a possible scroll bar. The second one simply preserves spaces and indentation, and uses a fixed with font.

C) A simple fix would be to add the following right before your "return 0;" line:
system("PAUSE");
The problem is that the function is executing all your code, gets to the "return 0;", exits the function, and exits the program. You have to manually tell it not to. There are a few other functions that you can use as well to wait for a keypress, but this one is easy, and prints a nice message too.

Share this post


Link to post
Share on other sites
That just solve my problem :)
Thanks a lot mate.

btw, I have to say that i´m very impressed with the overall quality of this forum. Everyone really tries to help. Keep it up all of u guys!!

awrabest
RS

Share this post


Link to post
Share on other sites
Quote:
Original post by m3rcyful ag3nt
That just solve my problem :)
Thanks a lot mate.


Just for the sake of other newbies who may be having the same problem...
The window is not really "vanishing". Notice how after "fuel" your program
requires no further input. After you press enter there the program simply
finishes so quickly you don't see the cout lines after that(but they are
working).

You may want include some kind of exit routine, something that requires an
additional input at the end of your program.

char x;
do {
// guts of the program go here
cout << "Do you want to quit? Y or N";
cin >> x;
} while (x != 'y' && x != 'Y');





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!