0.7 — A few common C++ problems

In this section, we’ll address some of the common issues that new programmers seem to run across with fairly high probability. This is not meant to be a comprehensive list of compilation or execution problems, but rather a pragmatic list of solutions to very basic issues. If you have any suggestions for other issues that might be added to this list, post them in the comments section below.

Problem 1: When executing a program from the IDE, the console window blinks and then closes immediately.

Answer 1: Some compilers (eg. Bloodshed’s Dev C++) don’t automatically pause the console screen after the program has finished executing. If this is the case with your compiler, the following two steps will fix your problem:

First, add the following line near the top of your program:

Second, add the following code at the end of the main() function (right before the return statement):

This will cause your program to wait for you to press a key before continuing, which will give you time to examine your program’s output before your compiler closes the console window.

Other solutions, such as the commonly suggested system("pause") solution may only work on certain operating systems and should be avoided.

Note: Visual Studio will not pause at the end of a console application if it is run with debugging (Debug Menu->Start Debugging). If you want it to pause, you can either use the code solution above, or run your program without debugging (Debug Menu->Start Without Debugging).

Problem 2a: When compiling with Microsoft Visual C++, you get the following error: “c:vcprojectstest.cpp(263) :fatal error C1010: unexpected end of file while looking for precompiled header directive”
Problem 2b: When compiling with Microsoft Visual C++, your program produces compile errors, but your program doesn’t look incorrect.

Answer 2: This occurs when the Microsoft Visual C++ compiler is set to use precompiled headers (which it is by default) but one (or more) of your C++ code files does not #include the stdafx.h header as the first line. To fix this problem, simply locate the file(s) producing the error (in the above error, test.cpp is the culprit), and ensure the following line is at the very top of the file(s):

Note that for programs with multiple files, every C++ code file needs to start with this line.

Alternatively, you can turn off precompiled headers.

Problem 3: When trying to use cin, cout, or endl, the compiler says cin, cout, or endl is an “undeclared identifier”

Answer 3: First, make sure you have included the following line near the top of your file:

Second, make sure cin, cout, and endl are prefixed by “std::”. For example:

Problem 4: When trying to use endl to end a printed line, the compiler says end1 is an “undeclared identifier”

Answer 4: Make sure you do not mistake the letter l (lower case L) in endl for the number 1. endl is all letters. I recommend using a font that makes it clear the differences between the letter lower case L, upper case i, and the number 1. Also the letter capital o and the number zero can easily be confused in many non-programming fonts.

Problem 5: My program compiles but it isn’t working correctly. What do I do?

Answer 5: Debug it! You can find information on how to debug programs in lesson 1, specifically sections 1.11 -- Debugging your program (stepping and breakpoints) and 1.11a -- Debugging your program (watching variables and the call stack).

Problem 6: How do I turn on line numbering in Visual Studio?

Answer 6: Go to the Tools Menu, and choose Options. Under the Text Editor submenu, choose All Languages (or C/C++), and you’ll see a checkbox on the right for Line numbers.

Problem 7: When I compile my program in Visual Studio 2010, I get an error message about a COFF file being invalid. How do I fix this?

Answer 7: If you see the following error when compiling with Visual Studio 2010:

LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt

You’ve encountered a Microsoft OS/compiler incompatibility. It has nothing to do with your code.

The best first option is to download and install Visual Studio 2010 Service Pack 1.

If that does not fix your issue, there are many other good suggestions on this Stack Overflow thread about the various causes and solutions to this problem.

Problem 8: When I compile my program, I get an error about unresolved external symbol _main or _WinMain@16

Answer 8: This means your compiler can’t find your main() function. All programs must include a main() function.

There are a few things to check:
a) Does your code include a function named main?
b) Is main spelled correctly?
c) Is the file containing main part of your project? (if not, either move the main function to one that is, or add the file to your project. See lesson 1.8 -- Programs with multiple files for more information about how to do this).
d) Is the file containing function main set to compile? (Also see lesson 1.8 -- Programs with multiple files for more information about how to do this).

Problem 9: When I compile my program, I get a warning about “Cannot find or open the PDB file”

Answer 9: This is a warning, not an error, so it shouldn’t impact your program working. However, it is annoying. To fix it, go into the Debug menu -> Options and Settings -> Symbols, and check “Microsoft Symbol Server”.

