Problem!

Started by
4 comments, last by m3rcyful ag3nt 18 years, 10 months ago
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
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.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
I think Agony was refering to the [source][/source] tags with his large empty box [smile]
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
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');




That´s really a good idea mate.
Always learning here :)

This topic is closed to new replies.

Advertisement