PDA

View Full Version : C++ Console App Problem.


Gumball Watterson
June 19th, 2009, 07:06 PM
I have experience with programming now and I decided to look into C++. When I try to debug a Console Application program, it immediately exits and I have no idea why. I've looked into the MSDN forums and support but they have no solution because I use Visual C++ 2008 and those morons don't have support for C++ 2008.

So I was wondering if anyone here has the brain (or heart) to tell me why this is and maybe how I can fix it?

SCV
June 19th, 2009, 09:57 PM
I have experience with programming now and I decided to look into C++. When I try to debug a Console Application program, it immediately exits and I have no idea why. I've looked into the MSDN forums and support but they have no solution because I use Visual C++ 2008 and those morons don't have support for C++ 2008.

So I was wondering if anyone here has the brain (or heart) to tell me why this is and maybe how I can fix it?
You're going to have to give alot more information than that. There are thousands of things you could be doing wrong. The best would be to provide the source for us to look at.

Captain Fabio
June 20th, 2009, 12:18 AM
I have used C++ for just under a year, and I have never once had it just "Exit."
Can you give me some more details?

Do you have any conflicting code that would cause it to close once run?

Ausaudriel
June 20th, 2009, 01:05 AM
When an application finishes it by default closes the console window. To have it stay open so you can examine your results, try adding a cin at the very end so that it expects input before closing.

There are other ways but if you're an absolute beginner go with that first.

Gumball Watterson
June 20th, 2009, 11:22 AM
I tried adding a cin command but it still immediately exits.

However, when I put cin >> var it doesn't exit, but it won't exit unless something is typed for the variable, which is very inconvenient.

I've seen the same problem with C# users in the MSDN forum, but it is because of the number of characters in the project title being over 10 and I've never had a project in C++ over 10 characters so that is not the problem with me.

As for some code, here is a quick basic Console program I wrote:

#include <iostream>
using namespace std;

int main(){
cout << "Hello World!\n";

cout << "What is your name?\n";

char name[15];

cin >> name;

cout << "\n\nHello, " << name;

return 0;

}


The name is shown for a split second the the program exits. The code was written and compiled with Visual C++ Express 2008.

twocows
June 20th, 2009, 04:26 PM
1. Deal with the cout problem. Just tell them to enter "yes" to end the program or something.
2. Run the program from command line.
3. Make a batch program that runs the program from command line.

I recommend 3. Make a txt file with the following:
$PROGRAM_PATH
pauseWhere $PROGRAM_PATH is the path to your program. Change the extension to .bat and you're good. This will make the user press a key to close the command prompt. If you distribute the program and the batch file in the same archive, you won't need to include the program path, just the name of the program (I think).

SCV
June 20th, 2009, 05:33 PM
The name is shown for a split second the the program exits. The code was written and compiled with Visual C++ Express 2008.
Add:
cin.ignore();
before:
return 0;

Gumball Watterson
June 20th, 2009, 07:35 PM
Tried, but it still exited immediately with the "cin.ignore()". I also want to know what is could causing that problem before attempting something.

X-626
June 20th, 2009, 08:39 PM
I'm pretty much a C++ noob (I'm more familiar with C, honestly), but I messed around a bit and found the problem. Here's the corrected code:
#include <iostream>
using namespace std;

int main(){
cout << "Hello World!\n";

cout << "What is your name?\n";

char name[15];

cin >> name;
cin.ignore(); //Makes it ignore the enter key

cout << "\n\nHello, " << name;
cin.get(); //Requires the user to hit enter before continuing

return 0;
}From what I remember, whenever you do a cin, you also have to do cin.ignore() right after it so that it ignores the enter key.
As I put in the comments, the cin.get() makes it wait for the user to hit enter before continuing any further (which will exit the program).

Gumball Watterson
June 21st, 2009, 03:50 PM
Thanks, that made things more convenient. I'll try to remember those 2 commands.

But I still don't seem to know what is apparently causing that problem.

HarrisonH
June 21st, 2009, 05:58 PM
http://www.learncpp.com/cpp-tutorial/07-a-few-common-cpp-problems/ explains it. Some compilers automatically pause the console after running the program. Others don't.

passionfruitt
June 22nd, 2009, 04:27 AM
at uni when we started using a new compiler, the same thing happened to me.
try putting system("pause"); before return 0; and it might work?

SCV
June 22nd, 2009, 05:40 AM
Putting two cin.get() or cin.ignore() should fix the problem then. Since the enter is probably getting read as a character for the first one.