Problem 10: I’m using Code::Blocks or g++ on the command line, and none of the C++11 functionality works

Answer 10: For Code::Blocks, go to Project->Build options->Compiler settings->Compiler flags and check “Have g++ follow C++11 ISO C++ language standard”

See lesson 0.5 -- Installing an Integrated Development Environment (IDE) for pictures of how to do this.

For compiling with g++ on the command line, add the following to the command line: -std=c++11

Problem 11: I’m using Visual Studio and get the following error: “1>MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function “int __cdecl invoke_main(void)” (?invoke_main@@YAHXZ)”

Answer 11: Most likely you’ve created the wrong type of project. Make sure you’re creating a Win32 Console Application, not a Win32 Project.

Problem 12: I ran my program and get a window but no output.

Answer 12: Your virus scanner may be blocking execution. Try disabling it temporarily and see if that’s the issue.

I have some other problem that I can’t figure out. How can I get an answer quickly?

As you progress through the material, you’ll undoubtedly have questions or run into unexpected problems. What to do next depends on your problem. But in general, there are a few things you can try.

First, ask Google. Find a good way to phrase your question and do a Google search. If you have received an error message, paste the exact message into google using quotes.
Odds are someone has already asked the same question and there is an answer waiting for you.

If that fails, ask on a Q&A board. There are websites designed for programming questions and answers, like Stack Overflow. Try posting your question there. Remember to be thorough about what your problem is, and include all relevant information like what OS you’re on and what IDE you’re using.

1.1 -- Structure of a program
0.6a -- Build configurations

