• Our software update is now concluded. You will need to reset your password to log in. In order to do this, you will have to click "Log in" in the top right corner and then "Forgot your password?".
  • Welcome to PokéCommunity! Register now and join one of the best fan communities on the 'net to talk Pokémon and more! We are not affiliated with The Pokémon Company or Nintendo.

C++ Console App Problem.

Spinor

<i><font color="b1373f">The Lonely Physicist</font
5,176
Posts
18
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?
     

    SCV

    DPP Game Researcher
    178
    Posts
    16
    Years
  • 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.
     
    12,201
    Posts
    18
    Years
  • 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?
     

    Spinor

    <i><font color="b1373f">The Lonely Physicist</font
    5,176
    Posts
    18
    Years
    • Seen Feb 13, 2019
    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:

    twocows

    The not-so-black cat of ill omen
    4,307
    Posts
    15
    Years
  • 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).
     

    Spinor

    <i><font color="b1373f">The Lonely Physicist</font
    5,176
    Posts
    18
    Years
    • Seen Feb 13, 2019
    Tried, but it still exited immediately with the "cin.ignore()". I also want to know what is could causing that problem before attempting something.
     
    1,325
    Posts
    15
    Years
    • Age 34
    • Seen Jul 17, 2012
    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).
     

    Spinor

    <i><font color="b1373f">The Lonely Physicist</font
    5,176
    Posts
    18
    Years
    • Seen Feb 13, 2019
    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.
     
    34
    Posts
    14
    Years
  • 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

    DPP Game Researcher
    178
    Posts
    16
    Years
  • 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.
     
    Back
    Top