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):
1 2 |
#include <iostream> #include <limits> |
Second, add the following code at the end of your main() function (right before the return statement):
1 2 3 |
std::cin.clear(); // reset any error flags std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore any characters in the input buffer until we find an enter character std::cin.get(); // get one more char from the user |
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 _WinMain@16
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:
1 |
#include <iostream> |
Second, make sure each use of cin, cout, and endl are prefixed by “std::”. For example:
1 |
std::cout << "Hello world!" << std::endl; |
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):
1 |
#include "pch.h" |
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 _WinMain@16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main@@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.
![]() |
![]() |
![]() |
I've been using VisualStudio on windows with no problems. Now I'm trying to use Code::Blocks and the Hello World program does this in the command terminal:
---
Process returned 32761 (0x7FF9) execution time : 1.106 s
Press any key to continue.
---
And google isn't helping.
I think we can use "using namespace std;" below the "#include<iostream>" instead of using "std::" as a prefix for every usage of cin, cout and endl.
One thing I've noticed is that this tutorial uses std::
If you put
using namespace std;
at the top of your code
You won't have to put std:: before everything.
I saw the exact same thing, but then after coding for a few weeks (and asking questions), people have been pointing me to this question on Stack Overflow:
https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
To sum up, its good practice to use it!
A namespace is basically like a container that is used to avoid naming conflicts in a program, if you have say a "cout" with some other functionality in another namespace you will not be able to use it in your program since the compiler will always use the "std" version of it if you've used the "using namespace std". This may not matter when you are learning and will save some time but its a bad practice in the long run.
Hello, im usin Visual Studio and the debug show me this
'HelloWorld.exe' (Win32): 'C:\Users\Lenovo\source\repos\HelloWorld\Debug\HelloWorld.exe' cargado. Símbolos cargados.
'HelloWorld.exe' (Win32): 'C:\Windows\SysWOW64\ntdll.dll' cargado.
'HelloWorld.exe' (Win32): 'C:\Windows\SysWOW64\kernel32.dll' cargado.
'HelloWorld.exe' (Win32): 'C:\Windows\SysWOW64\KernelBase.dll' cargado.
'HelloWorld.exe' (Win32): 'C:\Windows\SysWOW64\msvcp140d.dll' cargado.
'HelloWorld.exe' (Win32): 'C:\Windows\SysWOW64\vcruntime140d.dll' cargado.
'HelloWorld.exe' (Win32): 'C:\Windows\SysWOW64\ucrtbased.dll' cargado.
'HelloWorld.exe' (Win32): 'C:\Windows\SysWOW64\kernel.appcore.dll' cargado.
'HelloWorld.exe' (Win32): 'C:\Windows\SysWOW64\msvcrt.dll' cargado.
'HelloWorld.exe' (Win32): 'C:\Windows\SysWOW64\rpcrt4.dll' cargado.
El subproceso 0x232c terminó con código 0 (0x0).
El subproceso 0x2368 terminó con código 0 (0x0).
El subproceso 0xa50 terminó con código 0 (0x0).
El programa '[2432] HelloWorld.exe' terminó con código 0 (0x0).
When i execute the program without the debuggin works fine, but when I open de .exe in the directory the windows opens and inmediatly closes up
what
fatal error: array (iostream etc.): No such file or directory
The above compiler error may be caused by giving .c extension instead of .cpp for your file... :) Worth to mention.
Hi, when I compile this code, it gives me error: unused variable 'x' [-Werror=unused-variable]
Here is the code
1 #include <iostream>
2
3 int main()
4 {
5 int x{ 2 + 3 }
6
7 return 0;
8 }
Someone please help me out
What can I do?
You have declared a variable `x`, but you're not using it. This doesn't make sense so the compiler is warning you.
If you use `x`, the warning goes away. For example you can print `x`
When I run the program I get my directory added to the console that outputs "Hello World!". How do I remove this from future projects?
here's a screenshot https://imgur.com/CFooxn2
thanks btw for the guide! it is very very well done!
The output is from your IDE, not from your program. If you run your program directory, you won't see this message.
Thank you for providing such detailed information.
Dear sir,
how i known about your other website like learnopengl.com. can i get the list of your all website.
and knowledge about Artificial intelligence & data structure are necessary for programmer.Is there any website website development or designing like this
Thanks for this tutorial
please reply
Hi everyone!!
I'm new here. I just registered for BSc in Computer Science with one of the universities here in SA, majoring in Maths & Computer Science. Currently busy with the basics....quiet interesting. But I am honestly clueless on what can I do/create at this level. Or at what level can I be able to create projects using C++
I wanted to bring up a potential problem Code Blocks users might face when trying to run their executables outside of the IDE.
The exe it creates in debug build rely on DLL files inside the Code::Blocks MINGW folder, but when running them outside of Code Blocks the DLLs cannot be found.
The solution is to add the MinGW\Bin to System Path
C:\Program Files\CodeBlocks\MinGW\bin
If you need help on how to edit system path you'll have to google that, apparently windows 10 made it easier to do and there are a few dedicated programs to make it less of a hassle.
I am using Particle IDE to run the "Hello World" code but when I try to compile the code I receive the same note which is:
Error: Could not compile. Please review your code
Thank you for your help.
Hey I'm not very experienced with IDEs but the error seems to be an IDE problem. Solve the problem by trying another IDE. However, I think the author mentioned that you should look up errors online (more specific: thread pages like stack or for your case on particle community). I picked up something for you that might help:
https://community.particle.io/t/error-could-not-compile-please-review-your-code-even-for-example-code/17670
If that didn't help search for yourself! I'm sure you'll find something, I bet you are not the only one with that problem (Searching for a solution online is just like testing and debugging. Give your error message and modify your search input until you'll get a result that helps you).
I followed the guide of the previous versiom when I created my project, and I still get the error LNK2019
I would have been memey as all hell and given people the lmgtfy link instead of just telling people to look it up on google.
Hey, I am revisiting this website from scratch after slightly over an year of being here.
I have now learned Java and built some Android projects as well!!
I just felt the need to revisit this marvelous website that was my entering point into programming, and to be honest: I MISS POINTERS and operator overloads!!! I miss c++'s complexity!!
Hello everyone, I am glad to be back!!
Hello again! I'm glad to hear you're still programming :)
If Android development is something you want to do, but you don't like Java, you can use C++. Have a look at Qt. There's also the NDK, but I never used that so I can't tell you about it.
FYI if you go to run your program in Code::Blocks in Linux, you'll need to make sure under Settings -> Environment that you have the right Terminal selected for the program to run in. By default in Arch KDE it pulls an error; if you're using the KDE Desktop environment change the drop down to "konsole -e".
Which terminal you end up using is up to you of course. :)
good
My suggestion for common problems would be a tutorial about #include guards. When beginning, I wasted countless hours chasing out linking/naming errors before I finally figured out what include guards were.
"Debug it! There are tips on how to diagnose and debug your programs later in chapter 1 or 2."
"in chapter 3" (Debugging C++ Programs)?
here: "... before your operating system closes the console window."
0.7: "before your IDE"
here:
0.7: "// .. we find a newline" (but no word "character")
here: "Second, add the following code at the end of your main() function (right before the return statement):"
0.7: Second, add the following code at the end of the main() function (just before the return statement):
here: "(right ...", "your main()"
0.7: "(just ..." , "the main()"
"... before the return statement)"
"(Visual Studio users, make sure these appear after ..."
Redundancy in 0.7?
In 0.7 there is word "lines" after "these":
"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):"
"Try running in Start Without Debugging (ctrl-F5) mode."
As your requested: no capitalization in "ctrl".
<a href="http://www.stackoverflow.com">Stack Overflow</a>
Can your, please, change to https (redirects to it anyway)?