January 8th, 2003, 08:08 PM
"easy" C++ help plz
ok my problem is when i write all the code and then press execute the program work great but when i enter the number to make the program works it close automatically what i coud do to fix this? here is my code:
(sorry for my english)
cout<<"Please input your age: ";
cout<<"You are pretty young!";
cout<<"You are old";
cout<<"You are really old"; //Executed if no other statement is executed
January 8th, 2003, 10:12 PM
the program runs and finishes so it exits.
try pausing the program before the return 0 (if this works in your compiler)
sleep(5000);//in milliseconds so five seconds
Better, try putting a while loop and setting up a condition (value ) that the user can enter to leave the program
cout<<"Please input your age (enter zero to exit): ";
//chack to see if the user tried to exit
if(0 != age)
//all your code here
}while(0 != age)
The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.
January 11th, 2003, 01:47 PM
You could either do that or you can simply run your program from a DOS prompt or console NOT from Windows.
I made the same mistake, when I first started teaching myself C++ programming. :D
January 15th, 2003, 04:55 PM
When I was teaching my students quickly learned to keep a command shell open and they did all runs from the command line, never the Execute button. That button is really only useful for Windows applications.
Articles and commentary on web development
January 31st, 2003, 07:48 AM
This looks like one of the program I gave to my students...
we are using Dev-C++ software (which works great on Windows platform apart from Windows XP) and the easiest way for your particular problem is to include this line just before the return 0:
so your program will look like this:
cout<<"You are really old";
when you use this your string will be displayed and everything will stand still until you press any key (and will display"Press any key to continue...")
I know that you posted this quite a while ago but just in case...
Enjoy learning C++!
February 2nd, 2003, 12:14 AM
Here is two simple lines of code that could help okay actually 3.
then right before your return 0 include the following:
cerr<<"Press any key to continue";
the cerr is part of the iostream header file similar to the cout statement allowing you to display something the to the string.
the getch(); is part of the conio.h header file it basicly allows you to store junk variable allowing you to pause the screen until as in the cerr statement says press any key to continue. then whatever key is pressed is stored then the program will go to the return 0 and then end the program
hope this helps