C++ Console App Problem.

Spinor

<i><font color="b1373f">The Lonely Physicist</font
  • 5,172
    Posts
    19
    Years
    • Seen Feb 13, 2019
    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?
     
    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.
     
    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?
     
    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:
    Code:
    #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.
     
    Last edited:
    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:
    Code:
    $PROGRAM_PATH
    pause
    Where $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).
     
    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;
     
    Tried, but it still exited immediately with the "cin.ignore()". I also want to know what is could causing that problem before attempting something.
     
    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:
    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).
     
    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.
     
    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?
     
    Back
    Top