My C++ code is not running the way it should



  • I am using Notepad++ with C++ and I have a short program that prompts the user to enter an integer. After he enters his number, the program should output the number that he just entered. But for whatever reason, after I enter my number into the program and hit Enter, the program just vanishes. It doesn’t echo the number that I just entered. I’m using MinGW.

    This is the script I have entered in the Execute box:

    npp_save
    cd “$(CURRENT_DIRECTORY)”
    g++ “$(FILE_NAME)” -o $(NAME_PART)
    NPP_RUN $(NAME_PART)

    Can you help me out?



  • The “NPP_RUN” is executing it in a separate process; as soon as that process is done, the window goes away; so if you want to keep the NPP_RUN to keep it in a separate window, then you need to have a system(“pause”) or similar at the end of your C++ program.

    Alternatively, if you want the program to just dump its output in the NppExec console, just like the output of g++, you could use either of:

    npp_save
    cd “$(CURRENT_DIRECTORY)”
    g++ “$(FILE_NAME)” -o $(NAME_PART)
    cmd /c $(NAME_PART)
    

    or

    npp_save
    cd “$(CURRENT_DIRECTORY)”
    g++ “$(FILE_NAME)” -o $(NAME_PART)
    $(NAME_PART)
    

    (If you use one of these, you have to be more careful with flusing STDOUT, if you want to see any prompts before the requested user input)

    I tested with this self-contained example (using gcc as my compiler, rather than g++)

    #include <stdio.h>
    main() {
        int d;
        printf("hello, world\n");
        fflush(stdout);
        fscanf(stdin, "%d", &d);
        printf("goodbye(%d)\n", d);
        fflush(stdout);
        //system("pause"); // use this line if you want to keep the npp_run to run in a separate window
    }


  • Also, put "$(NAME_PART)" in the quotes, just like you put "$(FILE_NAME)" in quotes: if $(FILE_NAME) has a space that you need to protect against, so will $(NAME_PART) (except in the very rare occasion when your file extension has a space… but that probably won’t happen with a .c or .cpp file).

    npp_save
    cd “$(CURRENT_DIRECTORY)”
    g++ “$(FILE_NAME)” -o "$(NAME_PART)"
    cmd /c "$(NAME_PART)"
    

    or

    npp_save
    cd “$(CURRENT_DIRECTORY)”
    g++ “$(FILE_NAME)” -o "$(NAME_PART)"
    "$(NAME_PART)"


  • Does not seem to work correctly. Here is my output:

    Current directory: G:\C++ Projects
    g++: error: “GettingInput.cpp”: No such file or directory
    g++: fatal error: no input files
    compilation terminated.
    Enter a value for x: 7
    ered

    Some of the output letters are being cut off. Also the system pause thing makes no difference. I would like to view the output from all my programs in the external black DOS window if possible. I know Notepad++ can be set up to view your program in the internal output window, but I would also like to know how to set it up so I can view both kinds of programs in an external window. What I mean by both kinds of programs is programs that simply output something to the screen, and programs that ask for user input.

    It still is not working the way it should.



  • The lack of the waiting is not the fault of NPP nor NppExec: it’s the way the cmd.exe works. If you were to compile your same program (whether or not thru the NppExec interface) then use Windows Start Menu > Run > "G:\C++ Projects\GettingInput.exe" or …Run > cmd.exe /C "G:\C++ Projects\GettingInput.exe", the cmd.exe window would disappear just after, unless you have something (either a second c fscanf or c++ cin or whatever they call that input stream – sorry, I’m not a c++ expert – or a system("pause") requesting the user HIT ENTER before terminating the GettingInput.exe) – unless you have that pause, the cmd.exe window will disappear.

    In case you don’t know, there’s a cmd /K option that runs any arguments passed to it in a separate cmd.exe processes, and then stays at that cmd.exe's prompt rather than exiting, which is probably what you want. (See below.)

    Further debugging for you: Looking at your output, the source didn’t compile, hence the g++ fatal error. (Presumably, after the fatal error, it was running a previously-compiled version of your executable, rather than the most recent update.) Based on the error message, my guess is that it’s the smart quotes shown in your output for “GettingInput.cpp” instead of standard quotes in "GettingInput.cpp"; with proper quotes, and my source code, it works for me (see below).

    For the c example I gave, to use system("pause") you also need to include stdlib.h (sorry, I hadn’t tested the system("pause")). But once I have that, it works for me as I’ve described.

    The c source code I am using:

    #include <stdio.h>
    #include <stdlib.h>
    main() {
        int d;
        printf("hello, world\n");
        fflush(stdout);
        fscanf(stdin, "%d", &d);
        printf("goodbye(%d)\n", d);
        fflush(stdout);
        system("pause");
    }
    

    Note that I flush stdout, like I mentioned yesterday, to make sure that “some of the output letters” don’t get “cut off”. When I don’t flush, some of the letters do get cut off. (That’s a “feature” of the gnu compiler; it changes auto-flush behavior when it’s going to a terminal/console vs output being piped (and it considers the NppExec framework to be a pipe).)

    I used this exact NppExec script (copy/pasted from my NppExec “Execute…” dialog):

    npp_save
    cd "$(CURRENT_DIRECTORY)"
    g++ "$(FILE_NAME)" -o "$(NAME_PART)"
    cmd /c "$(NAME_PART)"
    

    (I even used g++ instead of gcc as my compiler, even for my plain-c source; it still works, since c is valid c++ as well)

    As shown, I removed the smart quotes from the NppExec script, which had somehow made it into our earlier posts, and made sure they were normal quotes.

    But after that, it worked just fine in the console:

    NPP_SAVE: C:\Temp\Data\npp.c
    CD: C:\Temp\Data
    Current directory: C:\Temp\Data
    g++ "npp.c" -o "npp"
    Process started >>>
    <<< Process finished. (Exit code 0)
    cmd /c "npp"
    Process started >>>
    hello, world
    5
    goodbye(5)
    Press any key to continue . . . 
    
    <<< Process finished. (Exit code 0)
    ================ READY ================
    

    However, if you would really prefer it in the separate cmd.exe window:

    npp_save
    cd "$(CURRENT_DIRECTORY)"
    g++ "$(FILE_NAME)" -o "$(NAME_PART)"
    npp_run cmd.exe /k "$(NAME_PART)"
    

    Running using this, my NppExec Console window shows:

    NPP_SAVE: C:\Temp\Data\npp.c
    CD: C:\Temp\Data
    Current directory: C:\Temp\Data
    g++ "npp.c" -o "npp"
    Process started >>>
    <<< Process finished. (Exit code 0)
    NPP_RUN: cmd.exe /k "npp"
    ================ READY ================
    

    And my cmd.exe window shows

    hello, world
    5
    goodbye(5)
    Press any key to continue . . .
    
    C:\Temp\Data>

Log in to reply