|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
|||
|
|||
|
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) #include <iostream.h> int main() { int age; cout<<"Please input your age: "; cin>>age; if(age<100) { cout<<"You are pretty young!"; } else if(age==100) { cout<<"You are old"; } else { cout<<"You are really old"; //Executed if no other statement is executed } return 0; } |
|
#2
|
|||
|
|||
|
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 Code:
do
{
cout<<"Please input your age (enter zero to exit): ";
cin>>age;
//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. Frank Zappa |
|
#3
|
|||
|
|||
|
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. ![]() |
|
#4
|
|||
|
|||
|
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.
__________________
Clay Dowling Lazarus Notes Articles and commentary on web development http://www.lazarusid.com/notes/ |
|
#5
|
|||
|
|||
|
just pause...
Hi,
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: system ("PAUSE"); so your program will look like this: . . . . cout<<"You are really old"; } system ("PAUSE"); return 0; } 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++! Annie. |
|
#6
|
|||
|
|||
|
Here is two simple lines of code that could help okay actually 3.
first #include<conio.h> then right before your return 0 include the following: cerr<<"Press any key to continue"; getch(); 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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > C Programming > "easy" C++ help plz |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|