Hi guys, I'm just starting out and I'm having a little problem. When I run an application, it closes by itself right away.

A few days ago I went to the Local Library and picked out "Sam's Teach Yourself C++ in 24hrs - I know it's a lie" It came with a CD so that you can install Borland C++ - don't know what version it is, though it looks from at the least 2000. I had some trouble installing it and the interface looked pretty lame (old) so after searching through the forums I downloaded Dev-C++.

And I'm having the problem that the #4 poster had.

http://www.daniweb.com/forums/thread19944.html

I copied and pasted what poster #5 did and it worked. However when I tried to compile it without the C-style comments, I couldn't get it to work.

But the main problem I'm having is that when I run an app. It closes immediately. It's happened with two other tutorials in the book that I tried. I've come the conclusion that I'm doing them right since they open, I just can't get it to remain open for more than a second.

Here are the two other tutorials that I tried. Again they compiled without errors, and the app. ran except it closes down instantaneously. Thanks

callfunc.cpp

#include <iostream>

void DemonstrationFunction()
{
    std::cout << "In Demonstration Function\n";
}

int main()
{
    std::cout << "In main\n" ;
    DemonstrationFunction();
    std::cout << "Back in main\n";
    return 0;
}

func.cpp

#include <iostream>

int Add (int x, int y)
{
    std::cout << "In Add(), received " << x << " and " << y << "\n";
    return (x+y);
}

int main()
{
    std::cout << "I'm in main()!\n";
    std::cout << "\nCalling Add()\n";
    std::cout << "The value returned is: " << Add (3,4);
    std::cout << "\nBlack in main().\n";
    std::cout << "\nExiting...\n\n";
    return 0;
}

you could try

system("pause");

or

#include <conio.h>//this library is needed for getch()

getch();

right before the return command on your main

btw you should always use [[/B]code[B]] code here [[/B]/code[B]] tags on your posts

Thanks, it worked.

Do I always have to put in that command? I'm just curious as to why the book didn't mention it. Or is it the program that I am using?

Thanks once again. Another resourceful website!

It's no problem glad to know i helped you.

you could try

system("pause");

or

#include <conio.h>//this library is needed for getch()

getch();

right before the return command on your main

The system("pause"); version is frequently panned, and <conio.h> / getch(); is ancient and nonportable. So these "fixes" happen to be pretty low on the totem pole.
http://www.daniweb.com/forums/showthread.php?p=780810&highlight=system+pause#post780810

Do I always have to put in that command? I'm just curious as to why the book didn't mention it. Or is it the program that I am using?

The environment in which the program is run is what "requires" it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.