0.8 — 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.

General run-time issues

Q: When executing a program, the console window blinks and then closes immediately.

First, add or ensure the following lines are near the top of your program (Visual Studio users, make sure these lines appear after #include “pch.h” or #include “stdafx.h”, if those exist):

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

This will cause your program to wait for the user to press a key before continuing, which will give you time to examine your program’s output before your operating system 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.

Older versions of Visual Studio may not pause when the program is run in Start With Debugging (F5) mode. Try running in Start Without Debugging (Ctrl-F5) mode.

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

Your virus scanner or anti-malware may be blocking execution. Try disabling it temporarily and see if that’s the issue.

Q: My program compiles but it isn't working correctly. What do I do?

Debug it! There are tips on how to diagnose and debug your programs later in chapter 3.

General compile-time issues

Q: When I compile my program, I get an error about unresolved external symbol _main or [email protected]

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) When you compile your program, do you see the file that contains function main() get compiled? If not, either move the main() function to one that is, or add the file to your project (see lesson 2.7 -- Programs with multiple code files for more information about how to do this).
d) Did you create a console project? Try creating a new console project.

Q: I'm trying to use C++11/14/17/XX functionality and it doesn't work

If your compiler is old, it may not support these more recent additions to the language. In that case, upgrade your compiler.

For modern IDEs/compilers, your compiler may be defaulting to an older language standard. We cover how to change your language standard in lesson 0.12 -- Configuring your compiler: Choosing a language standard.

Q: When trying to use cin, cout, or endl, the compiler says cin, cout, or endl is an 'undeclared identifier'

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

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

If this doesn’t fix your issue, then it may be that your compiler is out of date, or the install is corrupted. Try reinstalling and/or upgrading to the latest version of your compiler.

Q: When trying to use endl to end a printed line, the compiler says end1 is an 'undeclared identifier'

Make sure you do not mistake the letter l (lower case L) in endl for the number 1. endl is all letters. Make sure your editor is using a font that makes 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.

Visual Studio issues

Q: When compiling with Microsoft Visual C++, you get a C1010 fatal error, with an error message like "c:\vcprojects\test.cpp(263) :fatal error C1010: unexpected end of file while looking for precompiled header directive"

This error occurs when the Microsoft Visual C++ compiler is set to use precompiled headers but one (or more) of your C++ code files does not #include “stdafx.h” or #include “pch.h” as the first line of the code file.

Our suggested fix is to turn off precompiled headers, which we show how to do in lesson 0.7 -- Compiling your first program.

If you would like to keep precompiled headers turned on, to fix this problem, simply locate the file(s) producing the error (in the above error, test.cpp is the culprit), and add the following line at the very top of the file(s):

Older versions of Visual Studio use “stdafx.h” instead of “pch.h”, so if pch.h doesn’t resolve the issue, try stdafx.h.

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

Alternatively, you can turn off precompiled headers.

Q: Visual Studio gives the following error: "1MSVCRTD.lib(exe_winmain.obj) : error LNK2019: unresolved external symbol [email protected] referenced in function "int __cdecl invoke_main(void)" ([email protected]@YAHXZ)"

You’ve likely created a Windows graphical application rather than a console application. Recreate your project, and make sure to create it as a Windows (or Win32) Console project.

Q: When I compile my program, I get a warnings about "Cannot find or open the PDB file"

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

Something else

Q: 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.

0.9 -- Configuring your compiler: Build configurations
0.7 -- Compiling your first program

286 comments to 0.8 — A few common C++ problems

  • Mark

    Hi Alex,
    I want to know will it be an error if I use both #pragma once and the #ifndefine in the same header file?

    • nascardriver

      Hi Mark!
      It shouldn't cause any errors but it'll be redundant.

      Case 1 (pragma once is supported by the compiler):
      - Works fine, as long as you didn't mess up your header guard, this is an extra source of errors.
      Case 2 (pragma once isn't supported by the compiler):
      - That's probably a compile time error, let's assume the compiler just ignores it
      - The header guard will take care of your file

      In both cases the header guard will be used, so why use pragma once at all?

    • Alex

      No, but if you're going to use the #ifndef method, why bother with the #pragma once?

  • Shaheen

    How do I save the Hello world program I just created, if I want to continue later? I am using Microsoft Visual Studio 2017

  • 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 all code inside code tags: [code]your code here[/code]