173 comments to 0.7 — A few common C++ problems

  • Hi Alex, thank you so much. I like your site so much. It really helped me to learn C++. You helped me a lot. I do not know how to thank you. Thank you so much.

  • safy

    hi sir, i’m using Microsoft visual studio c++ 2017 so, when i want to build my first program {hello world ^^} an error happens it tells me "this project is out of date" .
    what can do for that please ?
    i need to know !!
    i am waiting for your answer.

    • Alex

      Try rebuilding your project.

      • Aleksandar

        Alex, when i have added this part into code i have get same error as safy, but rebuilding project have resolve this issue. Thanks for help.

        std::cin.clear(); // reset any error flags
        std::cin.ignore(32767, ‘\n’); // ignore any characters in the input buffer until we find an enter character
        std::cin.get(); // get one more char from the user

      • safy

        I tried to rebuilding and clean solution but to no avail .
        do you have any other idea that could help me please???

      • safy

        I also tried to uninstall vsudio2017  and reinstall it and the same popup window still appear "this project is out of date" .
        i expect you to explain what is this out of date project exactly means sir.

        • Alex

          Generally “this project is out of date” means you’ve made a change to your code and that you’ll need to recompile/relink your program before the updated code is active. This is expected behavior.

  • Daniel

    Hi, Alex. Nice work here!

    I’m not sure if this error is common enough to be added to this section, but it may be too complex for a beginner to solve by himself:

    When I run Code::Blocks with a debugger (gdb) for the first time, the debugger didn’t stop at any breakpoint. Code::Blocks didn’t show any useful message and the console window (that one where gdb runs) closed as fast as it had opened.

    By running gdb manually, I was able see the following message:

    The cause of the problem: for some unknown reason (is this a Ubuntu thing?), my user’s profile didn’t set the "SHELL" env var used by gdb. So "exec $SHELL -c exec" didn’t work, breaking gdb execution.

    One way of fixing it: to set the env var before opening Code::Blocks

    export SHELL=/bin/sh; codeblocks

    Another alternative would be trying to add the env var to your user’s ~/.profile (or the equivalent file), but I haven’t tried that yet.



  • Aakash Sehrawat

    I just ran the same code as in the example.
    Taking input and printing it out. The program on running asks for input but never displays anything and is running continously. Doesn’t end at this.It doesnt nothing but doesnt print out anything. I have to use ctrl-z to end it.
    I’m using sublime text on Ubuntu.
    Please help

  • Another way to make the program to wait for the user to see the result of the output is to select:

    Debug >> start without debugging

    or simple press CTRL+F5

    But I like to have a code like you have done here so if the user presses F5 he/she can still see the output.

  • Ahtazaz Khan

    1)when i used return 0 in the program. console screen doesn’t wait after running,mean not wait for "press any key to continue". why?
    2) for this purpose i used in my code std::cin.get(); before return. I think get() is used here for getting any character as input and console screen close, Here get() is getting any character but doesn’t close console screen until i press enter.Why??

    • Alex

      1) Returning a value to the operating system, and waiting at the end of the program have nothing to do with each other.
      2) std::cin.get() returns a single character. If no character is ready to be returned, std::cin.get() waits for user input. The user input is terminated by a newline (enter). So the user can actually enter multiple characters here. The first will be returned by std::cin.get(), and the rest will be stored for future extraction.

      • Ahtazaz Khan

        ok right.thanks Alex.
        What is 32767 in the above ignore statement??

        • Alex

          The maximum number of characters to ignore. 32767 should be larger than just about any input, so even if you previously entered input that hasn’t been extracted yet, this line will ignore all of it.

  • Jeremiah

    The current installer for Visual Studio 2017 is broken. After some digging around (and a headache) Microsoft is aware of the issue pending a fix. If you’re getting the "Package ‘Win10SDK_10.0.15063.Desktop,version=10.0.15063.8’ failed to install" you will have to download and install the SDK separately.

    Also, thank you to the author for these excellent tutorials!

  • Felipe

    10 years later and still updating it, wow.

  • WalterK

    I started using codeblocks 16.01 in Windows 10 for C++ development and encountered an issue that I haven’t been able to resolve. When I use C code, e.g.

    #include <stdio.h>
    #include <stdlib.h>

    int main()
        printf("Hello world!\n");
        return 0;

    The program will run fine but when I use the C++ equivalent

    #include <iostream>

    using namespace std;

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

    a DOS box opens, it says it is running but I don’t get any output. I disabled my virus scanner (Norton) temporarily but without result. Anyone any idea what might happen?

    • WalterK

      I found the answer. There was syill an older version of MinGW installed in a different folder. I removed all installations, including CodeBlocks, then reinstalled CodeBlocks. Everything works fine now.

  • Joseph

    hello, when editing code i sometimes need to erase a couple bits in the middle of lines but when i do it does a weird type of selection where it selects the character not in between the characters, do you get what i mean? this means whenever i write something else on that line it overwrites everything that it comes in contact with, is there a way i can change my selection mode to select in between characters so instead of overwriting it just pushes whats infront of it a bit farther to make room for what im typing, not sure if that made sense but please help its very annoying.

    • Alex

      I think I get what you mean. How selection behaves is really up to the individual code editors, so you’d have to look inside your editor settings to see if there’s any settings to change this.

      • Joseph

        ah well i just installed the new 2017 version and thank the lord it selects how i want it too! btw im so thankful for you (im assuming you wrote all these tutorials or a few of them) giving me the opportunity to learn c++, i want to me a programmer and this is going to be the start of my life so thank you a lot!

    • Porter

      usually, insert changes selection mode, so you probably accidentaly hit it.

  • splinter

    i am new to programming . i was opting to learn c++.i was much impressed by your tutorial.can i use MVS .
              thank you

  • Marloes

    I want to insert pictures in my project. I use an API: SFML but for that I need to make a WIN32 application. Will I get an error for some functions?? And if I do, how can I fix it?

  • Pacster

    SORRY!!!! For long message but can you tell me if I told somthing wrong (PS Your tutorial is amazing)

    • Alex

      You’re in the ballpark. For #5, it acts as a line break, not a backspace.

      I talk more about all of these things in future lessons (in a lot more detail, so don’t worry about having everything nailed down here).

  • Bart

    To append my problem of Nov 30:
    The "Released" program worked well on another computer with Windows 10 installed. No problem with ".dll files. However, a modified version of the same program does not "Release" and I do not know why, since it compiled without a hitch. This version runs well on my computer which has the VS 2015 in c++ installed.
    I do not understand the ".dll" files. Are they part of the OS or do they come with the released program?

    As I was writing the above I compiled the program again after doing some other cleanup operations monkey style. This time it worked but I don’t know why. There were no error messages before and after. It still begs the question why this program does not work on Windows 7.

    • Alex

      The .dll files are the C++ runtime library in shared library format (so you only need to install them once on a computer rather than statically link them into every executable).

      In some cases, they may come with the OS (at least on Windows, since Microsoft owns both the Visual Studio compiler AND the OS). It’s possible Windows 10 comes with these files and Windows 7 doesn’t.

      But you’re better off either assuming they don’t exist and having your program try to install them (via the redistributable) or do a static linking instead.

  • Bart

    I recompiled two simple programs on VS 2015 in c++.
    No problem with that and in one case it actually "Released".
    Both worked on my computer: Windows 10.
    I copied it from the "Release" folder to another computer with Windows OS.
    It demanded several ".dll" files installed.
    I installed them from a web site but it still continued asking for the file I just installed. The file is in the Windows system folder.

    Please reply.

    • Alex

      You’re probably linking the C++ runtime as a dynamic DLL. If that’s the case, then you’d need to ensure those DLLs are installed on the target system. Microsoft provides a redistributable package for this purpose -- you can bundle it with your application and run it as part of your installer (or download it and run it manually once on the other machine, if you’re only doing this for one machine).

      The other alternative is to compile the C++ runtime into your executable as a static library. It’ll inflate the size of your executable significantly but you don’t have to distribute other files. You can change this setting through your project properties -> C/C++ -> Code Generation -> Runtime Library. Change from /MD to /MT (for release configurations) or /MDd to /MTd (for debug configurations). Then recompile.

  • GEScott71

    Great info - thanks Alex.  I enjoy reading all of the comments.  I started to play around and added a 2nd step requiring an extra key press.  Here is my code:

    Similar to others above, std::cin.clear(); waits for a key press already.  And my program already stopped at the end with a "Press any key to continue . . . " when run from within my IDE (Visual Studio 2015).  So this code takes 4 key presses when run from within my IDE, and 3 presses when I run the *.exe directly.

  • MeGaMinD

    i run my program , it works fine but it doesn’t say press any key to continue, i should press "enter"   2 times to close the console application
    i tried debugging but didn’t work, i use visual studio express 2012

  • My dear c++ Teacher,
    Please let me say, Dev-C++ 5.11 automatically pause the console screen after the program has finished executing.
    With regards and friendship.

  • My dear c++ Teacher,
    Please let me send simplest possible code for "Problem 1: When executing a program from the IDE, the console window blinks and then closes immediately".

    It works perfect on Dev-C++ 4.9.92.
    With regards and friendship

  • Matt

    Typo in problem #9:
    "Problem 9: When I compile my program, I get a warnings…."

    I think "warnings" should be singular.

  • dontletmegetme

    Thankfully haven’t had these problems (yet)

  • Great! It seems i have learned something new today… 😉

  • E. Roberston

    Tank you for your quick response to this question. I tried your solution before I posted this question, and it did not work then. However when creating a new project, dll is one of the optional project types I can select. Still don’t understand why dll is not recognized by VS.

  • E. Robertson

    I’m new to programing and have written a short, simple routine. It runs through the build process just fine but when I debug or start without debugging I get a message "Unable to start program ‘C:\***\projname\Debug\projname.dll’".
    I’m using VS2012 Express on a Windows 10 system and tried setting this as console project. How can I resolve this issue?

    • Alex

      The problem here is that you somehow are producing a dll (shared library) instead of a standalone executable. Try recreating your project and make sure you have selected the application type “Console application” and not “DLL”.

  • liviu balas

    Hello, Alex! I have Windows 10 and CodeBlocks. My CodeBlocks sayed that he dosen’t find the GNU GCC Compiler.
    What can i do? Please, help me!

  • Darren

    On Problem 1.

    Just posted this elsewhere in answer to why the console pops-up then immediately closes (it is specific to Visual Studio):

    If using Visual Studio then this is the default behaviour; the program exits and terminates the console window immediately. You can remedy this by going to the Property Sheet for the VS project (right-click the solution name in the "Solution Explorer" pane and click "Properties" at the bottom of the sub-menu) select linker -> system and the first field in the sheet should read "SubSystem" with a drop-down menu icon. Click the icon and select a "Console" subsystem and okay the change. You will have to recompile project (shortcut key f7) but now if you run it using ctrl-f5 the console will hang after the program has finished, allowing you to read the output. Note that pressing f5 alone will not make the console hang.

  • Phan Hoang Phuong

    Hi Alex, thank you so much. I like your site so much. It really helped me to learn C++. You helped me a lot. I do not know how to thank you. Thank you so much.

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter