Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 7th, 2006, 5:20 AM   #1
joeiscool
Newbie
 
Join Date: Mar 2006
Posts: 6
Rep Power: 0 joeiscool is on a distinguished road
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.
joeiscool is offline   Reply With Quote
Old Mar 7th, 2006, 5:34 AM   #2
d_heyzie
Newbie
 
Join Date: Jan 2006
Posts: 22
Rep Power: 0 d_heyzie is on a distinguished road
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
d_heyzie is offline   Reply With Quote
Old Mar 7th, 2006, 5:42 AM   #3
joeiscool
Newbie
 
Join Date: Mar 2006
Posts: 6
Rep Power: 0 joeiscool is on a distinguished road
Thank You

It worked I appreciate you helping me out.
joeiscool is offline   Reply With Quote
Old Mar 7th, 2006, 6:30 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Mar 8th, 2006, 1:40 AM   #5
d_heyzie
Newbie
 
Join Date: Jan 2006
Posts: 22
Rep Power: 0 d_heyzie is on a distinguished road
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?
d_heyzie is offline   Reply With Quote
Old Mar 8th, 2006, 2:34 AM   #6
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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.
grumpy is offline   Reply With Quote
Old Mar 8th, 2006, 3:38 AM   #7
d_heyzie
Newbie
 
Join Date: Jan 2006
Posts: 22
Rep Power: 0 d_heyzie is on a distinguished road
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.
d_heyzie is offline   Reply With Quote
Old Mar 8th, 2006, 6:44 AM   #8
grumpy
Programming Guru
 
grumpy's Avatar
 
Join Date: Jun 2005
Location: Adelaide, South Australia
Posts: 1,260
Rep Power: 5 grumpy will become famous soon enough
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 ......
grumpy is offline   Reply With Quote
Old Mar 8th, 2006, 7:49 AM   #9
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Mar 8th, 2006, 2:43 PM   #10
HaCkeR
Hobbyist Programmer
 
HaCkeR's Avatar
 
Join Date: Nov 2005
Location: UK
Posts: 131
Rep Power: 0 HaCkeR is an unknown quantity at this point
Send a message via AIM to HaCkeR Send a message via MSN to HaCkeR
And for some reason if your like me and even Dawei's method doesnt work ad
cin.clear();

before cin.get();
HaCkeR is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:40 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC