![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2006
Posts: 6
Rep Power: 0
![]() |
C++ program self termination
Hi I am using bloodshed DEV-C++ Compiler and I write the C++ code and compile it it compiles then I debug it it says there are no bugs but for some reason every time a run the program,the program opens and then closes right away and I cant see what it's suppose to print. Which is Hello World Im a C++ program. Any help would be appreciated.
|
|
|
|
|
|
#2 |
|
Newbie
Join Date: Jan 2006
Posts: 22
Rep Power: 0
![]() |
I just include a file called "conio.h" and before I type "return 0;" i put the "getch()" function so it look similar to this:
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
cout <<"Hello World";
getch();
return 0;
}probably some other way to do it though |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Mar 2006
Posts: 6
Rep Power: 0
![]() |
Thank You
It worked I appreciate you helping me out.
|
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Again this is a question that is asked a lot of times already. conio.h is not portable, instead one might want to use cin.get() instead. It requires a return character (read: press the enter key), instead of just any key though.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jan 2006
Posts: 22
Rep Power: 0
![]() |
sorry if it seems like im taking over this post.. but ive seen cin.get() in alot of examples but for some reason it never works for me when i need to input a number and get a result, i input the number and the result flashes up and the program closes. Can someone tell me why?
|
|
|
|
|
|
#6 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
Because the result is displayed and the program immediately exits (eg drops off the end of main()). When the program exits, the associated console that the program's output is being displayed to is terminated by the OS, as it is no longer needed. This happens pretty quickly, so you don't get to see the program output before the console is destroyed.
You either need to stop the program from exiting (eg place a cin.get() just before main() completes and AFTER the last line of output that you want to see) or run the program from a command prompt (which makes the console have a lifetime unconnected from the execution of the program you're running). Last edited by grumpy; Mar 8th, 2006 at 3:03 AM. |
|
|
|
|
|
#7 |
|
Newbie
Join Date: Jan 2006
Posts: 22
Rep Power: 0
![]() |
so do i put a cin.get() before i make it return a value? if so thats exactly where i put it but it still just flashes up and terminates.
|
|
|
|
|
|
#8 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,261
Rep Power: 5
![]() |
There are a number of variants of the get() method (each taking a different set of arguments). Some return immediately if there is no input pending. Others wait for input. You obviously need to use one of the latter ......
|
|
|
|
|
|
#9 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Another problem is if the previous input has not all been used. Then the final cin.get () will see the unused input, grab it, zoop right on by, and exit. Put cin.sync () prior to the final cin.get ().
.... .... program code .... cin.sync (); cin.get (); return 0; }
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#10 |
|
Hobbyist Programmer
|
And for some reason if your like me and even Dawei's method doesnt work ad
cin.clear(); before cin.get(); |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|