There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Software Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Software & Hardware > Software Development >
c++ newbie question


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
Dr3w's Avatar
Senior Member with 135 posts.
 
Join Date: Aug 2003
Location: Next to my pc
Experience: computer?... whats that
14-May-2005, 12:57 PM #1
c++ newbie question
sorry in advance for this really basic question but i am just beginning in c++, i am using bloodshed's dev c++ and when ever i do a tutorial program from the book i bought it compiles fine but when i go to run it the dos screen comes up than instantly closes. anyone know why that is?
__________________
go sox
InterKnight's Avatar
Computer Specs
Senior Member with 286 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
14-May-2005, 10:04 PM #2
I've worked with DevC++ just a little bit. Hopefully my advice will work. It works with programs that I work on in Microsoft Visual C++.

If it's not added in the #include statements, put in:

#include <conio.h>

for the console input/output header file (that's HOW the professor refers to it in his book, anyway).

And at the end of the program, before the return 0; statement in main() type getch();

That is what the professor of my college class always has us do. I hope I have been of some help.
__________________
"No matter how hard things may seem, you are never trapped until you stop trying."
______________________________________________________

Proud low-vision Linux user (Ubuntu 7.04 "Feisty Fawn").
Member of the Ubuntu Accessibility Team

Registered Linux User #423082
MacFromOK's Avatar
Senior Member with 1,947 posts.
 
Join Date: Mar 2001
Location: Oklahoma
Experience: idiota de la aldea
14-May-2005, 11:09 PM #3
You can also prolly open a DOS box first, then run the EXE from the command line.

Chers, Mac
alex_holker's Avatar
Senior Member with 1,928 posts.
 
Join Date: Sep 2003
Location: West Australia
Experience: Tech-Addict
15-May-2005, 07:05 AM #4
I use that compiler as well. The problem is that when the program finishes running, it will automatically close, rather than waiting for the user to close the program. You can get around this is to add a prompt for input to the end of your program. I personally used the example below, but InterKnight's method would work too.

Code:
#include <iostream>

int main() {
    char quit = ' ';

/*CODE AS NORMAL*/

    while (quit != 'q')
    {
        cout << "\nEnter q to close:" << endl;
        cin >> quit;
    }
}
Alex
__________________
3. There is no Gnomish god of heavy artillery.

Last edited by alex_holker : 15-May-2005 07:10 AM.
Dr3w's Avatar
Senior Member with 135 posts.
 
Join Date: Aug 2003
Location: Next to my pc
Experience: computer?... whats that
15-May-2005, 01:25 PM #5
thanks for the help guys i ended up just adding an input statment at the end but i will try interknights method next.
Shadow2531's Avatar
Distinguished Member with 2,629 posts.
 
Join Date: Apr 2001
15-May-2005, 03:11 PM #6
Just remember that if something is still left in the input buffer, getchar/getch etc. will grab it and your program will still exit.

I sometimes prefer this way.

cin.ignore(99999, '\n');
cin.get();
InterKnight's Avatar
Computer Specs
Senior Member with 286 posts.
 
Join Date: Oct 2004
Location: California
Experience: Advanced
15-May-2005, 03:50 PM #7
If that one doesn't work, I also have tried the system("pause"); statement also.
Kramer55's Avatar
Computer Specs
Senior Member with 588 posts.
 
Join Date: Jan 2005
Location: Michigan
Experience: Intermediate
15-May-2005, 10:34 PM #8
Don't use system("PAUSE") unless you need to; in a sense it calls another program to run while yours is executing in order to pause the program.

The best, as aforementioned, is probably cin.get();
Reply


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

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
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 06:39 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.